Testing · Middle · Question 18 of 75

How do you set up MSW for tests in Node.js?

const server = setupServer(...handlers);
beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

Explanation

onUnhandledRequest: "error" fails the test on any request without a handler, so nothing reaches a real network.

Further reading

Problem with a question

Testing · Middle · question 18

Test yourself

Take the Testing · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz