Skip to content

Commit 446d174

Browse files
the-spykeSimenB
authored andcommitted
Support '.js', '.cjs', '.json' configs (#9291)
1 parent b2c8a69 commit 446d174

File tree

21 files changed

+240
-106
lines changed

21 files changed

+240
-106
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924))
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))
14+
- `[jest-config]` Support `.cjs` config files ([#9291](https://github.com/facebook/jest/pull/9291))
1415
- `[jest-core]` Support reporters as default exports ([#9161](https://github.com/facebook/jest/pull/9161))
1516
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
1617
- `[jest-diff]` [**BREAKING**] Export as ECMAScript module ([#8873](https://github.com/facebook/jest/pull/8873))

docs/Configuration.md

Lines changed: 1 addition & 1 deletion
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/js|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|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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 {json as runWithJson} from '../runJest';
9+
10+
test('reads config from cjs file', () => {
11+
const {json, exitCode} = runWithJson('cjs-config', ['--show-config'], {
12+
skipPkgJsonCheck: true,
13+
});
14+
15+
expect(exitCode).toBe(0);
16+
expect(json.configs).toHaveLength(1);
17+
expect(json.configs[0].displayName).toEqual({
18+
color: 'white',
19+
name: 'Config from cjs file',
20+
});
21+
});

e2e/cjs-config/__tests__/test.js

Lines changed: 10 additions & 0 deletions
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/cjs-config/jest.config.cjs

Lines changed: 11 additions & 0 deletions
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+
module.exports = {
9+
displayName: 'Config from cjs file',
10+
testEnvironment: 'node',
11+
};

e2e/cjs-config/package.json

Lines changed: 3 additions & 0 deletions
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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ Object {
99
}
1010
`;
1111

12-
exports[`init has-jest-config-file ask the user whether to override config or not user answered with "Yes" 1`] = `
12+
exports[`init has-jest-config-file-cjs ask the user whether to override config or not user answered with "Yes" 1`] = `
13+
Object {
14+
"initial": true,
15+
"message": "It seems that you already have a jest configuration, do you want to override it?",
16+
"name": "continue",
17+
"type": "confirm",
18+
}
19+
`;
20+
21+
exports[`init has-jest-config-file-js ask the user whether to override config or not user answered with "Yes" 1`] = `
22+
Object {
23+
"initial": true,
24+
"message": "It seems that you already have a jest configuration, do you want to override it?",
25+
"name": "continue",
26+
"type": "confirm",
27+
}
28+
`;
29+
30+
exports[`init has-jest-config-file-json ask the user whether to override config or not user answered with "Yes" 1`] = `
1331
Object {
1432
"initial": true,
1533
"message": "It seems that you already have a jest configuration, do you want to override it?",
Lines changed: 8 additions & 0 deletions
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+
module.exports = {};

0 commit comments

Comments
 (0)