mocha.js - 当 Promise 回调中的测试失败时,Mocha 超时
问题描述
如果我有以下模块:
module.exports = kontinue => {
Promise.resolve({error:null})
.then(o => {
console.log('promise resolved');
// say something goes wrong here
if(true)
kontinue({error:'promise resolved but something else went wrong'});
else kontinue(o);
})
.catch(error => {
console.log('caught error');
kontinue({error:'promise rejected, or resolved but then continuation threw exception'})
});
};
以及以下测试:
const assert = require('assert').strict;
const target = require('./the_above_code.js');
it('should not timeout', (done) => {
target((sut) => {
console.log('continuation called');
assert.ok(false); // the test for sut.error === what I expected was failing
done();
});
});
它输出:
promise resolved
continuation called
caught error
...
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
我意识到这是因为 .catch() 正在返回一个未解决的新承诺,但这不是我在测试期间真正想要的。
我如何测试一个承诺解决的对象,如果有必要使测试失败,让 Mocha 报告失败?
也许除了延续(在使用此模块的代码中永远不会返回)之外的其他地方我可以进行测试?
我确信 monad 可以减少这里的样板代码量,但使用它们肯定会违反Kernighan 的格言。
解决方案
推荐阅读
- javascript - 如何在反应js javascript中映射这个给定的api响应
- r - 二进制运算符错误的Sjplot非数字参数?
- sql - 查找数据集中每年可用的每个国家/地区的前 2 个供应商?- 大查询 SQL
- android - vivo杀死应用程序时不接收广播
- kubernetes - Tekton 仪表板部署错误-来自服务器的错误(禁止):创建“config/service.yaml”时出错
- android - Flutter 使用 Cloud Firestore 和 Firebase Auth 存储用户数据
- visual-studio-code - 如何在 VS Code 1.57 中禁用工作区信任?
- r - 如何使用 lapply 遍历 ggplot2 中的 2 个变量
- java - 在泛型中向下转换对象,但在其他任何地方都没有?
- docker - rust docker 映像中的 .cargo 文件夹在哪里?