msw does not play well with jests describe blocks
See original GitHub issueEnvironment
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:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
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
server
(ideally, in jest’ssetupFilesAfterEnv
configuration option).server.use()
API.server.resetHandlers()
, usually in theafterEach
jest hook.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.