Использование mocha и chai asyn c / стиль ожидания:
import {expect} from "chai";
const test1 = async () => {
throw new Error("I AM THE ERROR");
};
describe("My test case", async () => {
it("should assert", async () => {
try {
await test1();
expect(true, "promise should fail").eq(false)
} catch (e) {
expect(e.message).to.eq("I AM THE EXPECTED ERROR");
}
});
});
Использование chai-as-обещано :
import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const {expect} = chai;
const test1 = async () => {
throw new Error("I AM THE ERROR");
};
describe("My test case", async () => {
it("should assert", async () => {
await expect(test1()).to.eventually.be.rejectedWith("I AM THE EXPECTED ERROR");
});
});
Использование chai-as-promised
, вы также можете вернуть ожидаемое обещание:
it("should assert", async () => {
return expect(test1()).to.eventually.be.rejectedWith("I AM THE EXPECTED ERROR");
});
В каждом случае вы должны получить тестовую ошибку с указанием:
1) My test case
should assert:
AssertionError: expected promise to be rejected with an error including 'I AM THE EXPECTED ERROR' but got 'I AM THE ERROR'
actual expected
I AM THE EXPECTED ERROR