node.js - 在 Promise 解决之前 Mocha 测试失败
问题描述
我在 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(...)
以多种方式使用。文档在这里
一种方式是这样,但存在多个选项:套件/测试/挂钩级别...
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 时在命令行中使用参数:
mocha test --timeout 4000
推荐阅读
- qml - 在密码文本字段上,我想应用一个不隐藏显示密码的选项,供用户在 qml 中检查和确认
- angular - 将 debounceTime 添加到 HttpInterceptor
- python-3.x - 检查 Python 中是否存在带有 pathlib 的文件并返回假阴性
- javascript - 使用 React 从功能组件中的类组件访问 ref
- python-3.x - 如何使用也具有外键字段的外键字段进行更新
- android - 有没有办法查看 PC 的 Android 端到 Android 的连接?
- c++ - c ++如何在自定义容器中正确调用deallocate
- java - java.lang.IllegalArgumentException:无法在 Corda 中接收原始类型 [int]
- r - 使用 renderUI 在 R Shiny 中插入额外的 tabPanel
- javascript - 当对象在另一个对象内时,如何通过其 id 找到对象