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 Breaks Jest Mocks?!

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
cbookgcommented, Jan 12, 2022

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.

1reaction
LittleHendrixcommented, Oct 19, 2022

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 use msw extensively, this is a good tip for avoiding such issues in the future.

Read more comments on GitHub >

github_iconTop 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 >

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