检测到多个呼叫 done()

优质
小牛编辑
122浏览
2023-12-01

如果使用基于回调的异步测试,如果done()多次调用Mocha,则会抛出错误。这对于捕获意外的双重回调非常方便。

it('double done', function(done) {
// Calling `done()` twice is an error
setImmediate(done);
setImmediate(done);
});

运行上面的测试将给出以下错误消息:

$ ./node_modules/.bin/mocha mocha.test.js
✓ double done
1) double done
1 passing (6ms)
1 failing
1) double done:
   Error: done() called multiple times
    at Object.<anonymous> (mocha.test.js:1:63)
    at require (internal/module.js:11:18)
    at Array.forEach (<anonymous>)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3