我想在一个摩卡测试案例中声明一个Promise拒绝。因此,我在打字稿中这样做:
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)
答案 0 :(得分:0)
您要测试是否拒绝了 ,而不是错误。因此,请使用rejectedWith
:
return expect(rejectedPromise).to.eventually.be.rejectedWith(msg);