diff --git a/packages/jest-cli/src/__tests__/args.test.ts b/packages/jest-cli/src/__tests__/args.test.ts index 512472ac83ed..00e36a329aff 100644 --- a/packages/jest-cli/src/__tests__/args.test.ts +++ b/packages/jest-cli/src/__tests__/args.test.ts @@ -89,13 +89,13 @@ describe('check', () => { it('raises an exception if config is not a valid JSON string', () => { expect(() => check(argv({config: 'x:1'}))).toThrow( - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json', + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .cts, .json', ); }); it('raises an exception if config is not a supported file type', () => { const message = - 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .json'; + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .cjs, .cts, .json'; expect(() => check(argv({config: 'jest.configjs'}))).toThrow(message); expect(() => check(argv({config: 'jest.config.exe'}))).toThrow(message); diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/jest.config.cts b/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/jest.config.cts new file mode 100644 index 000000000000..4f69b4e3bda0 --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/jest.config.cts @@ -0,0 +1,8 @@ +/** + * 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. + */ + +export default {}; diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/package.json b/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/package.json new file mode 100644 index 000000000000..0967ef424bce --- /dev/null +++ b/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cts/package.json @@ -0,0 +1 @@ +{} diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap index 31cca289857e..321310fad460 100644 --- a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap +++ b/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap @@ -18,6 +18,15 @@ Object { } `; +exports[`init has-jest-config-file-cts ask the user whether to override config or not user answered with "Yes" 1`] = ` +Object { + "initial": true, + "message": "It seems that you already have a jest configuration, do you want to override it?", + "name": "continue", + "type": "confirm", +} +`; + exports[`init has-jest-config-file-js ask the user whether to override config or not user answered with "Yes" 1`] = ` Object { "initial": true, @@ -54,6 +63,67 @@ Object { } `; +exports[`init project using jest.config.cts ask the user whether he wants to use Typescript or not user answered with "Yes" 1`] = ` +Array [ + Object { + "initial": true, + "message": "Would you like to use Jest when running "test" script in "package.json"?", + "name": "scripts", + "type": "confirm", + }, + Object { + "initial": false, + "message": "Would you like to use Typescript for the configuration file?", + "name": "useTypescript", + "type": "confirm", + }, + Object { + "choices": Array [ + Object { + "title": "node", + "value": "node", + }, + Object { + "title": "jsdom (browser-like)", + "value": "jsdom", + }, + ], + "initial": 0, + "message": "Choose the test environment that will be used for testing", + "name": "environment", + "type": "select", + }, + Object { + "initial": false, + "message": "Do you want Jest to add coverage reports?", + "name": "coverage", + "type": "confirm", + }, + Object { + "choices": Array [ + Object { + "title": "v8", + "value": "v8", + }, + Object { + "title": "babel", + "value": "babel", + }, + ], + "initial": 0, + "message": "Which provider should be used to instrument code for coverage?", + "name": "coverageProvider", + "type": "select", + }, + Object { + "initial": false, + "message": "Automatically clear mock calls, instances, contexts and results before every test?", + "name": "clearMocks", + "type": "confirm", + }, +] +`; + exports[`init project using jest.config.ts ask the user whether he wants to use Typescript or not user answered with "Yes" 1`] = ` Array [ Object { diff --git a/packages/jest-config/src/constants.ts b/packages/jest-config/src/constants.ts index 476874874ec7..63172c139250 100644 --- a/packages/jest-config/src/constants.ts +++ b/packages/jest-config/src/constants.ts @@ -15,11 +15,13 @@ export const JEST_CONFIG_EXT_CJS = '.cjs'; export const JEST_CONFIG_EXT_MJS = '.mjs'; export const JEST_CONFIG_EXT_JS = '.js'; export const JEST_CONFIG_EXT_TS = '.ts'; +export const JEST_CONFIG_EXT_CTS = '.cts'; export const JEST_CONFIG_EXT_JSON = '.json'; export const JEST_CONFIG_EXT_ORDER = Object.freeze([ JEST_CONFIG_EXT_JS, JEST_CONFIG_EXT_TS, JEST_CONFIG_EXT_MJS, JEST_CONFIG_EXT_CJS, + JEST_CONFIG_EXT_CTS, JEST_CONFIG_EXT_JSON, ]); diff --git a/packages/jest-config/src/readConfigFileAndSetRootDir.ts b/packages/jest-config/src/readConfigFileAndSetRootDir.ts index fef76c2c7ab6..48e2c3e0d0a2 100644 --- a/packages/jest-config/src/readConfigFileAndSetRootDir.ts +++ b/packages/jest-config/src/readConfigFileAndSetRootDir.ts @@ -13,6 +13,7 @@ import type {Service} from 'ts-node'; import type {Config} from '@jest/types'; import {interopRequireDefault, requireOrImportModule} from 'jest-util'; import { + JEST_CONFIG_EXT_CTS, JEST_CONFIG_EXT_JSON, JEST_CONFIG_EXT_TS, PACKAGE_JSON, @@ -26,7 +27,7 @@ import { export default async function readConfigFileAndSetRootDir( configPath: string, ): Promise { - const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS); + const isTS = configPath.endsWith(JEST_CONFIG_EXT_TS || JEST_CONFIG_EXT_CTS); const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON); let configObject;