Module 39: Frontend Quality

Lesson 2 of 5 · 15–25 minutes

Testing with APIs

🏠The everyday picture

A flight simulator: pilots practise engine failures and storms safely, on demand, without a real plane or real weather.

📘What it really is

const server = setupServer(
  http.get("http://localhost:3001/enquiries", () => HttpResponse.json(sampleEnquiries)),
);

In a test, change it to simulate failure:

server.use(http.get("http://localhost:3001/enquiries", () => new HttpResponse(null, { status: 500 })));

🖱️Try it

Install MSW and write tests for the dashboard: shows enquiries from the API; shows the error message on a 500 and recovers after Try again; shows the empty state when the API returns [].

✅Quick check

Why use a pretend API in tests rather than a real server?

Show the answer

Tests stay fast and predictable, and failures can be simulated on demand.

When do you use findByText instead of getByText?

Show the answer

When the text appears after waiting, such as after data loads.

← PreviousComponent tests
ProLearners Assist