jhnns / rewire-webpack

Dependency injection for webpack bundles
The Unlicense
121 stars 20 forks source link

Rewire throws error when using ES6 Classes #11

Closed damassi closed 9 years ago

damassi commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

Closing this and moving the issue over to focus on imports

jhnns commented 9 years ago

That's strange... #12

damassi commented 9 years ago

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