Skip to content

.tip

· one of the small things

Mock HTTP at the network, not the function that calls it

Mock fetchUser and you're testing that your code calls fetchUser. Mock the HTTP request and you're testing that your code handles the response, headers and 500 included.

server.use(http.get("/api/users/42", () => HttpResponse.json({ id: 42, name: "Priya" })));

Use msw for the browser and Node, or nock for Node only. The code under test runs unchanged, all the way down to the socket.

testing