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
@@ -1837,15 +1837,17 @@ More about serializers API can be found [here](https://github.com/jestjs/jest/tr
1837
1837
1838
1838
:::
1839
1839
1840
-
### `testEnvironment`\[string]
1840
+
### `testEnvironment`\[node | jsdom | string]
1841
1841
1842
-
Default: `"node"`
1842
+
Default: `node`
1843
1843
1844
1844
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.
1845
1845
1846
1846
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:
1847
1847
1848
-
```js
1848
+
- With built-in environments:
1849
+
1850
+
```js tab title="my-test.spec.js"
1849
1851
/**
1850
1852
* @jest-environment jsdom
1851
1853
*/
@@ -1856,99 +1858,116 @@ test('use jsdom in this test file', () => {
1856
1858
});
1857
1859
```
1858
1860
1859
-
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.
1860
-
1861
-
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.
1861
+
```ts tab title="my-test.spec.ts"
1862
+
/**
1863
+
* @jest-environment jsdom
1864
+
*/
1862
1865
1863
-
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.
1866
+
test('use jsdom in this test file', () => {
1867
+
const element =document.createElement('div');
1868
+
expect(element).not.toBeNull();
1869
+
});
1870
+
```
1864
1871
1865
-
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).
1940
+
1937
1941
### `testEnvironmentOptions`\[Object]
1938
1942
1939
1943
Default: `{}`
1940
1944
1941
-
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment.
1945
+
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment being used.
1946
+
1947
+
#### Node Environment Options
1948
+
1949
+
When using the `node` environment, you can configure various options that are passed to `runInContext`. These options include:
1942
1950
1943
-
For example, you can override options passed to [`jsdom`](https://github.com/jsdom/jsdom):
1951
+
-**`globalsCleanup`** (**'on'** | **'soft'** | **'off'**): Controls cleanup of global variables between tests. Default: `'soft'`.
1952
+
- All the options listed in the [vm.runInContext](https://nodejs.org/api/vm.html#scriptrunincontextcontextifiedobject-options) documentation
1953
+
1954
+
#### JSDOM Environment Options
1955
+
1956
+
When using the `jsdom` environment, you can configure various options that are passed to [jsdom](https://github.com/jsdom/jsdom). These options include:
1957
+
1958
+
-**`url`** - The URL of the page (affects `window.location` and relative URLs). Default: `"http://localhost"`
1959
+
-**`userAgent`** - The user agent string. Default: a generic user agent
1960
+
- All the options listed in the [jsdom](https://github.com/jsdom/jsdom)
1961
+
1962
+
For example, you can override options passed to `jsdom`:
1944
1963
1945
1964
```js tab title="jest.config.js"
1946
1965
const {defineConfig} =require('jest');
1947
1966
1948
1967
module.exports=defineConfig({
1949
1968
testEnvironment:'jsdom',
1950
1969
testEnvironmentOptions: {
1951
-
html:'<html lang="zh-cmn-Hant"></html>',
1970
+
html:'<html lang="en-US"></html>',
1952
1971
url:'https://jestjs.io/',
1953
1972
userAgent:'Agent/007',
1954
1973
},
@@ -1961,14 +1980,23 @@ import {defineConfig} from 'jest';
1961
1980
exportdefaultdefineConfig({
1962
1981
testEnvironment: 'jsdom',
1963
1982
testEnvironmentOptions: {
1964
-
html: '<html lang="zh-cmn-Hant"></html>',
1983
+
html: '<html lang="en-US"></html>',
1965
1984
url: 'https://jestjs.io/',
1966
1985
userAgent: 'Agent/007',
1967
1986
},
1968
1987
});
1969
1988
```
1970
1989
1971
-
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']`.
1990
+
#### Custom Export Conditions
1991
+
1992
+
The `testEnvironmentOptions` allow specifying `customExportConditions`, which control which versions of a library are loaded from `exports` in `package.json`.
1993
+
1994
+
The built-in environments have the following defaults:
1995
+
1996
+
-`jest-environment-jsdom` defaults to `['browser']`
1997
+
-`jest-environment-node` defaults to `['node', 'node-addons']`
1998
+
1999
+
For example, you can override `customExportConditions` passed to `jsdom`:
1972
2000
1973
2001
```js tab title="jest.config.js"
1974
2002
const {defineConfig} =require('jest');
@@ -2005,6 +2033,20 @@ test('use jsdom and set the URL in this test file', () => {
0 commit comments