Sinon useFakeTimers() 在 before/afterEach 中创建超时

Sinon useFakeTimers() creates a timeout in before/afterEach

我正在使用 Sinon 和 Mocha 来测试一些到期日期值。几个月前我使用了相同的代码并且工作正常,但是在 v1.12.x 和 v1.17.x 之间的某个地方,有些东西发生了变化,我似乎找不到正确的路径。

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() { ... }
});

最终结果总是一样的:

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

是否有我在文档中没有注意到的更改?那里有我看不到的错误吗?

如有任何想法,我们将不胜感激。

更新

所以这里有更多信息。这显然与我的代码有关,但我不知所措。

如果我评论每个实际测试,测试 运行 并给我一个绿色的“0 通过”。

如果我运行进行实际测试,即使是这样:

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

我马上回到暂停时间。我错过了什么?

您正在将 done 传递给第 2 行中的 describe 回调:

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

Mocha 希望您调用它...要消除超时错误,只需从回调中删除 done 参数。

Mystery solved。这似乎是 Sinon 与 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');

我的原始代码是在 Knex v0.7.6 是最新版本的世界中编写的。现在,即使代码本身是一样的,也不是一切都失败了。我使用了提到的修复程序,一切看起来都很好。