Skip to content

Commit 5e5a9ba

Browse files
authored
feat: support test environment options in docblocks (#12470)
1 parent 0523bcc commit 5e5a9ba

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373))
2323
- `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392))
2424
- `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540))
25+
- `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470))
2526
- `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384))
2627
- `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343))
2728
- `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402))

docs/Configuration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,19 @@ Default: `{}`
11701170

11711171
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [`jsdom`](https://github.com/jsdom/jsdom) such as `{html: "<html lang="zh-cmn-Hant"></html>", url: 'https://jestjs.io/', userAgent: "Agent/007"}`.
11721172

1173+
These options can also be passed in a docblock, similar to `testEnvironment`. Note that it must be parseable by `JSON.parse`. Example:
1174+
1175+
```js
1176+
/**
1177+
* @jest-environment jsdom
1178+
* @jest-environment-options {"url": "https://jestjs.io/"}
1179+
*/
1180+
1181+
test('use jsdom and set the URL in this test file', () => {
1182+
expect(window.location.href).toBe('https://jestjs.io/');
1183+
});
1184+
```
1185+
11731186
### `testFailureExitCode` \[number]
11741187

11751188
Default: `1`

e2e/__tests__/testEnvironment.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ it('respects testEnvironment docblock', () => {
2323
const {json: result} = runWithJson('test-environment');
2424

2525
expect(result.success).toBe(true);
26-
expect(result.numTotalTests).toBe(3);
26+
expect(result.numTotalTests).toBe(4);
2727
});
2828

2929
it('handles missing `mocked` property', () => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment jsdom
8+
* @jest-environment-options {"url": "https://jestjs.io/"}
9+
*/
10+
'use strict';
11+
/*eslint-env browser */
12+
13+
test('use jsdom and set the URL in this test file', () => {
14+
expect(window.location.href).toBe('https://jestjs.io/');
15+
});

packages/jest-runner/src/runTest.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,27 @@ async function runTestInternal(
139139
testConsole = new BufferedConsole();
140140
}
141141

142+
let extraTestEnvironmentOptions;
143+
144+
const docblockEnvironmentOptions =
145+
docblockPragmas['jest-environment-options'];
146+
147+
if (typeof docblockEnvironmentOptions === 'string') {
148+
extraTestEnvironmentOptions = JSON.parse(docblockEnvironmentOptions);
149+
}
150+
142151
const environment = new TestEnvironment(
143152
{
144153
globalConfig,
145-
projectConfig,
154+
projectConfig: extraTestEnvironmentOptions
155+
? {
156+
...projectConfig,
157+
testEnvironmentOptions: {
158+
...projectConfig.testEnvironmentOptions,
159+
...extraTestEnvironmentOptions,
160+
},
161+
}
162+
: projectConfig,
146163
},
147164
{
148165
console: testConsole,

0 commit comments

Comments
 (0)