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 @@ -50,6 +50,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))
- `[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
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`run programmatically with multiple projects: summary 1`] = `
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites in 2 projects.
`;
20 changes: 20 additions & 0 deletions e2e/__tests__/runProgrammaticallyMultipleProjects.test.ts
@@ -0,0 +1,20 @@
/**
* 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 {wrap} from 'jest-snapshot-serializer-raw';
import stripAnsi = require('strip-ansi');
import {extractSummary, run} from '../Utils';

const dir = resolve(__dirname, '../run-programmatically-multiple-projects');

test('run programmatically with multiple projects', () => {
const {stderr, exitCode} = run(`node run-jest.js`, dir);
const {summary} = extractSummary(stripAnsi(stderr));
expect(exitCode).toEqual(0);
expect(wrap(summary)).toMatchSnapshot('summary');
});
10 changes: 10 additions & 0 deletions e2e/run-programmatically-multiple-projects/client/client.test.js
@@ -0,0 +1,10 @@
/**
* 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.
*/

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": {}
}
22 changes: 22 additions & 0 deletions e2e/run-programmatically-multiple-projects/run-jest.js
@@ -0,0 +1,22 @@
/**
* 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.
*/

const {runCLI} = require('@jest/core');

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

runCLI({config: JSON.stringify(config)}, [process.cwd()])
.then(() => console.log('run-programmatically-mutiple-projects completed'))
.catch(err => {
console.error(err);
process.exitCode = 1;
});
10 changes: 10 additions & 0 deletions e2e/run-programmatically-multiple-projects/server/server.test.js
@@ -0,0 +1,10 @@
/**
* 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.
*/

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