Skip to content

Commit 32665d8

Browse files
committed
fix(cli): .cjs files support by "--config" option
Fixes jestjs#9086
1 parent b2c8a69 commit 32665d8

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,32 @@ describe('check', () => {
6262
it('raises an exception if config is not a valid JSON string', () => {
6363
const argv = {config: 'x:1'} as Config.Argv;
6464
expect(() => check(argv)).toThrow(
65-
'The --config option requires a JSON string literal, or a file path with a .js or .json extension',
65+
'The --config option requires a JSON string literal, or a file path with a .js, .cjs, or .json extension',
66+
);
67+
});
68+
69+
it('raises an exception if config is not a js/cjs/json file path', () => {
70+
expect(() =>
71+
check({config: 'jest.config.js'} as Config.Argv),
72+
).not.toThrow();
73+
expect(() =>
74+
check({config: '../test/test/my_conf.js'} as Config.Argv),
75+
).not.toThrow();
76+
expect(() =>
77+
check({config: 'jest.config.cjs'} as Config.Argv),
78+
).not.toThrow();
79+
expect(() =>
80+
check({config: 'jest.config.json'} as Config.Argv),
81+
).not.toThrow();
82+
83+
const message =
84+
'The --config option requires a JSON string literal, or a file path with a .js, .cjs, or .json extension';
85+
86+
expect(() => check({config: 'jest.config.mjs'} as Config.Argv)).toThrow(
87+
message,
88+
);
89+
expect(() => check({config: 'jest.config.ts'} as Config.Argv)).toThrow(
90+
message,
6691
);
6792
});
6893
});

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export const check = (argv: Config.Argv) => {
5252
if (
5353
argv.config &&
5454
!isJSONString(argv.config) &&
55-
!argv.config.match(/\.js(on)?$/)
55+
!argv.config.match(/\.(js|cjs|json)$/)
5656
) {
5757
throw new Error(
58-
'The --config option requires a JSON string literal, or a file path with a .js or .json extension.\n' +
58+
'The --config option requires a JSON string literal, or a file path with a .js, .cjs, or .json extension.\n' +
5959
'Example usage: jest --config ./jest.config.js',
6060
);
6161
}

0 commit comments

Comments
 (0)