我想检查异步函数是否使用本机assert.throws模块中的assert抛出。我试过
const test = async () => await aPromise();
assert.throws(test); // AssertionError: Missing expected exception..(很明显?)无法工作,因为函数在承诺被解析之前就退出了。然而,我发现在这个问题中,使用回调实现了同样的目标。
有什么建议吗?
(我正在使用Babel转到Node.js本地生成器。)
发布于 2018-02-01 22:11:45
答案给了我工作,但我今天遇到了这个问题,并想出了另一个解决方案,我认为这是比较简单的。
// Code being tested
async function thisFunctionThrows() {
throw new Error('Bad response')
}
// In your test.
try {
await thisFunctionThrows()
assert.equal(1 == 0) // Never gets run. But if it does you know it didn't throw.
} catch (e) {
assert(e.message.includes('Bad response'))
}https://stackoverflow.com/questions/35782435
复制相似问题