From b067bc65597650e41e47c8992eb87dd26d06bced Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Wed, 26 Jun 2019 23:49:22 +0200 Subject: [PATCH] wip: runProjects --- docs/CLI.md | 4 ++ e2e/__tests__/runProjects.test.ts | 60 +++++++++++++++++++ .../__tests__/first-project.test.js | 3 + .../__tests__/second-project.test.js | 3 + e2e/run-projects/package.json | 16 +++++ .../jest-cli/src/__tests__/cli/args.test.ts | 4 ++ packages/jest-cli/src/cli/args.ts | 12 ++++ .../jest-core/src/__tests__/watch.test.js | 2 + packages/jest-core/src/cli/index.ts | 5 +- .../src/getConfigsOfProjectsToRun.ts | 12 ++++ packages/jest-types/src/Config.ts | 1 + 11 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 e2e/__tests__/runProjects.test.ts create mode 100644 e2e/run-projects/__tests__/first-project.test.js create mode 100644 e2e/run-projects/__tests__/second-project.test.js create mode 100644 e2e/run-projects/package.json create mode 100644 packages/jest-core/src/getConfigsOfProjectsToRun.ts diff --git a/docs/CLI.md b/docs/CLI.md index 315b2de3c0a2..a3fff8a12093 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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. diff --git a/e2e/__tests__/runProjects.test.ts b/e2e/__tests__/runProjects.test.ts new file mode 100644 index 000000000000..a14135418146 --- /dev/null +++ b/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'), + ]); +}); diff --git a/e2e/run-projects/__tests__/first-project.test.js b/e2e/run-projects/__tests__/first-project.test.js new file mode 100644 index 000000000000..82e75432e134 --- /dev/null +++ b/e2e/run-projects/__tests__/first-project.test.js @@ -0,0 +1,3 @@ +it('should run when first-project appears in runProjects', () => { + expect(true).toBe(true); +}); diff --git a/e2e/run-projects/__tests__/second-project.test.js b/e2e/run-projects/__tests__/second-project.test.js new file mode 100644 index 000000000000..09d3b1304e1c --- /dev/null +++ b/e2e/run-projects/__tests__/second-project.test.js @@ -0,0 +1,3 @@ +it('should run when second-project appears in runProjects', () => { + expect(true).toBe(true); +}); diff --git a/e2e/run-projects/package.json b/e2e/run-projects/package.json new file mode 100644 index 000000000000..b70e407cda1a --- /dev/null +++ b/e2e/run-projects/package.json @@ -0,0 +1,16 @@ +{ + "jest": { + "projects": [ + { + "name": "first-project", + "testMatch": ["/__tests__/first-project.test.js"], + "testEnvironment": "node" + }, + { + "name": "second-project", + "testMatch": ["/__tests__/second-project.test.js"], + "testEnvironment": "node" + } + ] + } +} diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index dfe1c80b4f02..b130726eb049 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -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( diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 6bc9650b7a01..d7ec0baf1bd7 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -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) && @@ -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: diff --git a/packages/jest-core/src/__tests__/watch.test.js b/packages/jest-core/src/__tests__/watch.test.js index 84e55a43dee0..8bebcb07e34e 100644 --- a/packages/jest-core/src/__tests__/watch.test.js +++ b/packages/jest-core/src/__tests__/watch.test.js @@ -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'} diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index ad2f2e2bea73..131d30c2efec 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -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'; @@ -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), diff --git a/packages/jest-core/src/getConfigsOfProjectsToRun.ts b/packages/jest-core/src/getConfigsOfProjectsToRun.ts new file mode 100644 index 000000000000..622c46841252 --- /dev/null +++ b/packages/jest-core/src/getConfigsOfProjectsToRun.ts @@ -0,0 +1,12 @@ +import {Config} from '@jest/types'; + +export default function getConfigsOfProjectsToRun( + argv: Config.Argv, + projectConfigs: Array, +): Array { + if (!argv.runProjects) { + return projectConfigs; + } + const namesOfProjectsToRun = new Set(argv.runProjects); + return projectConfigs.filter(config => namesOfProjectsToRun.has(config.name)); +} diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index f498f8f843a3..50bcb10b6c6c 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -407,6 +407,7 @@ export type Argv = Arguments< rootDir: string; roots: Array; runInBand: boolean; + runProjects: Array; setupFiles: Array; setupFilesAfterEnv: Array; showConfig: boolean;