Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-config): add mts and cts to default moduleFileExtensions config #14369

Merged
merged 10 commits into from
Sep 20, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-config]` [**BREAKING**] Add `mts` and `cts` to default `moduleFileExtensions` config ([#14369](https://github.com/facebook/jest/pull/14369))
- `[@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
6 changes: 3 additions & 3 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const {defaults} = require('jest-config');

/** @type {import('jest').Config} */
const config = {
moduleFileExtensions: [...defaults.moduleFileExtensions, 'mts', 'cts'],
moduleDirectories: [...defaults.moduleDirectories, 'bower_components'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed to moduleDirectories?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same question. My guess: if 'mts' and 'cts' get included in the default, the code in the example does nothing useful for the user. So @yinm was simply trying to find a way to keep the example useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, @mrazauskas's guess is right. I was trying to find a way to keep the example useful at 0f3e261.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha! makes sense 🙂

};

module.exports = config;
Expand All @@ -103,7 +103,7 @@ import type {Config} from 'jest';
import {defaults} from 'jest-config';

const config: Config = {
moduleFileExtensions: [...defaults.moduleFileExtensions, 'mts'],
moduleDirectories: [...defaults.moduleDirectories, 'bower_components'],
};

export default config;
Expand Down Expand Up @@ -931,7 +931,7 @@ export default config;

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

Default: `["js", "mjs", "cjs", "jsx", "ts", "tsx", "json", "node"]`
Default: `["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx", "json", "node"]`

An array of file extensions your modules use. If you require modules without specifying a file extension, these are the extensions Jest will look for, in left-to-right order.

Expand Down
2 changes: 2 additions & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ exports[`--showConfig outputs config info and exits 1`] = `
"cjs",
"jsx",
"ts",
"mts",
"cts",
"tsx",
"json",
"node"
Expand Down
9 changes: 9 additions & 0 deletions e2e/__tests__/__snapshots__/testMatchTs.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 cts and mts extension 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
16 changes: 16 additions & 0 deletions e2e/__tests__/testMatchTs.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 cts and mts extension', () => {
const result = runJest('test-match-ts');
expect(result.exitCode).toBe(0);
const {summary} = extractSummary(result.stderr);
expect(summary).toMatchSnapshot();
});
10 changes: 10 additions & 0 deletions e2e/test-match-ts/__tests__/sample-suite.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-ts/__tests__/sample-suite2.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);
});
7 changes: 7 additions & 0 deletions e2e/test-match-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jest": {
"testMatch": [
"**/__tests__/*.?ts"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ const config: Config = {
// "cjs",
// "jsx",
// "ts",
// "mts",
// "cts",
// "tsx",
// "json",
// "node"
Expand Down Expand Up @@ -404,6 +406,8 @@ const config = {
// "cjs",
// "jsx",
// "ts",
// "mts",
// "cts",
// "tsx",
// "json",
// "node"
Expand Down Expand Up @@ -606,6 +610,8 @@ const config = {
// "cjs",
// "jsx",
// "ts",
// "mts",
// "cts",
// "tsx",
// "json",
// "node"
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/Defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const defaultOptions: Config.DefaultOptions = {
'cjs',
'jsx',
'ts',
'mts',
'cts',
'tsx',
'json',
'node',
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export const initialOptions: Config.InitialOptions = {
'json',
'jsx',
'ts',
'mts',
'cts',
'tsx',
'node',
],
Expand Down Expand Up @@ -258,6 +260,8 @@ export const initialProjectOptions: Config.InitialProjectOptions = {
'json',
'jsx',
'ts',
'mts',
'cts',
'tsx',
'node',
],
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,8 @@ describe('moduleFileExtensions', () => {
'cjs',
'jsx',
'ts',
'mts',
'cts',
'tsx',
'json',
'node',
Expand Down