Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): .cjs files support by "--config" option #9578

Merged
merged 2 commits into from
Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call 👍

'|',
)})$`,
'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