Skip to content

Commit

Permalink
fix(cli): .cjs & .mjs files support by "--config" option (#9578)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-spyke committed Feb 16, 2020
1 parent d4a10f7 commit 63593a2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
27 changes: 26 additions & 1 deletion packages/jest-cli/src/__tests__/cli/args.test.ts
Expand Up @@ -7,6 +7,7 @@
*/

import {Config} from '@jest/types';
import {constants} from 'jest-config';
import {check} from '../../cli/args';
import {buildArgv} from '../../cli';

Expand Down Expand Up @@ -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,
);
});
});
Expand Down
16 changes: 12 additions & 4 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`,
);
}

Expand Down

0 comments on commit 63593a2

Please sign in to comment.