diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f749e0b2cdc..eb17b7e359c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/CLI.md b/docs/CLI.md index 4479ccb8e180..dfc34da58456 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -302,6 +302,10 @@ Print your Jest config and then exits. Prevent tests from printing messages through the console. +### `--testEnvironmentOptions=` + +A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment. + ### `--testNamePattern=` 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`. diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 3e378b31d15c..26d2101e24f0 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -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', diff --git a/packages/jest-config/src/__tests__/setFromArgv.test.ts b/packages/jest-config/src/__tests__/setFromArgv.test.ts index 99d810f58e32..370b0e9cdda4 100644 --- a/packages/jest-config/src/__tests__/setFromArgv.test.ts +++ b/packages/jest-config/src/__tests__/setFromArgv.test.ts @@ -47,6 +47,7 @@ test('works with string objects', () => { const argv = { moduleNameMapper: '{"types/(.*)": "/src/types/$1", "types2/(.*)": ["/src/types2/$1", "/src/types3/$1"]}', + testEnvironmentOptions: '{"userAgent": "Agent/007"}', transform: '{"*.js": "/transformer"}', } as Config.Argv; expect(setFromArgv(options, argv)).toMatchObject({ @@ -54,6 +55,9 @@ test('works with string objects', () => { 'types/(.*)': '/src/types/$1', 'types2/(.*)': ['/src/types2/$1', '/src/types3/$1'], }, + testEnvironmentOptions: { + userAgent: 'Agent/007', + }, transform: { '*.js': '/transformer', }, diff --git a/packages/jest-config/src/setFromArgv.ts b/packages/jest-config/src/setFromArgv.ts index aedb9b3792f8..0a977d8f70b8 100644 --- a/packages/jest-config/src/setFromArgv.ts +++ b/packages/jest-config/src/setFromArgv.ts @@ -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)) { options[key] = JSON.parse(str); diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 79c89df32244..e2967dc9dacc 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -462,6 +462,7 @@ export type Argv = Arguments< silent: boolean; snapshotSerializers: Array; testEnvironment: string; + testEnvironmentOptions: string; testFailureExitCode: string | null | undefined; testMatch: Array; testNamePattern: string; diff --git a/website/versioned_docs/version-27.2/CLI.md b/website/versioned_docs/version-27.2/CLI.md index 4479ccb8e180..dfc34da58456 100644 --- a/website/versioned_docs/version-27.2/CLI.md +++ b/website/versioned_docs/version-27.2/CLI.md @@ -302,6 +302,10 @@ Print your Jest config and then exits. Prevent tests from printing messages through the console. +### `--testEnvironmentOptions=` + +A JSON string with options that will be passed to the `testEnvironment`. The relevant options depend on the environment. + ### `--testNamePattern=` 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`.