testEnvironment
- Type:
'node' | 'jsdom' | 'happy-dom' | { name: 'node' | 'jsdom' | 'happy-dom'; options?: Record<string, any>; prebundle?: 'auto' | boolean } - Default:
'node' - CLI:
--testEnvironment=node
The runtime environment used for Node-mode test execution.
Rstest runs tests in Node.js by default. For web apps, set testEnvironment to 'jsdom' or 'happy-dom' instead.
DOM testing
Rstest supports jsdom and happy-dom for simulating DOM and browser APIs. Set testEnvironment to 'jsdom' or 'happy-dom' and install the corresponding package:
For jsdom
For happy-dom
After enabling DOM testing, you can write tests that use browser APIs like document and window.
Environment options
Pass options to configure the environment constructor directly (for example, url for jsdom):
The options object is passed directly to the environment's constructor.
When using a Node worker pool (forks, threads, vmForks, or vmThreads), the options must be structured-cloneable because they are sent to a worker. Function-valued options such as beforeParse are not supported in these pools and are rejected before a test file is dispatched.
On Bun, forks and vmForks use JSON IPC, so their options must also be JSON-compatible. Values such as Map, Set, Date, class instances, and BigInt are rejected because JSON IPC would change their shape or fail to serialize them.
- For
jsdom, it's passed to theJSDOMconstructor. You can find available options in the jsdom documentation. - For
happy-dom, it's passed to theWindowconstructor. You can find available options in the happy-dom documentation.
Environment prebundle
Rstest can prebundle a test environment before workers load it. This reduces repeated module resolution and initialization work when many test files use the same DOM environment. Since 0.12.0, this optimization is enabled by default in auto mode.
The prebundle option accepts:
'auto'(default): prebundle Rstest-tested versions of the built-injsdomandhappy-domenvironments. Unknown versions use native loading.true: always prebundle the selected built-in environment.false: disable the prebundle and load the environment natively.
The current automatic compatibility matrix covers jsdom 15–26 and 29–30, and happy-dom 20–21. Other major versions stay on the native path unless prebundle: true is set explicitly.
Troubleshooting the prebundle
The prebundle is optional. If your DOM environment behaves differently after enabling it, set prebundle: false to load it natively.
The prebundle is used only by the test environment. Imports of jsdom or happy-dom in your test code and dependencies follow the normal bundling and externalization rules; they do not share the environment's prebundle. DOM constructors from those imports can therefore differ from the test environment's constructors.
Rstest automatically falls back to native loading if it cannot build, load, or validate a prebundle. Validation covers known problem cases, not every API:
- Bundling can change how an environment resolves runtime assets, executable helpers, and optional or native dependencies, even if the bundle imports successfully.
- For example, jsdom 27 and 28 can resolve an optional CSS implementation differently after bundling, causing
getComputedStyle()to fail.autouses native loading for these versions. Withprebundle: true, Rstest probes this known case and falls back if validation fails.
The environment package is resolved from the project's dependencies first, then the Rstest workspace root, and finally through @rstest/core's native dependency resolution for backward compatibility.
Environment comments
You can override the environment for a single test file by adding an environment comment near the top of the file:
Use @rstest-environment-options to pass environment options for the annotated file. The options must be a single-line JSON object:
Rstest also recognizes @vitest-environment and @jest-environment aliases, including their -options variants, to make migration easier.
Environment comments support the built-in Node runner environments: node, jsdom, and happy-dom. They do not apply to browser mode. If most files use the same environment, prefer configuring testEnvironment or separate projects in rstest.config.ts.