为什么我预期的拒绝不是使用 chai-as-promise 的 mocha 的成功案例?
Why is my expected rejection not a success case in mocha using chai-as-promise?
我想在 mocha 测试用例中断言承诺拒绝。因此我在打字稿中这样做:
import {
expect,
use,
} from "chai";
import * as chaiAsPromised from "chai-as-promised";
use(chaiAsPromised);
describe("Promise rejection", async () => {
it("should assert promise rejection", async () => {
const msg = "I AM THE EXPECTED ERROR";
const rejectedPromise = Promise.reject(msg);
return expect(rejectedPromise).to.eventually.throw(msg);
});
});
我希望我的测试用例能够成功,因为会抛出预期的错误。然而我的测试失败了:
Error: the string "I AM THE EXPECTED ERROR" was thrown, throw an Error :)
at <anonymous>
at runMicrotasksCallback (internal/process/next_tick.js:122:5)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
您想测试承诺 拒绝,而不是错误。因此使用 rejectedWith
:
return expect(rejectedPromise).to.eventually.be.rejectedWith(msg);
我想在 mocha 测试用例中断言承诺拒绝。因此我在打字稿中这样做:
import {
expect,
use,
} from "chai";
import * as chaiAsPromised from "chai-as-promised";
use(chaiAsPromised);
describe("Promise rejection", async () => {
it("should assert promise rejection", async () => {
const msg = "I AM THE EXPECTED ERROR";
const rejectedPromise = Promise.reject(msg);
return expect(rejectedPromise).to.eventually.throw(msg);
});
});
我希望我的测试用例能够成功,因为会抛出预期的错误。然而我的测试失败了:
Error: the string "I AM THE EXPECTED ERROR" was thrown, throw an Error :)
at <anonymous>
at runMicrotasksCallback (internal/process/next_tick.js:122:5)
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
您想测试承诺 拒绝,而不是错误。因此使用 rejectedWith
:
return expect(rejectedPromise).to.eventually.be.rejectedWith(msg);