Skip to content

Commit

Permalink
Allow for jest.config.cts, add fixture and snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
DerTimonius committed Apr 13, 2023
1 parent 285b40d commit faa4fff
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/jest-cli/src/__tests__/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-config/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
3 changes: 2 additions & 1 deletion packages/jest-config/src/readConfigFileAndSetRootDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +27,7 @@ import {
export default async function readConfigFileAndSetRootDir(
configPath: string,
): Promise<Config.InitialOptions> {
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;

Expand Down

0 comments on commit faa4fff

Please sign in to comment.