function sum(a: number, b: number) {
return new Promise(function (resolve, reject) {
try {
throw Error()
resolve(a + b);
} catch (e) {
reject(-1);
}
})
}
test('The sum is error.', () => {
return expect(sum(1, 2)).rejects.toBe(-1);
});
テストそのものが非同期になっているため、async/awaitを組み合わせて書くこともできます。
test('The sum is Error.', async () => {
await expect(sum(1, 2)).rejects.toBe(-1);
await expect(eum(1, 2)).rejects.not.toBe(0);
});
参考
あわせて読みたい
Expect · JestWhen you’re writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of “matchers” that let you validate …
コメント