为什么我的预期拒绝在使用chai-as-promise的摩卡咖啡中不是成功的案例?

时间:2019-02-20 16:54:47

标签: typescript promise mocha chai chai-as-promised

我想在一个摩卡测试案例中声明一个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)

1 个答案:

答案 0 :(得分:0)

您要测试是否拒绝了 ,而不是错误。因此,请使用rejectedWith

return expect(rejectedPromise).to.eventually.be.rejectedWith(msg);