Upgrading our React apps to MSW 2.0, which came out at the end of October last year, broke every Jest test that uses it with two errors before a single handler ran, because JSDOM and MSW v2 don't agree on globals or on how to resolve packages. This is the MSW half of our test setup, next to the Module Federation part.
Why it breaks
MSW v2 is built on the standard Fetch API classes, Request, Response, Headers and friends, which Node has as globals, but jest-environment-jsdom gives each test file JSDOM's browser-like globals instead of Node's, and most of those classes aren't among them, so the first error is usually Request is not defined or TextEncoder is not defined.
The second one comes from how JSDOM resolves packages, since it uses the browser export condition by default, and MSW's msw/node entry has no browser build, so import { setupServer } from 'msw/node' fails with "Cannot find module 'msw/node'". That one is fixed with a single option from MSW's migration guide:
// jest.config.js testEnvironment: 'jsdom', testEnvironmentOptions: { customExportConditions: [''], }, setupFiles: ['<rootDir>/config/jest/polyfills.js'], The empty string makes Jest fall back to the package's default export condition, so msw/node resolves to the Node build, which is the one we want, since JSDOM is still Node underneath.
The polyfills file
The missing globals go into config/jest/polyfills.js, which started as the file from MSW's docs and still has its comment at the top:
/** * @note The block below contains polyfills for Node.js globals * required for Jest to function when running JSDOM tests. * These HAVE to be require's and HAVE to be in this exact * order, since "undici" depends on the "TextEncoder" global API. * * Consider migrating to a more modern test runner if * you don't want to deal with this. */ const { TextDecoder, TextEncoder } = require('node:util'); const { ReadableStream, TransformStream } = require('node:stream/web'); Object.defineProperties(globalThis, { TextDecoder: { value: TextDecoder }, TextEncoder: { value: TextEncoder }, ReadableStream: { value: ReadableStream }, TransformStream: { value: TransformStream }, }); const { Blob, File } = require('node:buffer'); const { fetch, Headers, FormData, Request, Response } = require('undici'); Object.defineProperties(globalThis, { fetch: { value: fetch, writable: true }, Blob: { value: Blob }, File: { value: File }, Headers: { value: Headers }, FormData: { value: FormData }, Request: { value: Request, writable: true, configurable: true }, Response: { value: Response, writable: true, configurable: true }, }); The order matters because undici creates a TextEncoder from the global as soon as its module loads, so require('undici') has to come after the first block, and it has to be require and not import, because imports are hoisted to the top of the file and would load undici before anything is defined.
Our version adds ReadableStream and TransformStream from node:stream/web to the docs' file, because undici 6 (we're on 6.20) uses both as globals too, new ReadableStream(...) for bodies and new TransformStream() in its fetch and request code, without importing either, and JSDOM doesn't provide them. It also makes Request and Response writable as well as configurable, where the docs only make them configurable.
The file goes in setupFiles, not setupFilesAfterEnv. setupFiles runs before the test framework and before any setup file that imports MSW, which is what the order above depends on.
The rest is the usual MSW setup, a server.ts with setupServer(...handlers), and listen(), resetHandlers() and close() in beforeAll, afterEach and afterAll of setupTests.ts.
Since the end of September, MSW's docs recommend a custom environment,
jest-fixed-jsdom, instead of this file. It's JSDOM with Node's ownfetch,Request,Response, streams and encoders put back, sotestEnvironment: 'jest-fixed-jsdom'replaces the polyfills file and the undici dependency (the export condition option is still a separate setting). For a new project that's less to maintain than the polyfills file.
JSDOM takes away the Node globals MSW v2 is built on, so they have to be put back with require, in a fixed order, from setupFiles, and the list of what to put back depends on the undici version, which is why our file has two more globals than the docs' one.