Skip to content

Commit e818dca

Browse files
authored
feat: add support for .mjs config (#9431)
1 parent 8236779 commit e818dca

29 files changed

+284
-130
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
1313
- `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027))
1414
- `[jest-config]` Support `.cjs` config files ([#9291](https://github.com/facebook/jest/pull/9291))
15+
- `[jest-config]` [**BREAKING**] Support `.mjs` config files ([#9431](https://github.com/facebook/jest/pull/9431))
1516
- `[jest-core]` Support reporters as default exports ([#9161](https://github.com/facebook/jest/pull/9161))
1617
- `[jest-core]` Support `--findRelatedTests` paths case insensitivity on Windows ([#8900](https://github.com/facebook/jest/issues/8900))
1718
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))

babel.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ module.exports = {
1919
presets: ['@babel/preset-typescript'],
2020
test: /\.tsx?$/,
2121
},
22+
// we want this file to keep `import()`, so exclude the transform for it
23+
{
24+
plugins: ['@babel/plugin-syntax-dynamic-import'],
25+
presets: [
26+
'@babel/preset-typescript',
27+
[
28+
'@babel/preset-env',
29+
{
30+
exclude: ['@babel/plugin-proposal-dynamic-import'],
31+
shippedProposals: true,
32+
targets: {node: 8},
33+
},
34+
],
35+
],
36+
test: 'packages/jest-config/src/importMjs.ts',
37+
},
2238
],
2339
plugins: [
2440
['@babel/plugin-transform-modules-commonjs', {allowTopLevelThis: true}],

docs/Configuration.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: configuration
33
title: Configuring Jest
44
---
55

6-
Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` file or through the `--config <path/to/file.js|cjs|json>` option. If you'd like to use your `package.json` to store Jest's config, the "jest" key should be used on the top level so Jest will know how to find your settings:
6+
Jest's configuration can be defined in the `package.json` file of your project, or through a `jest.config.js` file or through the `--config <path/to/file.js|cjs|mjs|json>` option. If you'd like to use your `package.json` to store Jest's config, the `"jest"` key should be used on the top level so Jest will know how to find your settings:
77

88
```json
99
{

e2e/__tests__/cjsConfigFile.test.ts

-21
This file was deleted.

e2e/__tests__/esmConfigFile.test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {onNodeVersions} from '@jest/test-utils';
9+
import {json as runWithJson} from '../runJest';
10+
11+
test('reads config from cjs file', () => {
12+
const {json, exitCode} = runWithJson('esm-config/cjs', ['--show-config'], {
13+
skipPkgJsonCheck: true,
14+
});
15+
16+
expect(exitCode).toBe(0);
17+
expect(json.configs).toHaveLength(1);
18+
expect(json.configs[0].displayName).toEqual({
19+
color: 'white',
20+
name: 'Config from cjs file',
21+
});
22+
});
23+
24+
// not unflagged for other versions yet. Update this range if that changes
25+
onNodeVersions('^13.2.0', () => {
26+
test('reads config from mjs file', () => {
27+
const {json, exitCode} = runWithJson('esm-config/mjs', ['--show-config'], {
28+
skipPkgJsonCheck: true,
29+
});
30+
31+
expect(exitCode).toBe(0);
32+
expect(json.configs).toHaveLength(1);
33+
expect(json.configs[0].displayName).toEqual({
34+
color: 'white',
35+
name: 'Config from mjs file',
36+
});
37+
});
38+
});
File renamed without changes.
File renamed without changes.

e2e/esm-config/mjs/__tests__/test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
test('dummy test', () => {
9+
expect(1).toBe(1);
10+
});

e2e/esm-config/mjs/jest.config.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default {
9+
displayName: 'Config from mjs file',
10+
testEnvironment: 'node',
11+
};

e2e/esm-config/mjs/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"jest": {}
3+
}

packages/jest-cli/src/init/__tests__/__snapshots__/init.test.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ Object {
3636
}
3737
`;
3838

39+
exports[`init has-jest-config-file-mjs ask the user whether to override config or not user answered with "Yes" 1`] = `
40+
Object {
41+
"initial": true,
42+
"message": "It seems that you already have a jest configuration, do you want to override it?",
43+
"name": "continue",
44+
"type": "confirm",
45+
}
46+
`;
47+
3948
exports[`init project with package.json and no jest config all questions answered with answer: "No" should return the default configuration (an empty config) 1`] = `
4049
"// For a detailed explanation regarding each configuration property, visit:
4150
// https://jestjs.io/docs/en/configuration.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export default {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "type_module",
3+
"scripts": {
4+
"test": "different-test-runner"
5+
},
6+
"type": "module"
7+
}

packages/jest-cli/src/init/__tests__/init.test.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import * as fs from 'fs';
1010
import * as path from 'path';
1111
import prompts from 'prompts';
12+
import {constants} from 'jest-config';
1213
import init from '../';
13-
import {JEST_CONFIG_EXT_ORDER} from '../constants';
14+
import {onNodeVersions} from '@jest/test-utils';
15+
16+
const {JEST_CONFIG_EXT_ORDER} = constants;
1417

1518
jest.mock('prompts');
1619
jest.mock('../../../../jest-config/build/getCacheDirectory', () => () =>
@@ -52,6 +55,22 @@ describe('init', () => {
5255

5356
expect(evaluatedConfig).toEqual({});
5457
});
58+
59+
onNodeVersions('^13.2.0', () => {
60+
it('should generate empty config with mjs extension', async () => {
61+
prompts.mockReturnValueOnce({});
62+
63+
await init(resolveFromFixture('type_module'));
64+
65+
const writtenJestConfigFilename = fs.writeFileSync.mock.calls[0][0];
66+
const writtenJestConfig = fs.writeFileSync.mock.calls[0][1];
67+
68+
expect(writtenJestConfigFilename.endsWith('.mjs')).toBe(true);
69+
70+
expect(typeof writtenJestConfig).toBe('string');
71+
expect(writtenJestConfig.split('\n')[3]).toBe('export default {');
72+
});
73+
});
5574
});
5675

5776
describe('some questions answered with answer: "Yes"', () => {

packages/jest-cli/src/init/constants.ts

-17
This file was deleted.

packages/jest-cli/src/init/generate_config_file.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const stringifyOption = (
3131
);
3232
};
3333

34-
const generateConfigFile = (results: Record<string, unknown>): string => {
34+
const generateConfigFile = (
35+
results: Record<string, unknown>,
36+
generateEsm = false,
37+
): string => {
3538
const {coverage, clearMocks, environment} = results;
3639

3740
const overrides: Record<string, any> = {};
@@ -75,7 +78,7 @@ const generateConfigFile = (results: Record<string, unknown>): string => {
7578
return (
7679
'// For a detailed explanation regarding each configuration property, visit:\n' +
7780
'// https://jestjs.io/docs/en/configuration.html\n\n' +
78-
'module.exports = {\n' +
81+
(generateEsm ? 'export default {\n' : 'module.exports = {\n') +
7982
properties.join('\n') +
8083
'};\n'
8184
);

packages/jest-cli/src/init/index.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import * as path from 'path';
1010
import chalk = require('chalk');
1111
import prompts = require('prompts');
1212
import {sync as realpath} from 'realpath-native';
13+
import {constants} from 'jest-config';
1314
import defaultQuestions, {testScriptQuestion} from './questions';
1415
import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors';
15-
import {
16+
import generateConfigFile from './generate_config_file';
17+
import modifyPackageJson from './modify_package_json';
18+
import {ProjectPackageJson} from './types';
19+
20+
const {
1621
JEST_CONFIG_BASE_NAME,
17-
JEST_CONFIG_EXT_CJS,
22+
JEST_CONFIG_EXT_MJS,
1823
JEST_CONFIG_EXT_JS,
1924
JEST_CONFIG_EXT_ORDER,
2025
PACKAGE_JSON,
21-
} from './constants';
22-
import generateConfigFile from './generate_config_file';
23-
import modifyPackageJson from './modify_package_json';
24-
import {ProjectPackageJson} from './types';
26+
} = constants;
2527

2628
type PromptsResults = {
2729
clearMocks: boolean;
@@ -65,7 +67,7 @@ export default async (rootDir: string = realpath(process.cwd())) => {
6567
rootDir,
6668
getConfigFilename(
6769
projectPackageJson.type === 'module'
68-
? JEST_CONFIG_EXT_CJS
70+
? JEST_CONFIG_EXT_MJS
6971
: JEST_CONFIG_EXT_JS,
7072
),
7173
);
@@ -131,7 +133,11 @@ export default async (rootDir: string = realpath(process.cwd())) => {
131133
console.log(`✏️ Modified ${chalk.cyan(projectPackageJsonPath)}`);
132134
}
133135

134-
const generatedConfig = generateConfigFile(results);
136+
const generatedConfig = generateConfigFile(
137+
results,
138+
projectPackageJson.type === 'module' ||
139+
jestConfigPath.endsWith(JEST_CONFIG_EXT_MJS),
140+
);
135141

136142
fs.writeFileSync(jestConfigPath, generatedConfig);
137143

packages/jest-config/src/__tests__/Defaults.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import {defaults} from '../index';
99

10+
jest.mock('../importMjs', () => (s: string) => import(s));
11+
1012
test('get configuration defaults', () => {
1113
expect(defaults).toBeDefined();
1214
});

packages/jest-config/src/__tests__/readConfig.test.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
import {readConfig} from '../index';
99

10-
test('readConfig() throws when an object is passed without a file path', () => {
11-
expect(() => {
10+
jest.mock('../importMjs', () => (s: string) => import(s));
11+
12+
test('readConfig() throws when an object is passed without a file path', async () => {
13+
await expect(
1214
readConfig(
1315
// @ts-ignore
1416
null /* argv */,
1517
{} /* packageRootOrConfig */,
1618
false /* skipArgvConfigOption */,
1719
null /* parentConfigPath */,
18-
);
19-
}).toThrowError(
20+
),
21+
).rejects.toThrowError(
2022
'Jest: Cannot use configuration as an object without a file path',
2123
);
2224
});

packages/jest-config/src/__tests__/readConfigs.test.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import {readConfigs} from '../index';
99

10-
test('readConfigs() throws when called without project paths', () => {
11-
expect(() => {
10+
jest.mock('../importMjs', () => (s: string) => import(s));
11+
12+
test('readConfigs() throws when called without project paths', async () => {
13+
await expect(
1214
// @ts-ignore
13-
readConfigs(null /* argv */, [] /* projectPaths */);
14-
}).toThrowError('jest: No configuration found for any project.');
15+
readConfigs(null /* argv */, [] /* projectPaths */),
16+
).rejects.toThrowError('jest: No configuration found for any project.');
1517
});

packages/jest-config/src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ export const DEFAULT_REPORTER_LABEL = 'default';
1313
export const PACKAGE_JSON = 'package.json';
1414
export const JEST_CONFIG_BASE_NAME = 'jest.config';
1515
export const JEST_CONFIG_EXT_CJS = '.cjs';
16+
export const JEST_CONFIG_EXT_MJS = '.mjs';
1617
export const JEST_CONFIG_EXT_JS = '.js';
1718
export const JEST_CONFIG_EXT_JSON = '.json';
1819
export const JEST_CONFIG_EXT_ORDER = Object.freeze([
1920
JEST_CONFIG_EXT_JS,
21+
JEST_CONFIG_EXT_MJS,
2022
JEST_CONFIG_EXT_CJS,
2123
JEST_CONFIG_EXT_JSON,
2224
]);

packages/jest-config/src/importMjs.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// this is in a separate file so that node 8 don't explode with a syntax error.
9+
// Remove this file when we drop support for Node 8
10+
export default (specifier: string) => import(specifier);

0 commit comments

Comments
 (0)