【发布时间】:2021-09-15 08:36:26
【问题描述】:
我有一个函数,我希望它会抛出一个错误,但如果它没有抛出它,mocha 就通过了测试。
async function throwError() {
throw new Error("foo");
}
async function notThrowError() {
}
测试:
describe("bar", function(){
it("bar-2", async function() {
await expect(throwError()).throw //passes the test
})
}
describe("bar", function(){
it("bar-2", async function() {
await expect(notThrowError()).throw //passes the test, but no error has been thrown
})
}
【问题讨论】:
-
不应该是
.throw()吗? -
@SebastianSimon 这个问题对我没有帮助,也许是因为我的异步函数。
-
expect(…).throw是一个函数。await一个函数实际上并没有做任何事情。您需要调用该函数。你试过了吗? -
你的函数不会抛出一个错误——它是异步的,它返回一个拒绝的承诺。使用例如chaijs.com/plugins/chai-as-promised.
标签: javascript node.js mocha.js chai