Below test case fails on timeout.
suite('#sandbox#', function() {
test('should restore all fake', function(done) {
var foo = {
bar: function() {
return 3;
}
};
var sandbox = sinon.sandbox.create();
sandbox.stub(foo);
foo.bar.returns(1);
assert.equal(1, foo.bar());
sandbox.useFakeTimers();
var ok = false;
setTimeout(function() {
ok = true;
}, 5000);
sandbox.clock.tick(5000 + 1);
assert.equal(true, ok);
sandbox.restore();
assert.equal(3, foo.bar());
setTimeout(function() {
// This never get called.
done();
}, 1);
});
});
After sandbox.restore(), the stub is restored, but fakeTimers are not.
Sandbox should restore all fakes hooked on it, right?
or something wrong in my code?
Thanks.
I have maybe similar problem.
var date = new Date(2013, 1, 1)
var clock = sinon.useFakeTimers(date.getTime());
console.log('before restore' + new Date()) //2013-2-1
clock.restore()
console.log('before restore' + new Date()) //2013-2-1
The restore doesn't work for me. After restore it should be 2014-08-28 right?
@nhu313 I got different result as below
$cat test.js
var sinon = require('sinon');
var date = new Date(2013, 1, 1);
var clock = sinon.useFakeTimers(date.getTime());
console.log('before restore' + new Date()) //2013-2-1
clock.restore()
console.log('before restore' + new Date()) //2013-2-1
$node test.js
before restoreFri Feb 01 2013 00:00:00 GMT+0800 (CST)
before restoreFri Aug 29 2014 10:17:17 GMT+0800 (CST)
@mocheng Thanks for testing it out. Weird. I ran this many times, but I got the same result. I wonder if it's because my testing environment is not set up correctly. I guess we're not having the same problem then. Thanks for testing out mine :)
I think this issue has been open for a while, but I have a situation in which setTimeout is not restored.
describe('setTimeout restore', function () {
const original = window.setTimeout;
it('restores setTimeout', function () {
const sandbox = sinon.sandbox.create();
sandbox.useFakeTimers();
sandbox.spy(window, 'setTimeout');
sandbox.restore();
expect(original).to.equal(setTimeout);
});
});
I'm wondering what the correct behavior here is. Normally, sinon doesn't let you wrap something that is already wrapped. Should this produce an error (when you try to spy on it) or should it restore the timer?
I had the same issue. Please make sure you are not calling sinon.useFakeTimers() twice in a row, without restoring behavior to default
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
I had the same issue. Please make sure you are not calling
sinon.useFakeTimers()twice in a row, without restoring behavior to default