CodePark/ questions / Mocking the network (MSW)
START PRACTICINGPRACTICE
Frontend question · Testing › Mocking the network (MSW)

One test calls server.use(...) to make /api/user return 500. The next test unexpectedly also gets a 500. What's missing?

afterEach(() => server.resetHandlers())

afterEach(() => server.resetHandlers()) is missing. server.use adds runtime handlers in front of the others, and they stay until you reset them. resetHandlers() removes them and goes back to the initial handlers. The tempting server.close() option is wrong. listen and close belong in beforeAll and afterAll. clearAllMocks is wrong because it doesn't touch MSW.