Allow `testMatch` to take a string value (#15734)

This commit is contained in:
Spenser Black 2025-07-13 19:38:22 -04:00 committed by GitHub
parent 6c6edd8949
commit 4d573817e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 32 additions and 10 deletions

View File

@ -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))

View File

@ -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(

View File

@ -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', () => {

View File

@ -769,9 +769,12 @@ export default async function normalize(
case 'moduleDirectories':
case 'testMatch':
{
const option = oldOptions[key];
const rawValue =
Array.isArray(option) || option == null ? option : [option];
const replacedRootDirTags = _replaceRootDirTags(
escapeGlobCharacters(options.rootDir),
oldOptions[key],
rawValue,
);
if (replacedRootDirTags) {

View File

@ -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())]),

View File

@ -470,7 +470,7 @@ export type Argv = Arguments<
testEnvironment: string;
testEnvironmentOptions: string;
testFailureExitCode: string | null | undefined;
testMatch: Array<string>;
testMatch: string | Array<string>;
testNamePattern: string;
testPathIgnorePatterns: Array<string>;
testPathPatterns: Array<string>;

View File

@ -2045,7 +2045,7 @@ This does not change the exit code in the case of Jest errors (e.g. invalid conf
:::
### `testMatch` \[array&lt;string&gt;]
### `testMatch` \[string | array&lt;string&gt;]
(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 `<rootDir>` 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&lt;string&gt;]](#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&lt;string&gt;]](#testmatch-arraystring), but note that you cannot specify both options.
The following is a visualization of the default regex: