i-like-robots / rewireify

Rewireify is a port of Rewire for Browserify that adds setter and getter methods to each module so that their behaviour can be modified for better unit testing.
https://www.npmjs.com/package/rewireify
Other
59 stars 10 forks source link

Error message when calling __set__ #11

Closed jhorneman closed 9 years ago

jhorneman commented 9 years ago

I hope this is the right repo.

When I call __set__ on a module, to mock a function it requires from another module, I get the following error message:

PhantomJS 1.9.8 (Windows 7 0.0.0) ERROR
  TypeError: 'undefined' is not a function (evaluating 'function (revertArgs) {
          __set__.apply(null, revertArgs);
      }.bind(null, arguments.revertArgs)')

I've made a minimal reproduction repo here: [https://github.com/jhorneman/karma-browserify-repro]().

Just run npm install, then npm run test.

I'm using Karma, Jasmine, Browserify, and Rewireify, under Windows 7.

Please let me know if you need more information.

i-like-robots commented 9 years ago

Thanks for setting up the test case, that makes my life much easier! I'll take a look when I can.

i-like-robots commented 9 years ago

I haven't had time to set this up yet but looking at the code - have you tried switching exports to module.exports? Rewireify assigns the __get__ and __set__ properties on module.exports which takes precedence over exports (see here for more info).

i-like-robots commented 9 years ago

I've had a minute and managed a proper look. Downgrading Rewireify to 0.2.0 in your example app works. This version of Rewireify bundled an old version of the injected __set__ method from Rewire so this may be a bug may be within the Rewire dependency or Rewireify's integration with it.

jhorneman commented 9 years ago

Thanks! (module.exports made no difference.)

By the way, I also added the issue over at Rewire: https://github.com/jhnns/rewire/issues/70

I'm glad there is a workaround at least. I was going crazy since this seemed like such a common use case and I wondered what I was doing wrong.

i-like-robots commented 9 years ago

Are you able to run the test runner successfully in your browser (rather than through headless PhantomJS)? Mocking the methods of dependencies is covered in the test suite and is working as expected when in Chrome and Firefox.

jhnns commented 9 years ago

Function.prototype.bind is undefined in PhantomJS 1.x (See https://github.com/ariya/phantomjs/issues/13107)

i-like-robots commented 9 years ago

Thanks for taking a look @jhnns and figuring it out =D

jhorneman commented 9 years ago

Thank you for figuring out what the problem was.