Skip to content

Mock Service Worker

Mock Service Worker, or MSW, lets tests use a made-up reply in place of a service’s real response.

Visit Mock Service Worker

How can you check a screen when a service fails?

Give your app a fictional reply. For a workshop schedule, return one class in one test and an error in another. Check that the first result shows the class and the second explains the problem.

The library handles the requests you describe. The test still has to check what the screen displays. A successful test proves how your app handled the fictional reply.

Where do the test requests go?

MSW supports browser and Node.js setups. A request without a matching reply can otherwise continue to its original destination. For a future run, confirm the app and data are disposable, remove live service credentials and prevent outside connections. Configure the test to fail on unexpected requests it handles.

Keep the response rules and fictional data with your tests. MSW is MIT-licensed. The browser and test runner are separate parts of your setup.

Which three results should your first test describe?

Draw a local app on paper with no connection to a service.

  1. Show a class
    Draw a successful reply with one made-up class and show where the class appears.
  2. Show a failure
    Draw an error reply and the message a reader should see.
  3. Mark an unexpected request
    Write that the test should report the unexpected request. Real request handling remains untested.