超时运行Mocha测试永远不会结束

时间:2019-12-07 21:23:10

标签: javascript node.js unit-testing testing mocha

使用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

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

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