Test-related React setTimeout enzymes causing errors

I want to check if a method is called after a certain time interval in the React component using an enzyme and sinon mocha.

Component response

var TestComp = React.createClass({ myFunc: function() { console.log('I am called'); } componentDidMount: function() { this.timer = window.setTimeout(myFunc, 2000); } componentWillUnmount: function() { clearTimeout(this.timer); } }) 

Unit test

 var clock; before(function () { clock = sinon.useFakeTimers(); }); after(function () { clock.restore(); }); it('myFunc is called once', function() { spy(TestComp.prototype, 'myFunc'); var wrapper = mount(<TestComp/>); clock.tick(3000); expect(TestComp.prototype.myFunc.calledOnce).to.equal(true); }); 

Error

It throws an assertion error. The test crashes in a synchronization event, which I identified using some console logs on a node wrapper. He says a stack trace error. However, the test script launches myFunc , as seen in the console log.

How can I grab a call to myFunc ? Does anyone know why this is happening?

The console error is the same as https://github.com/sinonjs/sinon/issues/87#issuecomment-8547823

Update

Stacktrace:

 'Error: Stack Trace for original at Object.wrapMethod at spy at Context.<anonymous> at callFn at Test.Runnable.run at Runner.runTest at next at Immediate.<anonymous> at Immediate.<anonymous> at runCallback at processImmediate [as _immediateCallback] restore: { [Function] sinon: true } } and in between all filename locations. 
+5
source share

All Articles