question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

msw does not play well with jests describe blocks

See original GitHub issue

Environment

Name Version
msw latest
node 14.6.0
OS Win10

Request handlers

https://github.com/ljosberinn/personal-react-boilerplate/blob/master/src/client/i18n.test.ts#L21

Actual request

https://github.com/ljosberinn/personal-react-boilerplate/blob/master/src/client/i18n.ts#L209

Current behavior

Trying out this eslint rule which is considered best practice, at least by that plugin, I moved all jest hooks including setupServer into describe blocks.

However, it appears msw just stops working then. Any fetch request will not be intercepted.

If this is a known limitation, it’d be great if the docs mention it; I know the documented usage is outside, but that might also just be because describe isn’t used in the docs at all.

Expected behavior

Continues working when setting up within describe.

Screenshots

I initially wrote the issue yesterday but accidentally didn’t submit so forgive me here if I add screenshots later.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
kettanaitocommented, Aug 7, 2020

In different test cases, I append the same route, with different responses to test different API outcomes. From what you just said this is technically discouraged too?

No, what you are doing is perfectly fine. As @marcosvega91 has mentioned, just make sure to reset the handlers between the tests with server.resetHandlers(), so the changes you make per-test do not leak into irrelevant test suites.

For posterity, I’m going to include the solution that worked for you.

Solution

  • Declare a single instance of server (ideally, in jest’s setupFilesAfterEnv configuration option).
  • Prepend necessary request handlers per test via server.use() API.
  • Don’t forget to reset the runtime state of request handlers with server.resetHandlers(), usually in the afterEach jest hook.
2reactions
kettanaitocommented, Jul 19, 2021

Hey, @brianzhou13. Thanks for checking in before creating an issue.

Yes, that statement still holds today, and the solution above is something I’d recommend for MSW users. We may look into this as a part of #474 in the future. I’ve also answered you in the discussion (#821) regarding how to approach the combination of the global MSW setup while still having the per-suite control over your API mocks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Mock Service Worker to Improve Jest Unit Tests - WWT
Mock Service Worker (MSW) improves unit tests of components that make API calls by defining mocks at the network level instead of mocking ......
Read more >
Frequently asked questions - Mock Service Worker Docs
Yes, but only for NodeJS integration. Since React Native does not execute in a browser environment, you cannot run a Service Worker alongside ......
Read more >
vscode-jest - Visual Studio Marketplace
It should work out-of-the-box for most common jest projects. ... trigger test runs via the gutter menu of each test and describe blocks....
Read more >
React Testing Library Cheat Sheet - Flexiple
Check out this easy-to-understand React Testing Library cheat sheet - your complete guide whenever you need it.
Read more >
A Comprehensive Guide to Mock Service Worker (MSW)
If you prefer to test MSW quickly in a single test file, you can do so as well. It is possible to use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found