Sinon: UseFakeTimers breaks request when setImmediate method is enabled

Created on 21 May 2014  路  2Comments  路  Source: sinonjs/sinon

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.

Most helpful comment

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.

All 2 comments

:+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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ctaylo21 picture ctaylo21  路  18Comments

mantoni picture mantoni  路  32Comments

fhd picture fhd  路  23Comments

lavelle picture lavelle  路  31Comments

mroderick picture mroderick  路  19Comments