Skip to content

Commit 77c3cee

Browse files
kenrick95SimenB
authored andcommitted
fix: Jest mutli-project-runner should handle exactly one project (#8894)
1 parent 86010d8 commit 77c3cee

3 files changed

Lines changed: 74 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- `[expect]` Consider all RegExp flags for equality ([#9167](https://github.com/facebook/jest/pull/9167))
4141
- `[expect]` [**BREAKING**] Consider primitives different from wrappers instantiated with `new` ([#9167](https://github.com/facebook/jest/pull/9167))
4242
- `[jest-config]` Use half of the available cores when `watchAll` mode is enabled ([#9117](https://github.com/facebook/jest/pull/9117))
43+
- `[jest-config]` Fix Jest multi project runner still cannot handle exactly one project ([#8894](https://github.com/facebook/jest/pull/8894))
4344
- `[jest-console]` Add missing `console.group` calls to `NullConsole` ([#9024](https://github.com/facebook/jest/pull/9024))
4445
- `[jest-core]` Don't include unref'd timers in --detectOpenHandles results ([#8941](https://github.com/facebook/jest/pull/8941))
4546
- `[jest-diff]` Do not inverse format if line consists of one change ([#8903](https://github.com/facebook/jest/pull/8903))

e2e/__tests__/multiProjectRunner.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,57 @@ test.each([{projectPath: 'packages/somepackage'}, {projectPath: 'packages/*'}])(
199199
});
200200

201201
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
202-
expect(stderr).toContain('PASS packages/somepackage/test.js');
202+
expect(stderr).toContain('PASS somepackage packages/somepackage/test.js');
203+
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
204+
expect(stdout).toEqual('');
205+
expect(exitCode).toEqual(0);
206+
},
207+
);
208+
209+
test.each([
210+
{displayName: 'p1', projectPath: 'packages/p1'},
211+
{displayName: 'p2', projectPath: 'packages/p2'},
212+
])(
213+
'correctly runs a single non-root project',
214+
({projectPath, displayName}: {projectPath: string; displayName: string}) => {
215+
writeFiles(DIR, {
216+
'package.json': `
217+
{
218+
"jest": {
219+
"projects": [
220+
"${projectPath}"
221+
]
222+
}
223+
}
224+
`,
225+
'packages/p1/package.json': `
226+
{
227+
"jest": {
228+
"displayName": "p1"
229+
}
230+
}
231+
`,
232+
'packages/p1/test.js': `
233+
test('1+1', () => {
234+
expect(1).toBe(1);
235+
});
236+
`,
237+
'packages/p2/package.json': `
238+
{
239+
"jest": {
240+
"displayName": "p2"
241+
}
242+
}
243+
`,
244+
'packages/p2/test.js': `
245+
test('1+1', () => {
246+
expect(1).toBe(1);
247+
});
248+
`,
249+
});
250+
251+
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
252+
expect(stderr).toContain(`PASS ${displayName} ${projectPath}/test.js`);
203253
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
204254
expect(stdout).toEqual('');
205255
expect(exitCode).toEqual(0);

packages/jest-config/src/index.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as fs from 'fs';
99
import * as path from 'path';
1010
import {Config} from '@jest/types';
1111
import chalk = require('chalk');
12+
import {sync as realpath} from 'realpath-native';
1213
import {isJSONString, replaceRootDirInPath} from './utils';
1314
import normalize from './normalize';
1415
import resolveConfigPath from './resolveConfigPath';
@@ -278,26 +279,24 @@ export function readConfigs(
278279
const parsedConfig = readConfig(argv, projects[0]);
279280
configPath = parsedConfig.configPath;
280281

281-
if (parsedConfig.globalConfig.projects) {
282-
// If this was a single project, and its config has `projects`
283-
// settings, use that value instead.
284-
projects = parsedConfig.globalConfig.projects;
285-
}
286-
287282
hasDeprecationWarnings = parsedConfig.hasDeprecationWarnings;
288283
globalConfig = parsedConfig.globalConfig;
289284
configs = [parsedConfig.projectConfig];
290285
if (globalConfig.projects && globalConfig.projects.length) {
291286
// Even though we had one project in CLI args, there might be more
292287
// projects defined in the config.
288+
// In other words, if this was a single project,
289+
// and its config has `projects` settings, use that value instead.
293290
projects = globalConfig.projects;
294291
}
295292
}
296293

297-
if (
298-
projects.length > 1 ||
299-
(projects.length && typeof projects[0] === 'object')
300-
) {
294+
if (projects.length > 0) {
295+
const projectIsCwd =
296+
process.platform === 'win32'
297+
? projects[0] === realpath(process.cwd())
298+
: projects[0] === process.cwd();
299+
301300
const parsedConfigs = projects
302301
.filter(root => {
303302
// Ignore globbed files that cannot be `require`d.
@@ -313,9 +312,19 @@ export function readConfigs(
313312

314313
return true;
315314
})
316-
.map((root, projectIndex) =>
317-
readConfig(argv, root, true, configPath, projectIndex),
318-
);
315+
.map((root, projectIndex) => {
316+
const projectIsTheOnlyProject =
317+
projectIndex === 0 && projects.length === 1;
318+
const skipArgvConfigOption = !(projectIsTheOnlyProject && projectIsCwd);
319+
320+
return readConfig(
321+
argv,
322+
root,
323+
skipArgvConfigOption,
324+
configPath,
325+
projectIndex,
326+
);
327+
});
319328

320329
ensureNoDuplicateConfigs(parsedConfigs, projects);
321330
configs = parsedConfigs.map(({projectConfig}) => projectConfig);

0 commit comments

Comments
 (0)