I have this function that returns a promise and fails when is called:
export const mockAsync = () => {
return new Promise((_, reject) => {
setTimeout(() => reject(new Error('Error')), 50);
});
};
And I have this test, which is passing:
describe('mockAsync', () => {
test('it should throw if we ask for it', async () => {
const result = mockAsync();
await expect(result).rejects.toThrow('Error');
});
});
But I find it weird, I would expect it to call await before the function call:
describe('mockAsync', () => {
test('it should throw if we ask for it', async () => {
const result = await mockAsync();
expect(result).rejects.toThrow('Error');
});
});
This last one is not passing, and I cant understand why. Is not the correct syntax?
await mockAsync(), the rejected promise throws as an error. That's nothing to do with Jest, it's how the syntax works.