From 1a4537505b76d2352ed2b638428b282800d1cffb Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Thu, 10 Jul 2025 18:11:31 -0400 Subject: [PATCH 1/3] Allow `testMatch` to take a string value --- CHANGELOG.md | 4 ++++ packages/jest-config/src/ValidConfig.ts | 11 +++++++---- .../jest-config/src/__tests__/normalize.test.ts | 14 +++++++++++++- packages/jest-config/src/normalize.ts | 4 +++- packages/jest-schemas/src/raw-types.ts | 2 +- packages/jest-types/src/Config.ts | 2 +- .../versioned_docs/version-30.0/Configuration.md | 4 ++-- 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f69c2ac2754a..31652ea233da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## main +### Features + +- `[jest-config]` Allow `testMatch` to take a string value + ### Fixes - `[expect]` Fix `bigint` error ([#15702](https://github.com/jestjs/jest/pull/15702)) diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index 135e19778198..f40c1c9247d1 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -156,10 +156,13 @@ export const initialOptions: Config.InitialOptions = { }, testFailureExitCode: 1, testLocationInResults: false, - testMatch: [ - '**/__tests__/**/*.?([mc])[jt]s?(x)', - '**/?(*.)+(spec|test).?([mc])[jt]s?(x)', - ], + testMatch: multipleValidOptions( + '**/__tests__/**/?(*.)+(spec|test).?([mc])[jt]s?(x)', + [ + '**/__tests__/**/*.?([mc])[jt]s?(x)', + '**/?(*.)+(spec|test).?([mc])[jt]s?(x)', + ], + ), testNamePattern: 'test signature', testPathIgnorePatterns: [NODE_MODULES_REGEXP], testRegex: multipleValidOptions( diff --git a/packages/jest-config/src/__tests__/normalize.test.ts b/packages/jest-config/src/__tests__/normalize.test.ts index f978c31b9466..3939302c890a 100644 --- a/packages/jest-config/src/__tests__/normalize.test.ts +++ b/packages/jest-config/src/__tests__/normalize.test.ts @@ -954,7 +954,7 @@ describe('testMatch', () => { ).rejects.toThrowErrorMatchingSnapshot(); }); - it('normalizes testMatch', async () => { + it('normalizes testMatch root directory', async () => { const {options} = await normalize( { rootDir: '/root', @@ -965,6 +965,18 @@ describe('testMatch', () => { expect(options.testMatch).toEqual(['/root/**/*.js']); }); + + it('normalizes testMatch to array', async () => { + const {options} = await normalize( + { + rootDir: '/root', + testMatch: '**/*.js', + }, + {} as Config.Argv, + ); + + expect(options.testMatch).toEqual(['**/*.js']); + }); }); describe('moduleDirectories', () => { diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 2b0fa06e348a..ccc3a568760b 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -769,9 +769,11 @@ export default async function normalize( case 'moduleDirectories': case 'testMatch': { + const option = oldOptions[key]; + const rawValue = Array.isArray(option) ? option : [option]; const replacedRootDirTags = _replaceRootDirTags( escapeGlobCharacters(options.rootDir), - oldOptions[key], + rawValue, ); if (replacedRootDirTags) { diff --git a/packages/jest-schemas/src/raw-types.ts b/packages/jest-schemas/src/raw-types.ts index dc1de9b2b1b6..e1b034485da1 100644 --- a/packages/jest-schemas/src/raw-types.ts +++ b/packages/jest-schemas/src/raw-types.ts @@ -323,7 +323,7 @@ export const InitialOptions = Type.Partial( testEnvironmentOptions: Type.Record(Type.String(), Type.Unknown()), testFailureExitCode: Type.Integer(), testLocationInResults: Type.Boolean(), - testMatch: Type.Array(Type.String()), + testMatch: Type.Union([Type.String(), Type.Array(Type.String())]), testNamePattern: Type.String(), testPathIgnorePatterns: Type.Array(Type.String()), testRegex: Type.Union([Type.String(), Type.Array(Type.String())]), diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 6591b6f5f3de..1a0a9d0b64ff 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -470,7 +470,7 @@ export type Argv = Arguments< testEnvironment: string; testEnvironmentOptions: string; testFailureExitCode: string | null | undefined; - testMatch: Array; + testMatch: string | Array; testNamePattern: string; testPathIgnorePatterns: Array; testPathPatterns: Array; diff --git a/website/versioned_docs/version-30.0/Configuration.md b/website/versioned_docs/version-30.0/Configuration.md index 14904fe06b1d..bf189e7c99cb 100644 --- a/website/versioned_docs/version-30.0/Configuration.md +++ b/website/versioned_docs/version-30.0/Configuration.md @@ -2045,7 +2045,7 @@ This does not change the exit code in the case of Jest errors (e.g. invalid conf ::: -### `testMatch` \[array<string>] +### `testMatch` \[string | array<string>] (default: `[ "**/__tests__/**/*.?([mc])[jt]s?(x)", "**/?(*.)+(spec|test).?([mc])[jt]s?(x)" ]`) @@ -2073,7 +2073,7 @@ These pattern strings match against the full path. Use the `` string to Default: `(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$` -The pattern or patterns Jest uses to detect test files. By default it looks for `.js`, `.jsx`, `.ts` and `.tsx` files inside of `__tests__` folders, as well as any files with a suffix of `.test` or `.spec` (e.g. `Component.test.js` or `Component.spec.js`). It will also find files called `test.js` or `spec.js`. See also [`testMatch` [array<string>]](#testmatch-arraystring), but note that you cannot specify both options. +The pattern or patterns Jest uses to detect test files. By default it looks for `.js`, `.jsx`, `.ts` and `.tsx` files inside of `__tests__` folders, as well as any files with a suffix of `.test` or `.spec` (e.g. `Component.test.js` or `Component.spec.js`). It will also find files called `test.js` or `spec.js`. See also [`testMatch` [string | array<string>]](#testmatch-arraystring), but note that you cannot specify both options. The following is a visualization of the default regex: From 11b7d4b7d1f4e4f7d11ce35fc93ef1b907a0d5c2 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 11 Jul 2025 02:32:30 +0000 Subject: [PATCH 2/3] Handle undefined option --- packages/jest-config/src/normalize.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index ccc3a568760b..bd3e641150c9 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -770,7 +770,7 @@ export default async function normalize( case 'testMatch': { const option = oldOptions[key]; - const rawValue = Array.isArray(option) ? option : [option]; + const rawValue = Array.isArray(option) || typeof option === "undefined" ? option : [option]; const replacedRootDirTags = _replaceRootDirTags( escapeGlobCharacters(options.rootDir), rawValue, From 13d5d3b37b7c8dc56b3287ae2498708f942b3622 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Fri, 11 Jul 2025 02:36:59 +0000 Subject: [PATCH 3/3] Address lint issues --- packages/jest-config/src/normalize.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index bd3e641150c9..e2ed840cf020 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -770,7 +770,8 @@ export default async function normalize( case 'testMatch': { const option = oldOptions[key]; - const rawValue = Array.isArray(option) || typeof option === "undefined" ? option : [option]; + const rawValue = + Array.isArray(option) || option == null ? option : [option]; const replacedRootDirTags = _replaceRootDirTags( escapeGlobCharacters(options.rootDir), rawValue,