Skip to content

Commit

Permalink
wip: runProjects
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed Nov 23, 2019
1 parent 36be0ba commit 8c6078b
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CLI.md
Expand Up @@ -248,6 +248,10 @@ Run tests with specified reporters. [Reporter options](configuration#reporters-a

Alias: `-i`. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging.

### `--runProjects <project1> ... <projectN>`

Run only the tests of the specified projects. Jest uses the attribute `name` in the configuration to identify each project.

### `--runTestsByPath`

Run only the tests that were specified with their exact paths.
Expand Down
60 changes: 60 additions & 0 deletions e2e/__tests__/runProjects.test.ts
@@ -0,0 +1,60 @@
/**
* 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 {json as runWithJson} from '../runJest';

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

test('run first project when runProjects is [first-project]', () => {
const {json} = runWithJson('run-projects', [
`--runProjects`,
'first-project',
]);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBe(1);
expect(json.testResults.map(({name}) => name)).toEqual([
resolve(dir, '__tests__/first-project.test.js'),
]);
});

test('run second project when runProjects is [second-project]', () => {
const {json} = runWithJson('run-projects', [
'--runProjects',
'second-project',
]);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBe(1);
expect(json.testResults.map(({name}) => name)).toEqual([
resolve(dir, '__tests__/second-project.test.js'),
]);
});

test('run first and second project when runProjects is [first-project, second-project]', () => {
const {json} = runWithJson('run-projects', [
'--runProjects',
'first-project',
'second-project',
]);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBe(2);
expect(json.testResults.map(({name}) => name).sort()).toEqual([
resolve(dir, '__tests__/first-project.test.js'),
resolve(dir, '__tests__/second-project.test.js'),
]);
});

test('run first and second project when runProjects is not specified', () => {
const {json} = runWithJson('run-projects', []);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBe(2);
expect(json.testResults.map(({name}) => name).sort()).toEqual([
resolve(dir, '__tests__/first-project.test.js'),
resolve(dir, '__tests__/second-project.test.js'),
]);
});
10 changes: 10 additions & 0 deletions e2e/run-projects/__tests__/first-project.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.
*/

it('should run when first-project appears in runProjects', () => {
expect(true).toBe(true);
});
10 changes: 10 additions & 0 deletions e2e/run-projects/__tests__/second-project.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.
*/

it('should run when second-project appears in runProjects', () => {
expect(true).toBe(true);
});
20 changes: 20 additions & 0 deletions e2e/run-projects/package.json
@@ -0,0 +1,20 @@
{
"jest": {
"projects": [
{
"name": "first-project",
"testMatch": [
"<rootDir>/__tests__/first-project.test.js"
],
"testEnvironment": "node"
},
{
"name": "second-project",
"testMatch": [
"<rootDir>/__tests__/second-project.test.js"
],
"testEnvironment": "node"
}
]
}
}
7 changes: 7 additions & 0 deletions packages/jest-cli/src/__tests__/cli/args.test.ts
Expand Up @@ -59,6 +59,13 @@ describe('check', () => {
expect(() => check(argv)).not.toThrow();
});

it('raises an exception if runProjects is not provided any project names', () => {
const argv: Config.Argv = {runProjects: []} as Config.Argv;
expect(() => check(argv)).toThrow(
'The --runProjects option requires the name of at least one project to be specified.\n',
);
});

it('raises an exception if config is not a valid JSON string', () => {
const argv = {config: 'x:1'} as Config.Argv;
expect(() => check(argv)).toThrow(
Expand Down
14 changes: 14 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -49,6 +49,13 @@ export const check = (argv: Config.Argv) => {
);
}

if (argv.runProjects && argv.runProjects.length === 0) {
throw new Error(
'The --runProjects option requires the name of at least one project to be specified.\n' +
'Example usage: jest --runProjects my-first-project my-second-project',
);
}

if (
argv.config &&
!isJSONString(argv.config) &&
Expand Down Expand Up @@ -503,6 +510,13 @@ export const options = {
'rare.',
type: 'boolean',
},
runProjects: {
description:
'Run only the tests of the specified projects.' +
'Jest uses the attribute `name` in the configuration to identify each project.',
string: true,
type: 'array',
},
runTestsByPath: {
default: false,
description:
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-core/src/cli/index.ts
Expand Up @@ -19,6 +19,7 @@ import exit = require('exit');
import {Filter} from '../types';
import createContext from '../lib/create_context';
import getChangedFilesPromise from '../getChangedFilesPromise';
import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun';
import {formatHandleErrors} from '../collectHandles';
import handleDeprecationWarnings from '../lib/handle_deprecation_warnings';
import runJest from '../runJest';
Expand Down Expand Up @@ -72,9 +73,11 @@ export const runCLI = async (
exit(0);
}

const configsOfProjectsToRun = getConfigsOfProjectsToRun(argv, configs);

await _run(
globalConfig,
configs,
configsOfProjectsToRun,
hasDeprecationWarnings,
outputStream,
r => (results = r),
Expand Down
19 changes: 19 additions & 0 deletions packages/jest-core/src/getConfigsOfProjectsToRun.ts
@@ -0,0 +1,19 @@
/**
* 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 {Config} from '@jest/types';

export default function getConfigsOfProjectsToRun(
argv: Config.Argv,
projectConfigs: Array<Config.ProjectConfig>,
): Array<Config.ProjectConfig> {
if (!argv.runProjects) {
return projectConfigs;
}
const namesOfProjectsToRun = new Set(argv.runProjects);
return projectConfigs.filter(config => namesOfProjectsToRun.has(config.name));
}
1 change: 1 addition & 0 deletions packages/jest-types/src/Config.ts
Expand Up @@ -407,6 +407,7 @@ export type Argv = Arguments<
rootDir: string;
roots: Array<string>;
runInBand: boolean;
runProjects: Array<string>;
setupFiles: Array<string>;
setupFilesAfterEnv: Array<string>;
showConfig: boolean;
Expand Down

0 comments on commit 8c6078b

Please sign in to comment.