We use the request npm module (https://github.com/mikeal/request) for doing requests, but it freezes when useFakeTimers uses the setImmediate method. Seems like faking the global setImmediate function is not done correctly by SinonJS.
The code that does not work (be aware: this is a stupid test, but just points out that the code freezes):
var sinon = require('sinon');
var libRequest = require('request');
var now = new Date.now();
var clock;
it('is a testing example!', function (done) {
clock = sinon.useFakeTimers(now);
libRequest.get({
url: 'http://www.example.com'
}, function() {
done();
});
});
The code freezes and never finishes, probably because the request module uses the setImmediate function somewhere in its code (https://github.com/mikeal/request/search?q=setImmediate&ref=cmdform).
The workaround is kind-a simple for our use-case, since we are only testing functionality that has to do with the Date object:
var sinon = require('sinon');
var libRequest = require('request');
var now = new Date.now();
var clock;
it('is a testing example!', function (done) {
clock = sinon.useFakeTimers(now, 'Date');
libRequest.get({
url: 'http://www.example.com'
}, function() {
done();
});
});
However, the issue is that faking the setImmediate function seems to break stuff, which needs to be resolved.
:+1: on issue. Ran into it this morning
You can specify which things to fake:
var clock = sinon.useFakeTimers("Date");
Will only fake the Date constructor.
var clock = sinon.useFakeTimers("Date", "setTimeout", "clearTimeout");
Fakes these three properties.
Most helpful comment
You can specify which things to fake:
Will only fake the
Dateconstructor.Fakes these three properties.