rewire icon indicating copy to clipboard operation
rewire copied to clipboard

Doesn't work with `jasmine.clock()`

Open ttskch opened this issue 9 years ago • 3 comments

I found rewire doesn't work with jasmine.clock().

  • Rewire a module uses setTimeout()
  • Test the module with jasmine.clock()
  • jasmine.clock().tick() doesn't work

I made a sample to reproduce this here. Please see it :bow:

To be short:

module.exports = function () {
    console.log(1);

    setTimeout(function () {
        console.log(2);
    }, 100);

    console.log(3);
};

When rewire above module and test it as below, the test fails.

jasmine.clock().install();

spyOn(console, 'log');

func();

jasmine.clock().tick(101);

expect(console.log).toHaveBeenCalledWith(2);  // this fails

jasmine.clock().uninstall();

Of course when use require instead of rewire test passes correctly.

ttskch avatar Oct 28 '16 07:10 ttskch

Hi, do you have a workaround for this?

andreaazzara avatar May 16 '17 16:05 andreaazzara

@andreaazzara I use setTimeout() actually in my test code instead of jasmine.clock() for now. See here.

ttskch avatar May 16 '17 21:05 ttskch

I did a PR with workaround. The solution is pretty simple I think

var rewire = require('rewire');
var rewireOptions = {
  ignore: ['setTimeout']
};
var myModule = rewire('./myModule', rewireOptions);

killmenot avatar Sep 15 '17 22:09 killmenot