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 b067bc6
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
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`

Alias: `-p . TODO: runProjects

### `--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
Original file line number Diff line number Diff line change
@@ -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'),
]);
});
3 changes: 3 additions & 0 deletions e2e/run-projects/__tests__/first-project.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('should run when first-project appears in runProjects', () => {
expect(true).toBe(true);
});
3 changes: 3 additions & 0 deletions e2e/run-projects/__tests__/second-project.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('should run when second-project appears in runProjects', () => {
expect(true).toBe(true);
});
16 changes: 16 additions & 0 deletions e2e/run-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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"
}
]
}
}
4 changes: 4 additions & 0 deletions packages/jest-cli/src/__tests__/cli/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ describe('check', () => {
expect(() => check(argv)).not.toThrow();
});

it('raises an exception if runProjects is not provided any project names', () => {
expect('TODO: runProjects').toBeDefined();
});

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
12 changes: 12 additions & 0 deletions packages/jest-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export const check = (argv: Config.Argv) => {
);
}

if (argv.runProjects && argv.runProjects.length === 0) {
// Must throw if no value
throw new Error('TODO: runProjects');
}

if (
argv.config &&
!isJSONString(argv.config) &&
Expand Down Expand Up @@ -503,6 +508,13 @@ export const options = {
'rare.',
type: 'boolean',
},
runProjects: {
alias: 'p',
// TODO: write a proper description
description: 'TODO',
string: true as true,
type: 'array' as 'array',
},
runTestsByPath: {
default: false,
description:
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-core/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ describe('Watch mode flows', () => {
).rejects.toMatchSnapshot();
});

// TODO: Figure out what to do with `runProjects` here
// (and maybe `name`)
it.each`
ok | option
${'✔︎'} | ${'bail'}
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-core/src/cli/index.ts
Original file line number Diff line number Diff line change
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
12 changes: 12 additions & 0 deletions packages/jest-core/src/getConfigsOfProjectsToRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
Original file line number Diff line number Diff line change
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 b067bc6

Please sign in to comment.