DEV Community

Discussion on: Why isn't this unit test catching an error from this async/await function?

Collapse
 
ccleary00 profile image
Corey Cleary

Unfortunately this doesn't work and just leads to the same issue. We're expecting a rejected promise with an error, not just an error.

Collapse
 
szpadel profile image
Szpadel

Await unwraps Promise and maps promise reject to throw, so you can catch it in try..catch so you should get there thrown error.

After second thought exception in this case is thrown before passing it to expect.
Better solution would be to wrap it in try catch block and test error that is caught.

You really want to make sure what error is caught, because you might get also other issue, like TypeError, and test will pass