From 54bdaed255a350339c2b6e541fc584a9c0c83a17 Mon Sep 17 00:00:00 2001 From: Anton Alexandrenok Date: Sun, 16 Feb 2020 11:26:03 +0300 Subject: [PATCH] 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', ); }