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

Improve the error message when 2 projects resolve to the same config #5674

Merged
merged 2 commits into from
Feb 26, 2018
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
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