Skip to content

Commit 4d57381

Browse files
authored
Allow testMatch to take a string value (#15734)
1 parent 6c6edd8 commit 4d57381

File tree

7 files changed

+32
-10
lines changed

7 files changed

+32
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## main
22

3+
### Features
4+
5+
- `[jest-config]` Allow `testMatch` to take a string value
6+
37
### Fixes
48

59
- `[expect]` Fix `bigint` error ([#15702](https://github.com/jestjs/jest/pull/15702))

packages/jest-config/src/ValidConfig.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,13 @@ export const initialOptions: Config.InitialOptions = {
156156
},
157157
testFailureExitCode: 1,
158158
testLocationInResults: false,
159-
testMatch: [
160-
'**/__tests__/**/*.?([mc])[jt]s?(x)',
161-
'**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
162-
],
159+
testMatch: multipleValidOptions(
160+
'**/__tests__/**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
161+
[
162+
'**/__tests__/**/*.?([mc])[jt]s?(x)',
163+
'**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
164+
],
165+
),
163166
testNamePattern: 'test signature',
164167
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
165168
testRegex: multipleValidOptions(

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ describe('testMatch', () => {
954954
).rejects.toThrowErrorMatchingSnapshot();
955955
});
956956

957-
it('normalizes testMatch', async () => {
957+
it('normalizes testMatch root directory', async () => {
958958
const {options} = await normalize(
959959
{
960960
rootDir: '/root',
@@ -965,6 +965,18 @@ describe('testMatch', () => {
965965

966966
expect(options.testMatch).toEqual(['/root/**/*.js']);
967967
});
968+
969+
it('normalizes testMatch to array', async () => {
970+
const {options} = await normalize(
971+
{
972+
rootDir: '/root',
973+
testMatch: '**/*.js',
974+
},
975+
{} as Config.Argv,
976+
);
977+
978+
expect(options.testMatch).toEqual(['**/*.js']);
979+
});
968980
});
969981

970982
describe('moduleDirectories', () => {

packages/jest-config/src/normalize.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,12 @@ export default async function normalize(
769769
case 'moduleDirectories':
770770
case 'testMatch':
771771
{
772+
const option = oldOptions[key];
773+
const rawValue =
774+
Array.isArray(option) || option == null ? option : [option];
772775
const replacedRootDirTags = _replaceRootDirTags(
773776
escapeGlobCharacters(options.rootDir),
774-
oldOptions[key],
777+
rawValue,
775778
);
776779

777780
if (replacedRootDirTags) {

packages/jest-schemas/src/raw-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ export const InitialOptions = Type.Partial(
323323
testEnvironmentOptions: Type.Record(Type.String(), Type.Unknown()),
324324
testFailureExitCode: Type.Integer(),
325325
testLocationInResults: Type.Boolean(),
326-
testMatch: Type.Array(Type.String()),
326+
testMatch: Type.Union([Type.String(), Type.Array(Type.String())]),
327327
testNamePattern: Type.String(),
328328
testPathIgnorePatterns: Type.Array(Type.String()),
329329
testRegex: Type.Union([Type.String(), Type.Array(Type.String())]),

packages/jest-types/src/Config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ export type Argv = Arguments<
470470
testEnvironment: string;
471471
testEnvironmentOptions: string;
472472
testFailureExitCode: string | null | undefined;
473-
testMatch: Array<string>;
473+
testMatch: string | Array<string>;
474474
testNamePattern: string;
475475
testPathIgnorePatterns: Array<string>;
476476
testPathPatterns: Array<string>;

website/versioned_docs/version-30.0/Configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,7 +2045,7 @@ This does not change the exit code in the case of Jest errors (e.g. invalid conf
20452045

20462046
:::
20472047

2048-
### `testMatch` \[array&lt;string&gt;]
2048+
### `testMatch` \[string | array&lt;string&gt;]
20492049

20502050
(default: `[ "**/__tests__/**/*.?([mc])[jt]s?(x)", "**/?(*.)+(spec|test).?([mc])[jt]s?(x)" ]`)
20512051

@@ -2073,7 +2073,7 @@ These pattern strings match against the full path. Use the `<rootDir>` string to
20732073

20742074
Default: `(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$`
20752075

2076-
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.
2076+
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.
20772077

20782078
The following is a visualization of the default regex:
20792079

0 commit comments

Comments
 (0)