Sandro Rey
Sandro Rey

Reputation: 2989

using Async/await in Jest

I have this test in Jest that throws an Exception and pass the test

await expect(hotelService.getByIdAsync(Identifier)).rejects.toThrow(FunctionalError);

but when I do

const action = async () => {
   await hotelService.getByIdAsync(Identifier);
};


expect(action).toThrowError(FunctionalError);

I have this result: Received function did not throw

Upvotes: 1

Views: 152

Answers (1)

eol
eol

Reputation: 24565

You forgot to actually call action, change it to:

it('should throw', async () => {
  await expect(action()).rejects.toThrowError(FunctionalError);
});

Upvotes: 2

Related Questions