diff --git a/CHANGELOG.md b/CHANGELOG.md index 0daa54b614b6..4bd86745e036 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `[jest-config]` Support `.mjs` config files on Windows as well ([#9558](https://github.com/facebook/jest/pull/9558)) - `[jest-config]` Verify `rootDir` and all `roots` are directories ([#9569](https://github.com/facebook/jest/pull/9569)) - `[jest-cli]` Set `coverageProvider` correctly when provided in config ([#9562](https://github.com/facebook/jest/pull/9562)) +- `[jest-cli]` Allow specifying `.cjs` and `.mjs` config files by `--config` CLI option ([#9578](https://github.com/facebook/jest/pull/9578)) - `[jest-config]` Ensure pattern of `replacePosixSep` is a string ([#9546]https://github.com/facebook/jest/pull/9546) - `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499)) - `[@jest/reporters]` Notifications should be fire&forget rather than having a timeout ([#9567](https://github.com/facebook/jest/pull/9567)) diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index dfe1c80b4f02..450c43c5c2fb 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -7,6 +7,7 @@ */ import {Config} from '@jest/types'; +import {constants} from 'jest-config'; import {check} from '../../cli/args'; import {buildArgv} from '../../cli'; @@ -59,10 +60,34 @@ describe('check', () => { expect(() => check(argv)).not.toThrow(); }); + test.each(constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)))( + 'allows using "%s" file for --config option', + ext => { + expect(() => + check({config: `jest.config.${ext}`} as Config.Argv), + ).not.toThrow(); + expect(() => + check({config: `../test/test/my_conf.${ext}`} as Config.Argv), + ).not.toThrow(); + }, + ); + it('raises an exception if config is not a valid JSON string', () => { const argv = {config: 'x:1'} as Config.Argv; expect(() => check(argv)).toThrow( - 'The --config option requires a JSON string literal, or a file path with a .js or .json extension', + 'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .mjs, .cjs, .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, .mjs, .cjs, .json'; + + expect(() => check({config: 'jest.configjs'} as Config.Argv)).toThrow( + message, + ); + expect(() => check({config: 'jest.config.exe'} as Config.Argv)).toThrow( + message, ); }); }); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 6eeebac1be07..47b0d5f89285 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -6,7 +6,7 @@ */ import {Config} from '@jest/types'; -import {isJSONString} from 'jest-config'; +import {constants, isJSONString} from 'jest-config'; import isCI = require('is-ci'); export function check(argv: Config.Argv): true { @@ -52,11 +52,19 @@ export function check(argv: Config.Argv): true { if ( argv.config && !isJSONString(argv.config) && - !argv.config.match(/\.js(on)?$/) + !argv.config.match( + new RegExp( + `\\.(${constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)).join( + '|', + )})$`, + 'i', + ), + ) ) { throw new Error( - 'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' + - 'Example usage: jest --config ./jest.config.js', + `The --config option requires a JSON string literal, or a file path with one of these extensions: ${constants.JEST_CONFIG_EXT_ORDER.join( + ', ', + )}.\nExample usage: jest --config ./jest.config.js`, ); }