I just wanted to test that a certain async call should throw an error and I tried it on Jest. I’m already familiar with RSpec which has similar syntax. Below is what I did.
it('should throw an error', async () => {
await expect(service.exec(params)).rejects.toThrow('Unable to create new issue.');
});
The exec method is an async function. It turns out we can capture the error by calling the rejects method to get the expected error. On the other hand, if we want to NOT throw an error, we can just call the method with the regular await clause.
That’s it!
Thanks! This was super helpful.