Got the following failing test case and i'm not sure why:
foo.js
async function throws() {
throw 'error';
}
async function foo() {
try {
await throws();
} catch(e) {
console.error(e);
throw e;
}
}
test.js
const foo = require('./foo');
describe('foo', () => {
it('should log and rethrow', async () => {
await expect(foo()).rejects.toThrow();
});
});
I expect foo to throw but for some reason it just resolves and the test fails:
FAILED foo › should log and rethrow - Received function did not throw
Live example
Probably missing some basic detail of async await throw behavior.
I think what you need is to check the rejected error
const foo = require('./foo');
describe('foo', () => {
it('should log and rethrow', async () => {
await expect(foo()).rejects.toEqual('error');
});
});
Seems like it's a known bug: https://github.com/facebook/jest/issues/1700
This works though:
describe('foo', () => {
it('should log and rethrow', async () => {
await expect(foo()).rejects.toEqual('error')
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With