Skip to content

Commit 76f1686

Browse files
authored
jest-jsdom: use jest-fixed-jsdom (#124)
1 parent 10a00e7 commit 76f1686

File tree

6 files changed

+170
-65
lines changed

6 files changed

+170
-65
lines changed

examples/with-jest-jsdom/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ Using MSW with JSDOM requires additional configuration. Unfortunately, JSDOM (`j
1313

1414
Please see the setup steps below to properly configure Jest when using in combination with JSDOM.
1515

16-
### Module resolution
16+
## Custom Jest environment
1717

18-
Opt-out from the browser module resolution in JSDOM by setting the `testEnvironmentOptions.customExportConditions` option in [`jest.config.ts`](./jest.config.ts). This will force JSDOM to use Node.js module resolution, correctly resolving export conditions of third-party packages.
18+
Use the [`jest-fixed-jsdom`](https://github.com/mswjs/jest-fixed-jsdom) custom environment for your JSDOM tests. That environment is a superset of JSDOM with a few important modifications:
1919

20-
> Despite JSDOM predenting to be a browser environment, your code _still runs in Node.js_. Using the browser module resolution can cause all sorts of import issues with third-party packages that depend on the standard Node.js API.
20+
- Ensures the module resolution is set to Node.js, not the browser (`customExporConditions`);
21+
- Restores the global functions and classes present in the browser (e.g. `fetch`, `structuredClone`, etc.).
2122

22-
### Polyfills
23-
24-
Create a [jest.polyfills.ts](./jest.polyfills.ts) file in your project (you can copy it) and include it in the `setupFiles` option in `jest.config.ts`. This will re-add some of the Node.js globals (and browser) missing in JSDOM, like `fetch`, `Request`, `Response`, etc.
23+
See [`jest.config.ts`](./jest.config.ts) for the configuration reference.

examples/with-jest-jsdom/example.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @jest-environment jsdom
3-
*/
4-
51
it('receives a mocked response to a REST API request', async () => {
62
const response = await fetch('https://api.example.com/user')
73

examples/with-jest-jsdom/jest.config.ts

+8-18
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@ import type { Config } from 'jest'
22

33
export default {
44
rootDir: __dirname,
5-
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
6-
/**
7-
* @note Include the polyfills in the "setupFiles"
8-
* to apply them BEFORE the test environment.
9-
*/
10-
setupFiles: ['<rootDir>/jest.polyfills.ts'],
5+
6+
// Use a custom environment to fix missing globals in jsdom.
7+
testEnvironment: 'jest-fixed-jsdom',
8+
9+
// Provide a setup file to enable MSW.
10+
setupFilesAfterEnv: ['./jest.setup.ts'],
11+
12+
// (Optional) Add suppor for TypeScript in Jest.
1113
transform: {
1214
'^.+\\.tsx?$': '@swc/jest',
1315
},
14-
testEnvironmentOptions: {
15-
/**
16-
* @note Opt-out from JSDOM using browser-style resolution
17-
* for dependencies. This is simply incorrect, as JSDOM is
18-
* not a browser, and loading browser-oriented bundles in
19-
* Node.js will break things.
20-
*
21-
* Consider migrating to a more modern test runner if you
22-
* don't want to deal with this.
23-
*/
24-
customExportConditions: [''],
25-
},
2616
} satisfies Config

examples/with-jest-jsdom/jest.polyfills.ts

-28
This file was deleted.

examples/with-jest-jsdom/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"@types/node": "^18",
1111
"jest": "^29.5.0",
1212
"jest-environment-jsdom": "^29.5.0",
13-
"msw": "2.2.2",
13+
"jest-fixed-jsdom": "^0.0.9",
14+
"msw": "2.6.4",
1415
"ts-node": "^10.9.2",
15-
"typescript": "^5.0.4",
16-
"undici": "^5.22.0"
16+
"typescript": "^5.0.4"
1717
}
1818
}

pnpm-lock.yaml

+154-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)