Skip to content

Commit

Permalink
Add mts and cts to test match and test regex (#14584)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinm authored Oct 2, 2023
1 parent 85278ee commit a889046
Show file tree
Hide file tree
Showing 28 changed files with 228 additions and 19 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ module.exports = {
'e2e/failures/macros.js',
'e2e/test-in-root/*.js',
'e2e/test-match/test-suites/*',
'e2e/test-match-default/dot-spec-tests/*',
'packages/test-utils/src/ConditionalTest.ts',
],
env: {'jest/globals': true},
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- `[jest-config]` [**BREAKING**] Add `mts` and `cts` to default `moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369))
- `[jest-config]` [**BREAKING**] Update `testMatch` and `testRegex` default option for supporting `mjs`, `cjs`, `mts`, and `cts` ([#14584](https://github.com/jestjs/jest/pull/14584))
- `[@jest/core]` [**BREAKING**] Group together open handles with the same stack trace ([#13417](https://github.com/jestjs/jest/pull/13417), & [#14543](https://github.com/jestjs/jest/pull/14543))
- `[@jest/core, @jest/test-sequencer]` [**BREAKING**] Exposes `globalConfig` & `contexts` to `TestSequencer` ([#14535](https://github.com/jestjs/jest/pull/14535), & [#14543](https://github.com/jestjs/jest/pull/14543))
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade JSDOM to v22 ([#13825](https://github.com/jestjs/jest/pull/13825))
Expand Down
4 changes: 2 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ This does not change the exit code in the case of Jest errors (e.g. invalid conf

### `testMatch` \[array<string>]

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

The glob 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`.

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

### `testRegex` \[string | array&lt;string&gt;]

Default: `(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$`
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.

Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
"testEnvironmentOptions": {},
"testLocationInResults": false,
"testMatch": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)"
"**/__tests__/**/*.?([mc])[jt]s?(x)",
"**/?(*.)+(spec|test).?([mc])[jt]s?(x)"
],
"testPathIgnorePatterns": [
"/node_modules/"
Expand Down
9 changes: 9 additions & 0 deletions e2e/__tests__/__snapshots__/testMatchDefault.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`testMatch should able to match file with \`?([mc])[jt]s?(x)\` by default 1`] = `
"Test Suites: 16 passed, 16 total
Tests: 16 passed, 16 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
16 changes: 16 additions & 0 deletions e2e/__tests__/testMatchDefault.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {extractSummary} from '../Utils';
import runJest from '../runJest';

it('testMatch should able to match file with `?([mc])[jt]s?(x)` by default', () => {
const result = runJest('test-match-default');
expect(result.exitCode).toBe(0);
const {summary} = extractSummary(result.stderr);
expect(summary).toMatchSnapshot();
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('cjs extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('cts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('js extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('jsx extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('mjs extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('mts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('ts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/__tests__/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('tsx extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('cjs extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('cts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('js extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('jsx extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('mjs extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('mts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('ts extension', () => {
expect(1).toBe(1);
});
10 changes: 10 additions & 0 deletions e2e/test-match-default/dot-spec-tests/test.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

test('tsx extension', () => {
expect(1).toBe(1);
});
3 changes: 3 additions & 0 deletions e2e/test-match-default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"jest": {}
}
4 changes: 4 additions & 0 deletions e2e/test-match-default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["./**/*.tsx"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ const config: Config = {
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// "**/__tests__/**/*.?([mc])[jt]s?(x)",
// "**/?(*.)+(spec|test).?([mc])[jt]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
Expand Down Expand Up @@ -480,8 +480,8 @@ const config = {
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// "**/__tests__/**/*.?([mc])[jt]s?(x)",
// "**/?(*.)+(spec|test).?([mc])[jt]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
Expand Down Expand Up @@ -684,8 +684,8 @@ const config = {
// The glob patterns Jest uses to detect test files
// testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
// "**/?(*.)+(spec|test).[tj]s?(x)"
// "**/__tests__/**/*.?([mc])[jt]s?(x)",
// "**/?(*.)+(spec|test).?([mc])[jt]s?(x)"
// ],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-config/src/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const defaultOptions: Config.DefaultOptions = {
testEnvironmentOptions: {},
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
testMatch: [
'**/__tests__/**/*.?([mc])[jt]s?(x)',
'**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
],
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: [],
testRunner: 'jest-circus/runner',
Expand Down
24 changes: 18 additions & 6 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,18 @@ export const initialOptions: Config.InitialOptions = {
},
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
testMatch: [
'**/__tests__/**/*.?([mc])[jt]s?(x)',
'**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
],
testNamePattern: 'test signature',
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: multipleValidOptions(
'(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
['/__tests__/\\.test\\.[jt]sx?$', '/__tests__/\\.spec\\.[jt]sx?$'],
'(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$',
[
'/__tests__/\\.test\\.[mc]?[jt]sx?$',
'/__tests__/\\.spec\\.[mc]?[jt]sx?$',
],
),
testResultsProcessor: 'processor-node-module',
testRunner: 'circus',
Expand Down Expand Up @@ -296,11 +302,17 @@ export const initialProjectOptions: Config.InitialProjectOptions = {
userAgent: 'Agent/007',
},
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
testMatch: [
'**/__tests__/**/*.?([mc])[jt]s?(x)',
'**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
],
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: multipleValidOptions(
'(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
['/__tests__/\\.test\\.[jt]sx?$', '/__tests__/\\.spec\\.[jt]sx?$'],
'(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$',
[
'/__tests__/\\.test\\.[mc]?[jt]sx?$',
'/__tests__/\\.spec\\.[mc]?[jt]sx?$',
],
),
testRunner: 'circus',
transform: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const defaultConfig = {
snapshotSerializers: [],
testEnvironment: 'jest-environment-node',
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$',
testResultsProcessor: null,
transformIgnorePatterns: [NODE_MODULES_REGEXP],
useStderr: false,
Expand Down Expand Up @@ -104,7 +104,7 @@ export const validConfig = {
testEnvironment: 'jest-environment-node',
testNamePattern: 'test signature',
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[mc]?[jt]sx?$',
testResultsProcessor: 'processor-node-module',
testRunner: 'circus',
transform: {
Expand Down

0 comments on commit a889046

Please sign in to comment.