testEnvironment
- Type:
'node' | 'jsdom' | 'happy-dom' | { name: EnvironmentName, options?: EnvironmentOptions } - Default:
'node' - CLI:
--testEnvironment=node
The environment that will be used for testing.
The default environment in Rstest is a Node.js environment. If you are building a web application, you can use a browser-like environment through jsdom or happy-dom instead.
DOM testing
Rstest supports jsdom and happy-dom for mocking DOM and browser APIs.
If you want to enable DOM testing, you can use the following configuration:
rstest.config.ts
You also need to 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
You can also pass options to the test environment. This is useful for configuring jsdom or happy-dom. For example, you can set the url for jsdom:
rstest.config.ts
The options object is passed directly to the environment's constructor.
- 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.