MSW Breaks Jest Mocks?!
See original GitHub issueDescribe the bug
Currently we are transitioning our Typescript Jest test setup to testing-library and msw. Everything works and feels great (thank you!) so far, except that apparently msw seems to break our mocks.
As soon as we add to our jest-setup file:
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
Some(!), but not all, mocks are not working anymore. (It seems like that they’re not hoisted anymore?!). For example just console.logging (without using server) inside beforeAll works fine as well.
Probably that problem has nothing to do with msw, but nonetheless enabling the server seems to interfere with our system somehow.
Environment
msw: ^0.35.0
jest: ^27.2.4
,ts-jest: ^27.0.5
,whatwg-fetch: ^3.6.2
,nodejs: v14.17.6
typescript: ^4.4.3
,yarn2
To Reproduce
I was not able to reproduce it in a minimal example, sorry… which is also strange
Expected behavior
I want to keep using my jest.mocks when I enable msw
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to fix Jest and Mock Service Worker integration errors
In a recent project, my team decided to use Mock Service Worker (MSW) in our unit tests. It was meant to be a...
Read more >How to fix the Jest Test using Mock Service Worker?
Try using await screen.findByText('Hello World') instead. getByText is synchronous, while your "Hello World" text comes from an API call ...
Read more >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 >setupServer() - Api - Mock Service Worker Docs
Here's an example of the integration test using Jest and Mock Service Worker to provide a seamless API mocking: 1import { rest }...
Read more >Testing React Components with Testing Library and Mock ...
const mockedFetch = jest.fn(() => Promise.resolve({ json: () => Promise. ... MSW gives us the tools needed to avoid mocking the HTTP client...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok, so I figured it out and I’ll post here for any future readers.
The file containing my msw handlers was referencing a string constant imported from another file, and the chain led back to the file where the mocked import was not taking effect.
I think because the server is initialized in setupTests’s beforeAll(), it would cause my underlying module to load before the test was run, so before it would hoist the jest.mock.
@cbookg 🙏 Thank you for digging deeper and explaining what’s happened there. Was scratching my head over the failing
jest.mock
before I stumbled across this post. I moved the import to another location and problem went away! We usemsw
extensively, this is a good tip for avoiding such issues in the future.