Skip to content

Commit faa4fff

Browse files
committed
Allow for jest.config.cts, add fixture and snapshots
1 parent 285b40d commit faa4fff

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

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

+2-2
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, .json',
92+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .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, .json';
98+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .cts, .json';
9999

100100
expect(() => check(argv({config: 'jest.configjs'}))).toThrow(message);
101101
expect(() => check(argv({config: 'jest.config.exe'}))).toThrow(message);
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 {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

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

+70
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Object {
1818
}
1919
`;
2020

21+
exports[`init has-jest-config-file-cts ask the user whether to override config or not user answered with "Yes" 1`] = `
22+
Object {
23+
"initial": true,
24+
"message": "It seems that you already have a jest configuration, do you want to override it?",
25+
"name": "continue",
26+
"type": "confirm",
27+
}
28+
`;
29+
2130
exports[`init has-jest-config-file-js ask the user whether to override config or not user answered with "Yes" 1`] = `
2231
Object {
2332
"initial": true,
@@ -54,6 +63,67 @@ Object {
5463
}
5564
`;
5665

66+
exports[`init project using jest.config.cts ask the user whether he wants to use Typescript or not user answered with "Yes" 1`] = `
67+
Array [
68+
Object {
69+
"initial": true,
70+
"message": "Would you like to use Jest when running "test" script in "package.json"?",
71+
"name": "scripts",
72+
"type": "confirm",
73+
},
74+
Object {
75+
"initial": false,
76+
"message": "Would you like to use Typescript for the configuration file?",
77+
"name": "useTypescript",
78+
"type": "confirm",
79+
},
80+
Object {
81+
"choices": Array [
82+
Object {
83+
"title": "node",
84+
"value": "node",
85+
},
86+
Object {
87+
"title": "jsdom (browser-like)",
88+
"value": "jsdom",
89+
},
90+
],
91+
"initial": 0,
92+
"message": "Choose the test environment that will be used for testing",
93+
"name": "environment",
94+
"type": "select",
95+
},
96+
Object {
97+
"initial": false,
98+
"message": "Do you want Jest to add coverage reports?",
99+
"name": "coverage",
100+
"type": "confirm",
101+
},
102+
Object {
103+
"choices": Array [
104+
Object {
105+
"title": "v8",
106+
"value": "v8",
107+
},
108+
Object {
109+
"title": "babel",
110+
"value": "babel",
111+
},
112+
],
113+
"initial": 0,
114+
"message": "Which provider should be used to instrument code for coverage?",
115+
"name": "coverageProvider",
116+
"type": "select",
117+
},
118+
Object {
119+
"initial": false,
120+
"message": "Automatically clear mock calls, instances, contexts and results before every test?",
121+
"name": "clearMocks",
122+
"type": "confirm",
123+
},
124+
]
125+
`;
126+
57127
exports[`init project using jest.config.ts ask the user whether he wants to use Typescript or not user answered with "Yes" 1`] = `
58128
Array [
59129
Object {

packages/jest-config/src/constants.ts

+2
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_CTS = '.cts';
1819
export const JEST_CONFIG_EXT_JSON = '.json';
1920
export const JEST_CONFIG_EXT_ORDER = Object.freeze([
2021
JEST_CONFIG_EXT_JS,
2122
JEST_CONFIG_EXT_TS,
2223
JEST_CONFIG_EXT_MJS,
2324
JEST_CONFIG_EXT_CJS,
25+
JEST_CONFIG_EXT_CTS,
2426
JEST_CONFIG_EXT_JSON,
2527
]);

packages/jest-config/src/readConfigFileAndSetRootDir.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {Service} from 'ts-node';
1313
import type {Config} from '@jest/types';
1414
import {interopRequireDefault, requireOrImportModule} from 'jest-util';
1515
import {
16+
JEST_CONFIG_EXT_CTS,
1617
JEST_CONFIG_EXT_JSON,
1718
JEST_CONFIG_EXT_TS,
1819
PACKAGE_JSON,
@@ -26,7 +27,7 @@ import {
2627
export default async function readConfigFileAndSetRootDir(
2728
configPath: string,
2829
): Promise<Config.InitialOptions> {
29-
const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS);
30+
const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS || JEST_CONFIG_EXT_CTS);
3031
const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON);
3132
let configObject;
3233

0 commit comments

Comments
 (0)