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

fix(read-config): allow multiple projects with programmatic usage #11307

Merged
merged 14 commits into from Apr 23, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -47,6 +47,7 @@
- `[jest-circus]` Fix `testLocation` on Windows when using `test.each` ([#10871](https://github.com/facebook/jest/pull/10871))
- `[jest-cli]` Use testFailureExitCode when bailing from a failed test ([#10958](https://github.com/facebook/jest/pull/10958))
- `[jest-cli]` Print custom error if error thrown from global hooks is not an error already ([#11003](https://github.com/facebook/jest/pull/11003))
- `[jest-cli]` Allow running multiple "projects" from programmatic api ([#11307](https://github.com/facebook/jest/pull/11307))
nicojs marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-config]` [**BREAKING**] Change default file extension order by moving json behind ts and tsx ([10572](https://github.com/facebook/jest/pull/10572))
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
- `[jest-core]` Don't report PerformanceObserver as open handle ([#11123](https://github.com/facebook/jest/pull/11123))
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/runProgrammaticallyMultipleProjects.test.ts
@@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {resolve} from 'path';
import {run} from '../Utils';

const dir = resolve(__dirname, '..', 'run-programmatically-multiple-projects');
nicojs marked this conversation as resolved.
Show resolved Hide resolved

test('run programmatically with multiple projects', () => {
const {stdout, exitCode} = run(`node run-jest.js `, dir);
expect(exitCode).toEqual(0);
expect(stdout).toMatch(/Done/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you pass stdout through extractSummaries or something like that and snapshot it to ensure both tests show up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great addition, will do that and use snapshotting 👍

});
@@ -0,0 +1,3 @@
describe('client', () => {
it('should work', () => {});
});
6 changes: 6 additions & 0 deletions e2e/run-programmatically-multiple-projects/package.json
@@ -0,0 +1,6 @@
{
"name": "runcli-multiple-projects",
"version": "1.0.0",
"dependencies": {},
"jest": {}
}
15 changes: 15 additions & 0 deletions e2e/run-programmatically-multiple-projects/run-jest.js
@@ -0,0 +1,15 @@
const {runCLI} = require('../../packages/jest/build/jest');
nicojs marked this conversation as resolved.
Show resolved Hide resolved

const config = {
projects: [
{testMatch: ['<rootDir>/client/**/*.test.js']},
{testMatch: ['<rootDir>/server/**/*.test.js']},
],
};

runCLI({config: JSON.stringify(config)}, [process.cwd()])
.then(() => console.log('✅ Done'))
SimenB marked this conversation as resolved.
Show resolved Hide resolved
.catch(err => {
console.error(err);
process.exitCode = 1;
});
@@ -0,0 +1,3 @@
describe('server', () => {
it('should work', () => {});
});
14 changes: 6 additions & 8 deletions packages/jest-config/src/index.ts
Expand Up @@ -38,7 +38,7 @@ export async function readConfig(
// `project` property, we don't want to read `--config` value and rather
// read individual configs for every project.
skipArgvConfigOption?: boolean,
parentConfigPath?: Config.Path | null,
parentConfigDirname?: Config.Path | null,
projectIndex: number = Infinity,
): Promise<ReadConfig> {
let rawOptions:
Expand All @@ -47,8 +47,7 @@ export async function readConfig(
let configPath = null;

if (typeof packageRootOrConfig !== 'string') {
if (parentConfigPath) {
const parentConfigDirname = path.dirname(parentConfigPath);
if (parentConfigDirname) {
rawOptions = packageRootOrConfig;
rawOptions.rootDir = rawOptions.rootDir
? replaceRootDirInPath(parentConfigDirname, rawOptions.rootDir)
Expand Down Expand Up @@ -301,10 +300,9 @@ export async function readConfigs(
}

if (projects.length > 0) {
const projectIsCwd =
process.platform === 'win32'
? projects[0] === tryRealpath(process.cwd())
: projects[0] === process.cwd();
const cwd =
process.platform === 'win32' ? tryRealpath(process.cwd()) : process.cwd();
const projectIsCwd = projects[0] === cwd;

const parsedConfigs = await Promise.all(
projects
Expand Down Expand Up @@ -332,7 +330,7 @@ export async function readConfigs(
argv,
root,
skipArgvConfigOption,
configPath,
configPath ? path.dirname(configPath) : cwd,
projectIndex,
);
}),
Expand Down