运行超时的摩卡测试永远不会结束



我在使用 Mocha 对我的node.js项目执行测试脚本时遇到困难。 问题是测试脚本永远不会完成,除非我手动完成它(ctrl + c(。这个问题是在我--timeout参数添加到mocha.opts后开始的。添加超时是因为我使用mock-mongoose库,其中建议的超时是120000 ms。 这是我的mocha.opts

--require ts-node/register
--watch-extensions ts
--timeout 120000
tests/**/*.ts

下面是代码示例:

it("POST '/route' should return OK", async () => {
const result = await routeController.createSomething(data)
expect(result.statusCode).is.equal(HttpStatus.OK)
})

我也尝试调用done()函数,但仍然没有帮助。 我的项目结构:

node_modules
src
--- source code...
tests
--- tests source...
package.json
package-lock.json
....

测试脚本:mocha --opts ./tests/mocha.opts

提前感谢!

您可以尝试使用--exit参数,这将允许摩卡自杀。

但这里的问题是你错过了清理代码中的某些内容。
在这里阅读 https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit

您还可以尝试 https://github.com/mafintosh/why-is-node-running 查明代码未终止的原因。

最新更新