Skip to content

Commit cdc64c6

Browse files
authored
feat: warn when there are multiple configs (#11922)
1 parent 24f9bc8 commit cdc64c6

File tree

19 files changed

+321
-19
lines changed

19 files changed

+321
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-config]` Warn when multiple Jest configs are located ([#11922](https://github.com/facebook/jest/pull/11922))
6+
57
### Fixes
68

79
- `[expect]` Pass matcher context to asymmetric matchers ([#11926](https://github.com/facebook/jest/pull/11926) & [#11930](https://github.com/facebook/jest/pull/11930))

e2e/Utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,23 @@ interface JestPackageJson extends PackageJson {
176176
}
177177

178178
const DEFAULT_PACKAGE_JSON: JestPackageJson = {
179-
description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT',
180179
jest: {
181180
testEnvironment: 'node',
182181
},
183182
};
184183

185184
export const createEmptyPackage = (
186185
directory: Config.Path,
187-
packageJson = DEFAULT_PACKAGE_JSON,
186+
packageJson: PackageJson = DEFAULT_PACKAGE_JSON,
188187
) => {
188+
const packageJsonWithDefaults = {
189+
...packageJson,
190+
description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT',
191+
};
189192
fs.mkdirSync(directory, {recursive: true});
190193
fs.writeFileSync(
191194
path.resolve(directory, 'package.json'),
192-
JSON.stringify(packageJson, null, 2),
195+
JSON.stringify(packageJsonWithDefaults, null, 2),
193196
);
194197
};
195198

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`multiple configs will warn 1`] = `
4+
● Multiple configurations found:
5+
* <rootDir>/e2e/multiple-configs/jest.config.js
6+
* <rootDir>/e2e/multiple-configs/jest.config.json
7+
* \`jest\` key in <rootDir>/e2e/multiple-configs/package.json
8+
9+
Implicit config resolution does not allow multiple configuration files.
10+
Either remove unused config files or select one explicitly with \`--config\`.
11+
12+
Configuration Documentation:
13+
https://jestjs.io/docs/configuration.html
14+
15+
PASS Config from js file __tests__/test.js
16+
✓ dummy test
17+
`;
18+
19+
exports[`multiple configs will warn 2`] = `
20+
Test Suites: 1 passed, 1 total
21+
Tests: 1 passed, 1 total
22+
Snapshots: 0 total
23+
Time: <<REPLACED>>
24+
Ran all test suites.
25+
`;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 {getConfig} from '../runJest';
9+
10+
test('reads config from cjs file', () => {
11+
const {configs} = getConfig(
12+
'config-override',
13+
['--config', 'different-config.json'],
14+
{
15+
skipPkgJsonCheck: true,
16+
},
17+
);
18+
19+
expect(configs).toHaveLength(1);
20+
expect(configs[0].displayName).toEqual({
21+
color: 'white',
22+
name: 'Config from different-config.json file',
23+
});
24+
});

e2e/__tests__/dependencyClash.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const hasteImplModulePath = path
1818

1919
beforeEach(() => {
2020
cleanup(tempDir);
21-
createEmptyPackage(tempDir);
21+
createEmptyPackage(tempDir, {});
2222
});
2323

2424
// This test case is checking that when having both

e2e/__tests__/multipleConfigs.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 * as path from 'path';
9+
import {wrap} from 'jest-snapshot-serializer-raw';
10+
import slash = require('slash');
11+
import {extractSummary} from '../Utils';
12+
import runJest from '../runJest';
13+
14+
const MULTIPLE_CONFIGS_WARNING_TEXT = 'Multiple configurations found';
15+
16+
test('multiple configs will warn', () => {
17+
const rootDir = slash(path.resolve(__dirname, '../..'));
18+
const {exitCode, stderr} = runJest('multiple-configs', [], {
19+
skipPkgJsonCheck: true,
20+
});
21+
22+
expect(exitCode).toBe(0);
23+
expect(stderr).toContain(MULTIPLE_CONFIGS_WARNING_TEXT);
24+
25+
const cleanStdErr = stderr.replace(new RegExp(rootDir, 'g'), '<rootDir>');
26+
const {rest, summary} = extractSummary(cleanStdErr);
27+
28+
expect(wrap(rest)).toMatchSnapshot();
29+
expect(wrap(summary)).toMatchSnapshot();
30+
});
31+
32+
test('multiple configs warning can be suppressed by using --config', () => {
33+
const {exitCode, stderr} = runJest(
34+
'multiple-configs',
35+
['--config', 'jest.config.json'],
36+
{
37+
skipPkgJsonCheck: true,
38+
},
39+
);
40+
41+
expect(exitCode).toBe(0);
42+
expect(stderr).not.toContain(MULTIPLE_CONFIGS_WARNING_TEXT);
43+
});
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+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"displayName": "Config from different-config.json file"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"displayName": "Config from json file"
3+
}

e2e/config-override/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "config-override"
3+
}

0 commit comments

Comments
 (0)