如何解决Mocha 测试在 Promise 解决之前失败
我在 NodeJS 中有一个 Mocha 测试:
it('Test',async () => {
this.party = new Party('example_id');
await this.party.startWithPlaylist('3e8JNsQmYEXtfV7K1M0pAb');
assert.isTrue(this.party.getStreamingProvider().getAuth().getToken() !== undefined);
})
this.party.startWithPlaylist
是:
startWithPlaylist(id) {
return new Promise(async (resolve,reject) => {
assert.ok(id !== undefined);
await this.start();
let playlist = await this.songInfoProvider.getPlaylist(id);
resolve();
});
}
代码工作正常,但我的测试没有。开始测试后 2 秒我收到错误:
Error: Timeout of 2000ms exceeded. For async tests and hooks,ensure "done()" is called; if returning a Promise,ensure it resolves.
发生错误后 startWithPlaylist
正确完成,但似乎没有及时进行我的测试。
我浏览了 Stackoverflow 并发现了类似的问题,但没有一个可以接受的答案或任何其他对我有用的提示。我已经尝试将测试从 async
更改为仅等待使用 .then
解决的承诺,但我的尝试均未成功。
我真的很感激任何帮助!提前致谢!
解决方法
这里的问题是函数需要执行的时间高于提供的超时时间。
您可以通过多种方式使用 this.timeout(...)
。文档 here
一种方式是这样,但存在多个选项:套件/测试/钩子级别...
it('Test',async () => {
this.party = new Party('example_id');
await this.party.startWithPlaylist('3e8JNsQmYEXtfV7K1M0pAb');
assert.isTrue(this.party.getStreamingProvider().getAuth().getToken() !== undefined);
}).timeout(4000)
或在以这种方式运行 mocha 时使用 parameters in command line:
mocha test --timeout 4000
,
你真的定义了 arg.运行时“id”?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。