Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseFakeTimers breaks request when setImmediate method is enabled #484

Closed
thabemmz opened this issue May 21, 2014 · 2 comments
Closed

UseFakeTimers breaks request when setImmediate method is enabled #484

thabemmz opened this issue May 21, 2014 · 2 comments

Comments

@thabemmz
Copy link

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.

@delwaterman
Copy link

👍 on issue. Ran into it this morning

@cjohansen
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants