Rob Wilkerson
Rob Wilkerson

Reputation: 41236

Sinon useFakeTimers() creates a timeout in before/afterEach

I'm using Sinon with Mocha to test some expiration date values. I used the same code a few months ago and it worked fine, but somewhere between v1.12.x and v1.17.x, something has changed and I can't seem to find the right path.

let sinon = require('sinon');
describe('USER & AUTHENTICATION ENDPOINTS', function(done) {
    beforeEach(function() {
        this.clock = sinon.useFakeTimers(new Date().getTime());
        return fixtures.load(data);
    });
    afterEach(function() {
        this.clock.restore();
        return fixtures.clear(data);
    });

    context('POST /users', function() { ... }
});

The end result is always the same:

Error: timeout of 5000ms exceeded. Ensure the done() callback is being called in this test.

Has something changed that I just haven't noticed in the documentation? Do I have some kind of error in there that I can't see?

Any thoughts would be appreciated.

UPDATE

So a little more info here. This clearly has something to do with my code, but I'm at a loss.

If I comment every actual test, the tests run and give me a green "0 passing".

If I run an actual test, even one that just this:

context('POST /users', function() {
    it('should create a new user', function(done) {
        done();
    })
});

I'm right back to the timeout. What am I missing?

Upvotes: 0

Views: 2043

Answers (2)

Rob Wilkerson
Rob Wilkerson

Reputation: 41236

Mystery solved. It appears to be a conflict between Sinon and versions of Knex > 0.7.6.

Seems to be because pool2 relies on behavior of setTimeout. Using sinon.useFakeTimers(...) replaces several methods including setTimeout with synchronous versions which breaks it. Can fix by replacing with: clock = sinon.useFakeTimers(Number(date), 'Date');

My original code was written in a world where Knex v0.7.6 was the latest version. Now that it's not everything failed even though the code itself was the same. I used the fix mentioned and things look to be fine.

Upvotes: 3

Nicole
Nicole

Reputation: 1707

You are passing done to your describe callback in line 2:

describe('USER & AUTHENTICATION ENDPOINTS', function(done) {

Mocha expects you to invoke it... To get rid of the timeout error, just remove the done parameter from the callback.

Upvotes: 1

Related Questions