Skip to content
Advertisement

Mock an axios 404 with Jest?

When mocking a 404 error the result is

Uncaught UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason “Error: error”.

Setup a basic Mock

JavaScript

Create a test case

JavaScript

How to mock the 404 request?

Advertisement

Answer

Using https://www.npmjs.com/package/axios-mock-adapter will make mocking API very simple.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement