Skip to content

Commit

Permalink
fix: Jest mutli-project-runner still cannot handle exactly one project
Browse files Browse the repository at this point in the history
Add failing test case

Explicitly distinguish if project has been added using process.cwd

Fix CI errors
  • Loading branch information
kenrick95 committed Sep 6, 2019
1 parent 70bd497 commit d447ef0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
30 changes: 22 additions & 8 deletions e2e/__tests__/multiProjectRunner.test.ts
Expand Up @@ -170,36 +170,50 @@ test('"No tests found" message for projects', () => {
);
});

test.each([{projectPath: 'packages/somepackage'}, {projectPath: 'packages/*'}])(
'allows a single non-root project',
({projectPath}: {projectPath: string}) => {
test.each([
{displayName: 'p1', projectPath: 'packages/p1'},
{displayName: 'p2', projectPath: 'packages/p2'},
])(
'correctly runs a single non-root project',
({projectPath, displayName}: {projectPath: string; displayName: string}) => {
writeFiles(DIR, {
'package.json': `
{
"jest": {
"testMatch": ["<rootDir>/packages/somepackage/test.js"],
"projects": [
"${projectPath}"
]
}
}
`,
'packages/somepackage/package.json': `
'packages/p1/package.json': `
{
"jest": {
"displayName": "somepackage"
"displayName": "p1"
}
}
`,
'packages/somepackage/test.js': `
'packages/p1/test.js': `
test('1+1', () => {
expect(1).toBe(1);
});
`,
'packages/p2/package.json': `
{
"jest": {
"displayName": "p2"
}
}
`,
'packages/p2/test.js': `
test('1+1', () => {
expect(1).toBe(1);
});
`,
});

const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('PASS packages/somepackage/test.js');
expect(stderr).toContain(`PASS ${displayName} ${projectPath}/test.js`);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
Expand Down
18 changes: 14 additions & 4 deletions packages/jest-cli/src/cli/index.ts
Expand Up @@ -28,9 +28,16 @@ export async function run(maybeArgv?: Array<string>, project?: Config.Path) {
return;
}

const projects = getProjectListFromCLIArgs(argv, project);

const {results, globalConfig} = await runCLI(argv, projects);
const {isGlobalProject, projects} = getProjectListFromCLIArgs(
argv,
project,
);

const {results, globalConfig} = await runCLI(
argv,
projects,
isGlobalProject,
);
readResultsAndExit(results, globalConfig);
} catch (error) {
clearLine(process.stderr);
Expand Down Expand Up @@ -86,6 +93,7 @@ const getProjectListFromCLIArgs = (
argv: Config.Argv,
project?: Config.Path,
) => {
let isGlobalProject = false;
const projects = argv.projects ? argv.projects : [];

if (project) {
Expand All @@ -95,17 +103,19 @@ const getProjectListFromCLIArgs = (
if (!projects.length && process.platform === 'win32') {
try {
projects.push(realpath(process.cwd()));
isGlobalProject = true;
} catch (err) {
// do nothing, just catch error
// process.binding('fs').realpath can throw, e.g. on mapped drives
}
}

if (!projects.length) {
isGlobalProject = true;
projects.push(process.cwd());
}

return projects;
return {isGlobalProject, projects};
};

const readResultsAndExit = (
Expand Down
32 changes: 19 additions & 13 deletions packages/jest-config/src/index.ts
Expand Up @@ -263,6 +263,7 @@ This usually means that your ${chalk.bold(
export function readConfigs(
argv: Config.Argv,
projectPaths: Array<Config.Path>,
isGlobalProject: boolean,
): {
globalConfig: Config.GlobalConfig;
configs: Array<Config.ProjectConfig>;
Expand All @@ -278,26 +279,19 @@ export function readConfigs(
const parsedConfig = readConfig(argv, projects[0]);
configPath = parsedConfig.configPath;

if (parsedConfig.globalConfig.projects) {
// If this was a single project, and its config has `projects`
// settings, use that value instead.
projects = parsedConfig.globalConfig.projects;
}

hasDeprecationWarnings = parsedConfig.hasDeprecationWarnings;
globalConfig = parsedConfig.globalConfig;
configs = [parsedConfig.projectConfig];
if (globalConfig.projects && globalConfig.projects.length) {
// Even though we had one project in CLI args, there might be more
// projects defined in the config.
// In other words, if this was a single project,
// and its config has `projects` settings, use that value instead.
projects = globalConfig.projects;
}
}

if (
projects.length > 1 ||
(projects.length && typeof projects[0] === 'object')
) {
if (projects.length > 0) {
const parsedConfigs = projects
.filter(root => {
// Ignore globbed files that cannot be `require`d.
Expand All @@ -313,9 +307,21 @@ export function readConfigs(

return true;
})
.map((root, projectIndex) =>
readConfig(argv, root, true, configPath, projectIndex),
);
.map((root, projectIndex) => {
// Skip on all except: projectIndex === 0, and isGlobalProject, and projects.length === 1
const skipArgvConfigOption = !(
projectIndex === 0 &&
isGlobalProject &&
projects.length === 1
);
return readConfig(
argv,
root,
skipArgvConfigOption,
configPath,
projectIndex,
);
});

ensureNoDuplicateConfigs(parsedConfigs, projects);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-core/src/cli/index.ts
Expand Up @@ -34,6 +34,7 @@ type OnCompleteCallback = (results: AggregatedResult) => void;
export const runCLI = async (
argv: Config.Argv,
projects: Array<Config.Path>,
isGlobalProject: boolean,
): Promise<{
results: AggregatedResult;
globalConfig: Config.GlobalConfig;
Expand All @@ -52,6 +53,7 @@ export const runCLI = async (
const {globalConfig, configs, hasDeprecationWarnings} = readConfigs(
argv,
projects,
isGlobalProject,
);

if (argv.debug) {
Expand Down

0 comments on commit d447ef0

Please sign in to comment.