Skip to content

Commit 63593a2

Browse files
authored
fix(cli): .cjs & .mjs files support by "--config" option (#9578)
1 parent d4a10f7 commit 63593a2

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- `[jest-config]` Support `.mjs` config files on Windows as well ([#9558](https://github.com/facebook/jest/pull/9558))
1515
- `[jest-config]` Verify `rootDir` and all `roots` are directories ([#9569](https://github.com/facebook/jest/pull/9569))
1616
- `[jest-cli]` Set `coverageProvider` correctly when provided in config ([#9562](https://github.com/facebook/jest/pull/9562))
17+
- `[jest-cli]` Allow specifying `.cjs` and `.mjs` config files by `--config` CLI option ([#9578](https://github.com/facebook/jest/pull/9578))
1718
- `[jest-config]` Ensure pattern of `replacePosixSep` is a string ([#9546]https://github.com/facebook/jest/pull/9546)
1819
- `[jest-matcher-utils]` Fix diff highlight of symbol-keyed object. ([#9499](https://github.com/facebook/jest/pull/9499))
1920
- `[@jest/reporters]` Notifications should be fire&forget rather than having a timeout ([#9567](https://github.com/facebook/jest/pull/9567))

packages/jest-cli/src/__tests__/cli/args.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {Config} from '@jest/types';
10+
import {constants} from 'jest-config';
1011
import {check} from '../../cli/args';
1112
import {buildArgv} from '../../cli';
1213

@@ -59,10 +60,34 @@ describe('check', () => {
5960
expect(() => check(argv)).not.toThrow();
6061
});
6162

63+
test.each(constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)))(
64+
'allows using "%s" file for --config option',
65+
ext => {
66+
expect(() =>
67+
check({config: `jest.config.${ext}`} as Config.Argv),
68+
).not.toThrow();
69+
expect(() =>
70+
check({config: `../test/test/my_conf.${ext}`} as Config.Argv),
71+
).not.toThrow();
72+
},
73+
);
74+
6275
it('raises an exception if config is not a valid JSON string', () => {
6376
const argv = {config: 'x:1'} as Config.Argv;
6477
expect(() => check(argv)).toThrow(
65-
'The --config option requires a JSON string literal, or a file path with a .js or .json extension',
78+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .mjs, .cjs, .json',
79+
);
80+
});
81+
82+
it('raises an exception if config is not a supported file type', () => {
83+
const message =
84+
'The --config option requires a JSON string literal, or a file path with one of these extensions: .js, .mjs, .cjs, .json';
85+
86+
expect(() => check({config: 'jest.configjs'} as Config.Argv)).toThrow(
87+
message,
88+
);
89+
expect(() => check({config: 'jest.config.exe'} as Config.Argv)).toThrow(
90+
message,
6691
);
6792
});
6893
});

packages/jest-cli/src/cli/args.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import {Config} from '@jest/types';
9-
import {isJSONString} from 'jest-config';
9+
import {constants, isJSONString} from 'jest-config';
1010
import isCI = require('is-ci');
1111

1212
export function check(argv: Config.Argv): true {
@@ -52,11 +52,19 @@ export function check(argv: Config.Argv): true {
5252
if (
5353
argv.config &&
5454
!isJSONString(argv.config) &&
55-
!argv.config.match(/\.js(on)?$/)
55+
!argv.config.match(
56+
new RegExp(
57+
`\\.(${constants.JEST_CONFIG_EXT_ORDER.map(e => e.substring(1)).join(
58+
'|',
59+
)})$`,
60+
'i',
61+
),
62+
)
5663
) {
5764
throw new Error(
58-
'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' +
59-
'Example usage: jest --config ./jest.config.js',
65+
`The --config option requires a JSON string literal, or a file path with one of these extensions: ${constants.JEST_CONFIG_EXT_ORDER.join(
66+
', ',
67+
)}.\nExample usage: jest --config ./jest.config.js`,
6068
);
6169
}
6270

0 commit comments

Comments
 (0)