Skip to content

Commit 7e40893

Browse files
authored
fix(jest-config): parse testEnvironmentOptions if received from CLI (#11902)
1 parent 8024306 commit 7e40893

File tree

7 files changed

+18
-3
lines changed

7 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixes
88

9+
- `[jest-config]` Parse `testEnvironmentOptions` if received from CLI ([#11902](https://github.com/facebook/jest/pull/11902))
910
- `[jest-reporters]` Call `destroy` on `v8-to-istanbul` converters to free memory ([#11896](https://github.com/facebook/jest/pull/11896))
1011

1112
### Chore & Maintenance

docs/CLI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ Print your Jest config and then exits.
302302

303303
Prevent tests from printing messages through the console.
304304

305+
### `--testEnvironmentOptions=<json string>`
306+
307+
A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment.
308+
305309
### `--testNamePattern=<regex>`
306310

307311
Alias: `-t`. Run only tests with a name that matches the regex. For example, suppose you want to run only tests related to authorization which will have names like `"GET /api/posts with auth"`, then you can use `jest -t=auth`.

packages/jest-cli/src/cli/args.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,9 @@ export const options = {
549549
},
550550
testEnvironmentOptions: {
551551
description:
552-
'Test environment options that will be passed to the testEnvironment. ' +
552+
'A JSON string with options that will be passed to the `testEnvironment`. ' +
553553
'The relevant options depend on the environment.',
554-
type: 'string', // Object
554+
type: 'string',
555555
},
556556
testFailureExitCode: {
557557
description: 'Exit code of `jest` command if the test run failed',

packages/jest-config/src/__tests__/setFromArgv.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ test('works with string objects', () => {
4747
const argv = {
4848
moduleNameMapper:
4949
'{"types/(.*)": "<rootDir>/src/types/$1", "types2/(.*)": ["<rootDir>/src/types2/$1", "<rootDir>/src/types3/$1"]}',
50+
testEnvironmentOptions: '{"userAgent": "Agent/007"}',
5051
transform: '{"*.js": "<rootDir>/transformer"}',
5152
} as Config.Argv;
5253
expect(setFromArgv(options, argv)).toMatchObject({
5354
moduleNameMapper: {
5455
'types/(.*)': '<rootDir>/src/types/$1',
5556
'types2/(.*)': ['<rootDir>/src/types2/$1', '<rootDir>/src/types3/$1'],
5657
},
58+
testEnvironmentOptions: {
59+
userAgent: 'Agent/007',
60+
},
5761
transform: {
5862
'*.js': '<rootDir>/transformer',
5963
},

packages/jest-config/src/setFromArgv.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export default function setFromArgv(
3535
break;
3636
case 'coverageThreshold':
3737
case 'globals':
38+
case 'haste':
3839
case 'moduleNameMapper':
40+
case 'testEnvironmentOptions':
3941
case 'transform':
40-
case 'haste':
4142
const str = argv[key];
4243
if (isJSONString(str)) {
4344
options[key] = JSON.parse(str);

packages/jest-types/src/Config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ export type Argv = Arguments<
462462
silent: boolean;
463463
snapshotSerializers: Array<string>;
464464
testEnvironment: string;
465+
testEnvironmentOptions: string;
465466
testFailureExitCode: string | null | undefined;
466467
testMatch: Array<string>;
467468
testNamePattern: string;

website/versioned_docs/version-27.2/CLI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ Print your Jest config and then exits.
302302

303303
Prevent tests from printing messages through the console.
304304

305+
### `--testEnvironmentOptions=<json string>`
306+
307+
A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment.
308+
305309
### `--testNamePattern=<regex>`
306310

307311
Alias: `-t`. Run only tests with a name that matches the regex. For example, suppose you want to run only tests related to authorization which will have names like `"GET /api/posts with auth"`, then you can use `jest -t=auth`.

0 commit comments

Comments
 (0)