Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-config): parse testEnvironmentOptions if received from CLI #11902

Merged
merged 10 commits into from
Sep 28, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Fixes

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

### Chore & Maintenance
Expand Down
4 changes: 4 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ Print your Jest config and then exits.

Prevent tests from printing messages through the console.

### `--testEnvironmentOptions=<json string>`

A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment.

### `--testNamePattern=<regex>`

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`.
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,9 @@ export const options = {
},
testEnvironmentOptions: {
description:
'Test environment options that will be passed to the testEnvironment. ' +
'A JSON string with options that will be passed to the `testEnvironment`. ' +
'The relevant options depend on the environment.',
type: 'string', // Object
type: 'string',
},
testFailureExitCode: {
description: 'Exit code of `jest` command if the test run failed',
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/__tests__/setFromArgv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ test('works with string objects', () => {
const argv = {
moduleNameMapper:
'{"types/(.*)": "<rootDir>/src/types/$1", "types2/(.*)": ["<rootDir>/src/types2/$1", "<rootDir>/src/types3/$1"]}',
testEnvironmentOptions: '{"userAgent": "Agent/007"}',
transform: '{"*.js": "<rootDir>/transformer"}',
} as Config.Argv;
expect(setFromArgv(options, argv)).toMatchObject({
moduleNameMapper: {
'types/(.*)': '<rootDir>/src/types/$1',
'types2/(.*)': ['<rootDir>/src/types2/$1', '<rootDir>/src/types3/$1'],
},
testEnvironmentOptions: {
userAgent: 'Agent/007',
},
transform: {
'*.js': '<rootDir>/transformer',
},
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-config/src/setFromArgv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export default function setFromArgv(
break;
case 'coverageThreshold':
case 'globals':
case 'haste':
case 'moduleNameMapper':
case 'testEnvironmentOptions':
case 'transform':
case 'haste':
const str = argv[key];
if (isJSONString(str)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should throw if it returns false... not for this PR anyways

options[key] = JSON.parse(str);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export type Argv = Arguments<
silent: boolean;
snapshotSerializers: Array<string>;
testEnvironment: string;
testEnvironmentOptions: string;
testFailureExitCode: string | null | undefined;
testMatch: Array<string>;
testNamePattern: string;
Expand Down
4 changes: 4 additions & 0 deletions website/versioned_docs/version-27.2/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ Print your Jest config and then exits.

Prevent tests from printing messages through the console.

### `--testEnvironmentOptions=<json string>`

A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment.

### `--testNamePattern=<regex>`

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`.
Expand Down