Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewire throws error when using ES6 Classes #11

Closed
damassi opened this issue Apr 17, 2015 · 4 comments
Closed

Rewire throws error when using ES6 Classes #11

damassi opened this issue Apr 17, 2015 · 4 comments

Comments

@damassi
Copy link

damassi commented Apr 17, 2015

Note that I am using the 'rewire-webpack' plugin. Please close if N/A:

I've just finished porting all of my React.js code to ES6 Classes and found that my tests have broken due to Rewire (seemingly) not supporting the new syntax.

Given the simplest example:

// DummyClass.js

export default class DummyClass {
  constructor() {}
}

And my test, which consists of just a single import:

var require = require('rewire');
var DummyClass = rewire('./DummyClass');

This error is thrown:

TypeError: Cannot read property 'call' of undefined
        at Object.rewire [as default] (webpack:///./~/rewire-webpack/lib/rewire.web.js?:10:34)

Any assistance would be appreciated!

@damassi
Copy link
Author

damassi commented Apr 17, 2015

Ok, so more progress on understanding this as it applies to testing:

Since upgrading Webpack, I'm now required to "require" rewire inside of my tests directly. Meaning this:

var rewire = require('rewire');
describe('Test', function() {
    it('should work', function() {
       var DummyClass = rewire('./DummyClass') // throws error
    })
})

But this works:

describe('Test', function() {
    it('should work', function() {
       var rewire = require('rewire');
       var DummyClass = rewire('./DummyClass') // works.
    })
});

Another thing: imports do not work for objects you wish to stub:

// DummyClass.js
import states from 'utils/states';
export default class DummyClass {
  constructor() {}
}

// DummyClass-spec.js
describe('Test', function() {
    it('should work', function() {
       var rewire = require('rewire');
       var DummyClass = rewire('./DummyClass');
       DummyClass.__set__('states', 'foo'); // throws ReferenceError: states is not defined
    })
});

When importing using var states = require('utils/states') everything is just fine.

Apologies -- lots going on here, but I feel I've been able to narrow the issue down.

@damassi
Copy link
Author

damassi commented Apr 17, 2015

Closing this and moving the issue over to focus on imports

@damassi damassi closed this as completed Apr 17, 2015
@jhnns
Copy link
Owner

jhnns commented Apr 22, 2015

That's strange... #12

@damassi
Copy link
Author

damassi commented Apr 22, 2015

@jhnns, the setup that they describe in #12 matches mine nearly completely; definitely a duplicate ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants