Skip to content

Commit 88ad6b5

Browse files
kmashintcpojer
authored andcommitted
Add testEnvironmentOptions to apply to jsdom options or node context. (#5003)
* Allow testEnvironmentOptions to be sent to jsdom options or node context. * Update pull-request#5003. * Fix integration_test show_config snapshot. * available in Jest **22 * Add testEnvironmentOptions to integration_tests. * Fix lint errors and environmentOptions. * Fix more lint errors. * Fix lint quoting, burned since all my other projects want double-quotes.
1 parent 6ade256 commit 88ad6b5

14 files changed

Lines changed: 69 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353

5454
### Features
5555

56+
* `[jest-config]` Add `testEnvironmentOptions` to apply to jsdom options or node context.
57+
([#5003](https://github.com/facebook/jest/pull/5003))
5658
* `[jest-jasmine2]` Update Timeout error message to `jest.timeout` and display
5759
current timeout value ([#4990](https://github.com/facebook/jest/pull/4990))
5860
* `[jest-runner]` Enable experimental detection of leaked contexts

docs/Configuration.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,17 @@ class CustomEnvironment extends NodeEnvironment {
726726
}
727727
```
728728

729+
### `testEnvironmentOptions` [Object]
730+
731+
##### available in Jest **22.0.0+**
732+
733+
Default: `{}`
734+
735+
Test environment options that will be passed to the `testEnvironment`. The
736+
relevant options depend on the environment. For example you can override
737+
options given to [jsdom](https://github.com/tmpvar/jsdom) such as
738+
`{userAgent: "Agent/007"}`.
739+
729740
### `testMatch` [array<string>]
730741

731742
##### available in Jest **19.0.0+**

integration_tests/__tests__/__snapshots__/show_config.test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
3939
\\"setupFiles\\": [],
4040
\\"snapshotSerializers\\": [],
4141
\\"testEnvironment\\": \\"jest-environment-jsdom\\",
42+
\\"testEnvironmentOptions\\": {},
4243
\\"testLocationInResults\\": false,
4344
\\"testMatch\\": [
4445
\\"**/__tests__/**/*.js?(x)\\",

integration_tests/__tests__/config.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,18 @@ test('config from argv is respected with sane config JSON', () => {
5959

6060
expect(stdout).toMatch('"watchman": false');
6161
});
62+
63+
test('works with jsdom testEnvironmentOptions config JSON', () => {
64+
const result = runJest('environmentOptions', [
65+
'--config=' +
66+
JSON.stringify({
67+
testEnvironmentOptions: {
68+
userAgent: 'Agent/007',
69+
},
70+
}),
71+
]);
72+
const stderr = result.stderr.toString();
73+
74+
expect(result.status).toBe(0);
75+
expect(stderr).toMatch('found userAgent Agent/007');
76+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. 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+
'use strict';
8+
/*global window */
9+
10+
test('found userAgent Agent/007', () => {
11+
expect(window.navigator.userAgent).toBe('Agent/007');
12+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "jsdom"
4+
}
5+
}

packages/jest-config/src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default ({
5656
runner: 'jest-runner',
5757
snapshotSerializers: [],
5858
testEnvironment: 'jest-environment-jsdom',
59+
testEnvironmentOptions: {},
5960
testFailureExitCode: 1,
6061
testLocationInResults: false,
6162
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],

packages/jest-config/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ const getConfigs = (
146146
skipNodeResolution: options.skipNodeResolution,
147147
snapshotSerializers: options.snapshotSerializers,
148148
testEnvironment: options.testEnvironment,
149+
testEnvironmentOptions: options.testEnvironmentOptions,
149150
testLocationInResults: options.testLocationInResults,
150151
testMatch: options.testMatch,
151152
testPathIgnorePatterns: options.testPathIgnorePatterns,

packages/jest-config/src/normalize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
480480
case 'silent':
481481
case 'skipNodeResolution':
482482
case 'testEnvironment':
483+
case 'testEnvironmentOptions':
483484
case 'testFailureExitCode':
484485
case 'testLocationInResults':
485486
case 'testNamePattern':

packages/jest-config/src/valid_config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export default ({
7777
skipNodeResolution: false,
7878
snapshotSerializers: ['my-serializer-module'],
7979
testEnvironment: 'jest-environment-jsdom',
80+
testEnvironmentOptions: {},
8081
testFailureExitCode: 1,
8182
testLocationInResults: false,
8283
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)'],

0 commit comments

Comments
 (0)