From 54bdaed255a350339c2b6e541fc584a9c0c83a17 Mon Sep 17 00:00:00 2001 From: Anton Alexandrenok Date: Sun, 16 Feb 2020 11:26:03 +0300 Subject: [PATCH 1/2] fix(cli): .cjs files support by "--config" option Fixes #9086 --- .../jest-cli/src/__tests__/cli/args.test.ts | 27 ++++++++++++++++++- packages/jest-cli/src/cli/args.ts | 4 +-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index dfe1c80b4f02..ac2fb7a80772 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -62,7 +62,32 @@ describe('check', () => { 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 a .js, .cjs, or .json extension', + ); + }); + + it('raises an exception if config is not a js/cjs/json file path', () => { + expect(() => + check({config: 'jest.config.js'} as Config.Argv), + ).not.toThrow(); + expect(() => + check({config: '../test/test/my_conf.js'} as Config.Argv), + ).not.toThrow(); + expect(() => + check({config: 'jest.config.cjs'} as Config.Argv), + ).not.toThrow(); + expect(() => + check({config: 'jest.config.json'} as Config.Argv), + ).not.toThrow(); + + const message = + 'The --config option requires a JSON string literal, or a file path with a .js, .cjs, or .json extension'; + + expect(() => check({config: 'jest.config.mjs'} as Config.Argv)).toThrow( + message, + ); + expect(() => check({config: 'jest.config.ts'} 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..32fed0ef75f1 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -52,10 +52,10 @@ export function check(argv: Config.Argv): true { if ( argv.config && !isJSONString(argv.config) && - !argv.config.match(/\.js(on)?$/) + !argv.config.match(/\.(js|cjs|json)$/) ) { throw new Error( - 'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' + + 'The --config option requires a JSON string literal, or a file path with a .js, .cjs, or .json extension.\n' + 'Example usage: jest --config ./jest.config.js', ); } From a5a75d252c8fe65aa279f031601a30b0ef663a96 Mon Sep 17 00:00:00 2001 From: Anton Alexandrenok Date: Sun, 16 Feb 2020 12:39:28 +0300 Subject: [PATCH 2/2] fix(cli): support .mjs in --config option --- CHANGELOG.md | 1 + .../jest-cli/src/__tests__/cli/args.test.ts | 36 +++++++++---------- packages/jest-cli/src/cli/args.ts | 16 ++++++--- 3 files changed, 31 insertions(+), 22 deletions(-) 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 ac2fb7a80772..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,34 +60,33 @@ 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, .cjs, 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 js/cjs/json file path', () => { - expect(() => - check({config: 'jest.config.js'} as Config.Argv), - ).not.toThrow(); - expect(() => - check({config: '../test/test/my_conf.js'} as Config.Argv), - ).not.toThrow(); - expect(() => - check({config: 'jest.config.cjs'} as Config.Argv), - ).not.toThrow(); - expect(() => - check({config: 'jest.config.json'} as Config.Argv), - ).not.toThrow(); - + 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 a .js, .cjs, or .json extension'; + '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.config.mjs'} as Config.Argv)).toThrow( + expect(() => check({config: 'jest.configjs'} as Config.Argv)).toThrow( message, ); - expect(() => check({config: 'jest.config.ts'} as Config.Argv)).toThrow( + 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 32fed0ef75f1..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|cjs|json)$/) + !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, .cjs, 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`, ); }