得到以下失败的测试用例,我不知道为什么:
foo.js
async function throws() {
throw 'error';
}
async function foo() {
try {
await throws();
} catch(e) {
console.error(e);
throw e;
}
}
test.js
const foo = require('./foo');
describe('foo', () => {
it('should log and rethrow', async () => {
await expect(foo()).rejects.toThrow();
});
});
我希望 foo 抛出异常,但由于某种原因它只是解析并且测试失败:
FAILED foo › 应该记录并重新抛出 - 接收到的函数没有抛出
可能缺少异步等待抛出行为的一些基本细节。
最佳答案
我认为你需要的是检查被拒绝的错误
const foo = require('./foo');
describe('foo', () => {
it('should log and rethrow', async () => {
await expect(foo()).rejects.toEqual('error');
});
});
关于javascript - Jest - 断言异步函数引发测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873984/