You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1889,15 +1889,17 @@ More about serializers API can be found [here](https://github.com/jestjs/jest/tr
1889
1889
1890
1890
:::
1891
1891
1892
-
### `testEnvironment`\[string]
1892
+
### `testEnvironment`\[node | jsdom | string]
1893
1893
1894
-
Default: `"node"`
1894
+
Default: `node`
1895
1895
1896
1896
The test environment that will be used for testing. The default environment in Jest is a Node.js environment. If you are building a web app, you can use a browser-like environment through [`jsdom`](https://github.com/jsdom/jsdom) instead.
1897
1897
1898
1898
By adding a `@jest-environment` docblock at the top of the file, you can specify another environment to be used for all tests in that file:
1899
1899
1900
-
```js
1900
+
- With built-in environments:
1901
+
1902
+
```js tab title="my-test.spec.js"
1901
1903
/**
1902
1904
* @jest-environment jsdom
1903
1905
*/
@@ -1908,99 +1910,116 @@ test('use jsdom in this test file', () => {
1908
1910
});
1909
1911
```
1910
1912
1911
-
You can create your own module that will be used for setting up the test environment. The module must export a class with `setup`, `teardown` and `getVmContext` methods. You can also pass variables from this module to your test suites by assigning them to `this.global` object – this will make them available in your test suites as global variables. The constructor is passed [`globalConfig`](https://github.com/jestjs/jest/blob/v29.2.1/packages/jest-types/src/Config.ts#L358-L422) and [`projectConfig`](https://github.com/jestjs/jest/blob/v29.2.1/packages/jest-types/src/Config.ts#L424-L481) as its first argument, and [`testEnvironmentContext`](https://github.com/jestjs/jest/blob/491e7cb0f2daa8263caccc72d48bdce7ba759b11/packages/jest-environment/src/index.ts#L13) as its second.
1912
-
1913
-
The class may optionally expose an asynchronous `handleTestEvent` method to bind to events fired by [`jest-circus`](https://github.com/jestjs/jest/tree/main/packages/jest-circus). Normally, `jest-circus` test runner would pause until a promise returned from `handleTestEvent` gets fulfilled, **except for the next events**: `start_describe_definition`, `finish_describe_definition`, `add_hook`, `add_test` or `error` (for the up-to-date list you can look at [SyncEvent type in the types definitions](https://github.com/jestjs/jest/tree/main/packages/jest-types/src/Circus.ts)). That is caused by backward compatibility reasons and `process.on('unhandledRejection', callback)` signature, but that usually should not be a problem for most of the use cases.
1913
+
```ts tab title="my-test.spec.ts"
1914
+
/**
1915
+
* @jest-environment jsdom
1916
+
*/
1914
1917
1915
-
Any docblock pragmas in test files will be passed to the environment constructor and can be used for per-test configuration. If the pragma does not have a value, it will be present in the object with its value set to an empty string. If the pragma is not present, it will not be present in the object.
1918
+
test('use jsdom in this test file', () => {
1919
+
const element =document.createElement('div');
1920
+
expect(element).not.toBeNull();
1921
+
});
1922
+
```
1916
1923
1917
-
To use this class as your custom environment, refer to it by its full path within the project. For example, if your class is stored in `my-custom-environment.js` in some subfolder of your project, then the annotation might look like this:
You can also define custom environment. When non-builtin environment is used, Jest will try to load either the file path or the package name defined as value for `testEnvironment`. That file or the package should export an object with the shape of `JestEnvironment`:
Jest also exposes `builtinEnvironments` through `jest-environment-node` and `jest-environment-jsdom` packages, in case you just want to extend it. You can read more about extending environments in [our guide](TestEnvironment.md).
1992
+
1989
1993
### `testEnvironmentOptions`\[Object]
1990
1994
1991
1995
Default: `{}`
1992
1996
1993
-
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment.
1997
+
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment being used.
1998
+
1999
+
#### Node Environment Options
2000
+
2001
+
When using the `node` environment, you can configure various options that are passed to `runInContext`. These options include:
1994
2002
1995
-
For example, you can override options passed to [`jsdom`](https://github.com/jsdom/jsdom):
2003
+
-**`globalsCleanup`** (**'on'** | **'soft'** | **'off'**): Controls cleanup of global variables between tests. Default: `'soft'`.
2004
+
- All the options listed in the [vm.runInContext](https://nodejs.org/api/vm.html#scriptrunincontextcontextifiedobject-options) documentation
2005
+
2006
+
#### JSDOM Environment Options
2007
+
2008
+
When using the `jsdom` environment, you can configure various options that are passed to [jsdom](https://github.com/jsdom/jsdom). These options include:
2009
+
2010
+
-**`url`** - The URL of the page (affects `window.location` and relative URLs). Default: `"http://localhost"`
2011
+
-**`userAgent`** - The user agent string. Default: a generic user agent
2012
+
- All the options listed in the [jsdom](https://github.com/jsdom/jsdom)
2013
+
2014
+
For example, you can override options passed to `jsdom`:
1996
2015
1997
2016
```js tab title="jest.config.js"
1998
2017
const {defineConfig} =require('jest');
1999
2018
2000
2019
module.exports=defineConfig({
2001
2020
testEnvironment:'jsdom',
2002
2021
testEnvironmentOptions: {
2003
-
html:'<html lang="zh-cmn-Hant"></html>',
2022
+
html:'<html lang="en-US"></html>',
2004
2023
url:'https://jestjs.io/',
2005
2024
userAgent:'Agent/007',
2006
2025
},
@@ -2013,14 +2032,23 @@ import {defineConfig} from 'jest';
2013
2032
exportdefaultdefineConfig({
2014
2033
testEnvironment: 'jsdom',
2015
2034
testEnvironmentOptions: {
2016
-
html: '<html lang="zh-cmn-Hant"></html>',
2035
+
html: '<html lang="en-US"></html>',
2017
2036
url: 'https://jestjs.io/',
2018
2037
userAgent: 'Agent/007',
2019
2038
},
2020
2039
});
2021
2040
```
2022
2041
2023
-
Both `jest-environment-jsdom` and `jest-environment-node` allow specifying `customExportConditions`, which allow you to control which versions of a library are loaded from `exports` in `package.json`. `jest-environment-jsdom` defaults to `['browser']`. `jest-environment-node` defaults to `['node', 'node-addons']`.
2042
+
#### Custom Export Conditions
2043
+
2044
+
The `testEnvironmentOptions` allow specifying `customExportConditions`, which control which versions of a library are loaded from `exports` in `package.json`.
2045
+
2046
+
The built-in environments have the following defaults:
2047
+
2048
+
-`jest-environment-jsdom` defaults to `['browser']`
2049
+
-`jest-environment-node` defaults to `['node', 'node-addons']`
2050
+
2051
+
For example, you can override `customExportConditions` passed to `jsdom`:
2024
2052
2025
2053
```js tab title="jest.config.js"
2026
2054
const {defineConfig} =require('jest');
@@ -2057,6 +2085,20 @@ test('use jsdom and set the URL in this test file', () => {
0 commit comments