Skip to content

Commit

Permalink
Improve the error message when 2 projects resolve to the same config (#…
Browse files Browse the repository at this point in the history
…5674)

* Improve the error message when 2 projects resolve to the same config

* changelog
  • Loading branch information
thymikee authored and cpojer committed Feb 26, 2018
1 parent f6f0c9c commit e12cab6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
symlinked paths ([#5085](https://github.com/facebook/jest/pull/5085))
* `[jest-editor-support]` Update `Settings` to use spawn in shell option
([#5658](https://github.com/facebook/jest/pull/5658))
* `[jest-cli]` Improve the error message when 2 projects resolve to the same
config ([#5674](https://github.com/facebook/jest/pull/5674))

## 22.4.2

Expand Down
10 changes: 7 additions & 3 deletions integration-tests/__tests__/multi_project_runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import runJest from '../runJest';
import os from 'os';
import path from 'path';
import stripAnsi from 'strip-ansi';

const {cleanup, extractSummary, writeFiles} = require('../Utils');
const SkipOnWindows = require('../../scripts/SkipOnWindows');
Expand Down Expand Up @@ -303,12 +304,15 @@ test('resolves projects and their <rootDir> properly', () => {
}),
});

({stderr} = runJest(DIR));
({stderr} = stripAnsi(runJest(DIR)));
expect(stderr).toMatch(
/One or more specified projects share the same config file/,
/Whoops! Two projects resolved to the same config path/,
);
expect(stderr).toMatch(`${path.join(DIR, 'package.json')}`);
expect(stderr).toMatch(/Project 1|2: dir1/);
expect(stderr).toMatch(/Project 1|2: dir2/);

// praject with a directory/file that does not exist
// project with a directory/file that does not exist
writeFiles(DIR, {
'package.json': JSON.stringify({
jest: {
Expand Down
41 changes: 22 additions & 19 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,30 @@ const printVersionAndExit = outputStream => {
exit(0);
};

const ensureNoDuplicateConfigs = (parsedConfigs, projects) => {
const configPathSet = new Set();

for (const {configPath} of parsedConfigs) {
if (configPathSet.has(configPath)) {
let message =
'One or more specified projects share the same config file\n';

parsedConfigs.forEach(({configPath}, index) => {
message =
message +
'\nProject: "' +
projects[index] +
'"\nConfig: "' +
String(configPath) +
'"';
});
const ensureNoDuplicateConfigs = (parsedConfigs, projects, rootConfigPath) => {
const configPathMap = new Map();

for (const config of parsedConfigs) {
const {configPath} = config;
if (configPathMap.has(configPath)) {
const message = `Whoops! Two projects resolved to the same config path: ${chalk.bold(
String(configPath),
)}:
Project 1: ${chalk.bold(projects[parsedConfigs.findIndex(x => x === config)])}
Project 2: ${chalk.bold(
projects[parsedConfigs.findIndex(x => x === configPathMap.get(configPath))],
)}
This usually means that your ${chalk.bold(
'"projects"',
)} config includes a directory that doesn't have any configuration recognizable by Jest. Please fix it.
`;

throw new Error(message);
}
if (configPath !== null) {
configPathSet.add(configPath);
configPathMap.set(configPath, config);
}
}
};
Expand Down Expand Up @@ -259,7 +262,7 @@ const getConfigs = (
})
.map(root => readConfig(argv, root, true, configPath));

ensureNoDuplicateConfigs(parsedConfigs, projects);
ensureNoDuplicateConfigs(parsedConfigs, projects, configPath);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
if (!hasDeprecationWarnings) {
hasDeprecationWarnings = parsedConfigs.some(
Expand Down

0 comments on commit e12cab6

Please sign in to comment.