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

Add back old test

Apply solution without isGlobalProject

Apply suggestion from code review
  • Loading branch information
kenrick95 committed Nov 22, 2019
1 parent edde511 commit 3d8549c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 14 deletions.
52 changes: 51 additions & 1 deletion e2e/__tests__/multiProjectRunner.test.ts
Expand Up @@ -199,7 +199,57 @@ test.each([{projectPath: 'packages/somepackage'}, {projectPath: 'packages/*'}])(
});

const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('PASS packages/somepackage/test.js');
expect(stderr).toContain('PASS somepackage packages/somepackage/test.js');
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
},
);

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": {
"projects": [
"${projectPath}"
]
}
}
`,
'packages/p1/package.json': `
{
"jest": {
"displayName": "p1"
}
}
`,
'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 ${displayName} ${projectPath}/test.js`);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(stdout).toEqual('');
expect(exitCode).toEqual(0);
Expand Down
35 changes: 22 additions & 13 deletions packages/jest-config/src/index.ts
Expand Up @@ -9,6 +9,7 @@ import * as fs from 'fs';
import * as path from 'path';
import {Config} from '@jest/types';
import chalk = require('chalk');
import {sync as realpath} from 'realpath-native';
import {isJSONString, replaceRootDirInPath} from './utils';
import normalize from './normalize';
import resolveConfigPath from './resolveConfigPath';
Expand Down Expand Up @@ -278,26 +279,24 @@ 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 projectIsCwd =
process.platform === 'win32'
? projects[0] === realpath(process.cwd())
: projects[0] === process.cwd();

const parsedConfigs = projects
.filter(root => {
// Ignore globbed files that cannot be `require`d.
Expand All @@ -313,9 +312,19 @@ export function readConfigs(

return true;
})
.map((root, projectIndex) =>
readConfig(argv, root, true, configPath, projectIndex),
);
.map((root, projectIndex) => {
const projectIsTheOnlyProject =
projectIndex === 0 && projects.length === 1;
const skipArgvConfigOption = !(projectIsTheOnlyProject && projectIsCwd);

return readConfig(
argv,
root,
skipArgvConfigOption,
configPath,
projectIndex,
);
});

ensureNoDuplicateConfigs(parsedConfigs, projects);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
Expand Down

0 comments on commit 3d8549c

Please sign in to comment.