Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 919e4de

Browse files
Support jest.config.mts
1 parent efb59c2 commit 919e4de

13 files changed

Lines changed: 92 additions & 8 deletions

File tree

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Configuring Jest
55

66
The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
77

8-
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named `jest.config.js|ts|mjs|cjs|cts|json`. You can use [`--config`](CLI.md#--configpath) flag to pass an explicit path to the file.
8+
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named `jest.config.js|ts|mts|mjs|cjs|cts|json`. You can use [`--config`](CLI.md#--configpath) flag to pass an explicit path to the file.
99

1010
:::note
1111

e2e/__tests__/readInitialOptions.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ describe('readInitialOptions', () => {
5353
['pkg-config', 'package.json', 'package.json'],
5454
['ts-node-config', 'jest.config.ts', 'jest.config.ts'],
5555
['ts-esbuild-register-config', 'jest.config.ts', 'jest.config.ts'],
56+
['mts-config', 'jest.config.mts', 'jest.config.mts'],
5657
['mjs-config', 'jest.config.mjs', 'jest.config.mjs'],
5758
['json-config', 'jest.config.json', 'jest.config.json'],
5859
['async-config', 'jest.config.js', 'async-config'],

e2e/__tests__/tsIntegration.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,42 @@ describe('when `Config` type is imported from "@jest/types"', () => {
9797
expect(globalConfig.verbose).toBe(true);
9898
});
9999

100+
test('with object config exported from MTS file when package.json#type=commonjs', () => {
101+
writeFiles(DIR, {
102+
'__tests__/dummy.test.js': "test('dummy', () => expect(123).toBe(123));",
103+
'jest.config.mts': `
104+
import type {Config} from '@jest/types';
105+
const config: Config.InitialOptions = {displayName: 'ts-mts-object-config', verbose: true};
106+
export default config;
107+
`,
108+
'package.json': '{"type": "commonjs"}',
109+
});
110+
111+
const {configs, globalConfig} = getConfig(path.join(DIR));
112+
113+
expect(configs).toHaveLength(1);
114+
expect(configs[0].displayName?.name).toBe('ts-mts-object-config');
115+
expect(globalConfig.verbose).toBe(true);
116+
});
117+
118+
test('with object config exported from MTS file when package.json#type=module', () => {
119+
writeFiles(DIR, {
120+
'__tests__/dummy.test.js': "test('dummy', () => expect(12).toBe(12));",
121+
'jest.config.mts': `
122+
import type {Config} from '@jest/types';
123+
const config: Config.InitialOptions = {displayName: 'ts-mts-esm-object-config', verbose: true};
124+
export default config;
125+
`,
126+
'package.json': '{"type": "module"}',
127+
});
128+
129+
const {configs, globalConfig} = getConfig(path.join(DIR));
130+
131+
expect(configs).toHaveLength(1);
132+
expect(configs[0].displayName?.name).toBe('ts-mts-esm-object-config');
133+
expect(globalConfig.verbose).toBe(true);
134+
});
135+
100136
testIfTsLoader('throws if type errors are encountered', () => {
101137
writeFiles(DIR, {
102138
'__tests__/dummy.test.js': "test('dummy', () => expect(123).toBe(123));",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
export default {
8+
jestConfig: 'jest.config.mts',
9+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/create-jest/src/__tests__/__snapshots__/init.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ Object {
5454
}
5555
`;
5656

57+
exports[`init has-jest-config-file-mts ask the user whether to override config or not user answered with "Yes" 1`] = `
58+
Object {
59+
"initial": true,
60+
"message": "It seems that you already have a jest configuration, do you want to override it?",
61+
"name": "continue",
62+
"type": "confirm",
63+
}
64+
`;
65+
5766
exports[`init has-jest-config-file-ts ask the user whether to override config or not user answered with "Yes" 1`] = `
5867
Object {
5968
"initial": true,

packages/jest-cli/src/__tests__/args.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ describe('check', () => {
8989

9090
it('raises an exception if config is not a valid JSON string', () => {
9191
expect(() => check(argv({config: 'x:1'}))).toThrow(
92-
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .cts, .json',
92+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mts, .mjs, .cjs, .cts, .json',
9393
);
9494
});
9595

9696
it('raises an exception if config is not a supported file type', () => {
9797
const message =
98-
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .cts, .json';
98+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mts, .mjs, .cjs, .cts, .json';
9999

100100
expect(() => check(argv({config: 'jest.configjs'}))).toThrow(message);
101101
expect(() => check(argv({config: 'jest.config.exe'}))).toThrow(message);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ import {onNodeVersions} from '@jest/test-utils';
1414
jest.mock('graceful-fs').mock('jest-util');
1515

1616
describe('readConfigFileAndSetRootDir', () => {
17+
describe('TypeScript ESM file', () => {
18+
test('reads .mts config and sets `rootDir`', async () => {
19+
jest.mocked(requireOrImportModule).mockResolvedValueOnce({notify: true});
20+
21+
const rootDir = path.resolve('some', 'path', 'to');
22+
const config = await readConfigFileAndSetRootDir(
23+
path.join(rootDir, 'jest.config.mts'),
24+
);
25+
26+
expect(config).toEqual({notify: true, rootDir});
27+
});
28+
});
29+
1730
describe('JavaScript file', () => {
1831
test('reads config and sets `rootDir`', async () => {
1932
jest.mocked(requireOrImportModule).mockResolvedValueOnce({notify: true});

packages/jest-config/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export const JEST_CONFIG_EXT_CJS = '.cjs';
1515
export const JEST_CONFIG_EXT_MJS = '.mjs';
1616
export const JEST_CONFIG_EXT_JS = '.js';
1717
export const JEST_CONFIG_EXT_TS = '.ts';
18+
export const JEST_CONFIG_EXT_MTS = '.mts';
1819
export const JEST_CONFIG_EXT_CTS = '.cts';
1920
export const JEST_CONFIG_EXT_JSON = '.json';
2021
export const JEST_CONFIG_EXT_ORDER = Object.freeze([
2122
JEST_CONFIG_EXT_JS,
2223
JEST_CONFIG_EXT_TS,
24+
JEST_CONFIG_EXT_MTS,
2325
JEST_CONFIG_EXT_MJS,
2426
JEST_CONFIG_EXT_CJS,
2527
JEST_CONFIG_EXT_CTS,

0 commit comments

Comments
 (0)