From a3a0627020ee71118d71f3e83d314f168eed546f Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Wed, 26 Jun 2019 23:49:22 +0200 Subject: [PATCH 01/19] runProjects cli option --- docs/CLI.md | 4 ++ e2e/__tests__/runProjects.test.ts | 60 +++++++++++++++++++ .../__tests__/first-project.test.js | 10 ++++ .../__tests__/second-project.test.js | 10 ++++ e2e/run-projects/package.json | 20 +++++++ .../jest-cli/src/__tests__/cli/args.test.ts | 7 +++ packages/jest-cli/src/cli/args.ts | 14 +++++ packages/jest-core/src/cli/index.ts | 5 +- .../src/getConfigsOfProjectsToRun.ts | 19 ++++++ packages/jest-types/src/Config.ts | 1 + 10 files changed, 149 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 a6ed467c95d0..11aa1088fa47 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -254,6 +254,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 ... ` + +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. 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..97565d91543e --- /dev/null +++ b/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); +}); 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..4f7d1f98a99f --- /dev/null +++ b/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); +}); diff --git a/e2e/run-projects/package.json b/e2e/run-projects/package.json new file mode 100644 index 000000000000..c6aeb31cbda5 --- /dev/null +++ b/e2e/run-projects/package.json @@ -0,0 +1,20 @@ +{ + "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 8e732954b771..2bbf50f8cb57 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -72,6 +72,13 @@ describe('check', () => { }, ); + 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( diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index b368e8a5830d..2e368900200a 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -49,6 +49,13 @@ export function check(argv: Config.Argv): true { ); } + 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) && @@ -515,6 +522,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: diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index dd8b469e7624..19b505c1190b 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 type {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'; @@ -68,9 +69,11 @@ export async function runCLI( 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..f396d17720d2 --- /dev/null +++ b/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, +): 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 cf895c67c1a8..ee3581386ab7 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -408,6 +408,7 @@ export type Argv = Arguments< rootDir: string; roots: Array; runInBand: boolean; + runProjects: Array; setupFiles: Array; setupFilesAfterEnv: Array; showConfig: boolean; From 206c4f45f46ab7f5e41852b7cc183e837a4c6e18 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sat, 23 Nov 2019 14:26:32 +0100 Subject: [PATCH 02/19] add message --- e2e/__tests__/runProjects.test.ts | 110 +++++++++++------- packages/jest-core/src/cli/index.ts | 7 +- .../src/getProjectsRunningMessage.ts | 23 ++++ 3 files changed, 98 insertions(+), 42 deletions(-) create mode 100644 packages/jest-core/src/getProjectsRunningMessage.ts diff --git a/e2e/__tests__/runProjects.test.ts b/e2e/__tests__/runProjects.test.ts index a14135418146..45b9a2313d6f 100644 --- a/e2e/__tests__/runProjects.test.ts +++ b/e2e/__tests__/runProjects.test.ts @@ -7,54 +7,82 @@ import {resolve} from 'path'; -import {json as runWithJson} from '../runJest'; +import {RunJestJsonResult, 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'), - ]); +describe('when Jest is started with `--runProjects first-project`', () => { + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('run-projects', [`--runProjects`, 'first-project']); + }); + it('runs the tests in the first project only', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(1); + expect(result.json.testResults.map(({name}) => name)).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + ]); + }); + it('prints that only first-project will run', () => { + expect(result.stderr).toMatch(/^Will run one project: first-project/); + }); }); -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'), - ]); +describe('when Jest is started with `--runProjects second-project`', () => { + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('run-projects', [`--runProjects`, 'second-project']); + }); + it('runs the tests in the second project only', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(1); + expect(result.json.testResults.map(({name}) => name)).toEqual([ + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('prints that only second-project will run', () => { + expect(result.stderr).toMatch(/^Will run one project: second-project/); + }); }); -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'), - ]); +describe('when Jest is started with `--runProjects first-project second-project`', () => { + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('run-projects', [ + `--runProjects`, + 'first-project', + 'second-project', + ]); + }); + it('runs the tests in the first and second projects', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(2); + expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('prints that both first-project and second-project will run', () => { + expect(result.stderr).toMatch( + /^Will run 2 projects:\n- first-project\n- second-project/, + ); + }); }); -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'), - ]); +describe('when Jest is started without providing `--runProjects`', () => { + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('run-projects', []); + }); + it('runs the tests in the first and second projects', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(2); + expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('does not print which projects are run', () => { + expect(result.stderr).not.toMatch(/^Will run/); + }); }); diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index 19b505c1190b..634c4bfa298e 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -19,7 +19,6 @@ import exit = require('exit'); import type {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'; @@ -27,6 +26,8 @@ import TestWatcher from '../TestWatcher'; import watch from '../watch'; import pluralize from '../pluralize'; import logDebugMessages from '../lib/log_debug_messages'; +import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun'; +import getProjectsRunningMessage from '../getProjectsRunningMessage'; const {print: preRunMessagePrint} = preRunMessage; @@ -70,6 +71,10 @@ export async function runCLI( } const configsOfProjectsToRun = getConfigsOfProjectsToRun(argv, configs); + if (argv.runProjects) { + const projectNames = configsOfProjectsToRun.map(config => config.name); + outputStream.write(getProjectsRunningMessage(projectNames) + '\n'); + } await _run( globalConfig, diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts new file mode 100644 index 000000000000..2b659a80a7aa --- /dev/null +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -0,0 +1,23 @@ +/** + * 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. + */ + +export default function getProjectsRunningMessage( + projectNames: Array, +): string { + if (projectNames.length === 0) { + return 'No project to run'; + } + if (projectNames.length === 1) { + return `Will run one project: ${projectNames[0]}`; + } + const projectsList = projectNames.map(getListElement).join('\n'); + return `Will run ${projectNames.length} projects:\n` + projectsList; +} + +function getListElement(content: string): string { + return `- ${content}`; +} From 57d17dbe4a5b0a7354f89209547b3c29614e754e Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sat, 23 Nov 2019 14:56:31 +0100 Subject: [PATCH 03/19] sort project names --- packages/jest-core/src/getProjectsRunningMessage.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index 2b659a80a7aa..2b39f1d26134 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -14,7 +14,10 @@ export default function getProjectsRunningMessage( if (projectNames.length === 1) { return `Will run one project: ${projectNames[0]}`; } - const projectsList = projectNames.map(getListElement).join('\n'); + const projectsList = projectNames + .map(getListElement) + .sort() + .join('\n'); return `Will run ${projectNames.length} projects:\n` + projectsList; } From 92b68b7dbdd75190496d702b66555c55f872a265 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sun, 8 Dec 2019 12:40:27 +0100 Subject: [PATCH 04/19] use displayName instead of name --- docs/CLI.md | 2 +- e2e/run-projects/package.json | 7 +++-- packages/jest-core/src/cli/index.ts | 5 ++-- .../src/getConfigsOfProjectsToRun.ts | 8 ++++-- .../jest-core/src/getProjectDisplayName.ts | 24 +++++++++++++++++ .../src/getProjectsRunningMessage.ts | 26 ++++++++++++------- 6 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 packages/jest-core/src/getProjectDisplayName.ts diff --git a/docs/CLI.md b/docs/CLI.md index 11aa1088fa47..06fa0376dc75 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -256,7 +256,7 @@ Alias: `-i`. Run all tests serially in the current process, rather than creating ### `--runProjects ... ` -Run only the tests of the specified projects. Jest uses the attribute `name` in the configuration to identify each project. +Run only the tests of the specified projects. Jest uses the attribute `displayName` in the configuration to identify each project. ### `--runTestsByPath` diff --git a/e2e/run-projects/package.json b/e2e/run-projects/package.json index c6aeb31cbda5..564c775b7cac 100644 --- a/e2e/run-projects/package.json +++ b/e2e/run-projects/package.json @@ -2,14 +2,17 @@ "jest": { "projects": [ { - "name": "first-project", + "displayName": "first-project", "testMatch": [ "/__tests__/first-project.test.js" ], "testEnvironment": "node" }, { - "name": "second-project", + "displayName": { + "name": "second-project", + "color": "blue" + }, "testMatch": [ "/__tests__/second-project.test.js" ], diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index 634c4bfa298e..9236fb432282 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -72,8 +72,9 @@ export async function runCLI( const configsOfProjectsToRun = getConfigsOfProjectsToRun(argv, configs); if (argv.runProjects) { - const projectNames = configsOfProjectsToRun.map(config => config.name); - outputStream.write(getProjectsRunningMessage(projectNames) + '\n'); + outputStream.write( + getProjectsRunningMessage(configsOfProjectsToRun) + '\n', + ); } await _run( diff --git a/packages/jest-core/src/getConfigsOfProjectsToRun.ts b/packages/jest-core/src/getConfigsOfProjectsToRun.ts index f396d17720d2..977ed2a9357a 100644 --- a/packages/jest-core/src/getConfigsOfProjectsToRun.ts +++ b/packages/jest-core/src/getConfigsOfProjectsToRun.ts @@ -6,6 +6,7 @@ */ import {Config} from '@jest/types'; +import getProjectDisplayName from './getProjectDisplayName'; export default function getConfigsOfProjectsToRun( argv: Config.Argv, @@ -14,6 +15,9 @@ export default function getConfigsOfProjectsToRun( if (!argv.runProjects) { return projectConfigs; } - const namesOfProjectsToRun = new Set(argv.runProjects); - return projectConfigs.filter(config => namesOfProjectsToRun.has(config.name)); + const namesOfProjectsToRun = new Set(argv.runProjects); + return projectConfigs.filter(config => { + const name = getProjectDisplayName(config); + return name && namesOfProjectsToRun.has(name); + }); } diff --git a/packages/jest-core/src/getProjectDisplayName.ts b/packages/jest-core/src/getProjectDisplayName.ts new file mode 100644 index 000000000000..301b331021b8 --- /dev/null +++ b/packages/jest-core/src/getProjectDisplayName.ts @@ -0,0 +1,24 @@ +import {Config} from '@jest/types'; + +/** + * 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. + */ + +export default function getProjectDisplayName( + projectConfig: Config.ProjectConfig, +): string | undefined { + const {displayName} = projectConfig; + if (!displayName) { + return undefined; + } + if (typeof displayName === 'string') { + return displayName; + } + if (typeof displayName === 'object') { + return displayName.name; + } + return undefined; +} diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index 2b39f1d26134..da923424e00c 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -1,3 +1,6 @@ +import {Config} from '@jest/types'; +import getProjectDisplayName from './getProjectDisplayName'; + /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * @@ -6,21 +9,26 @@ */ export default function getProjectsRunningMessage( - projectNames: Array, + projectConfigs: Array, ): string { - if (projectNames.length === 0) { + if (projectConfigs.length === 0) { return 'No project to run'; } - if (projectNames.length === 1) { - return `Will run one project: ${projectNames[0]}`; + if (projectConfigs.length === 1) { + const name = getProjectDisplayName(projectConfigs[0]); + return `Will run one project: ${name}`; } - const projectsList = projectNames - .map(getListElement) + const projectsList = projectConfigs + .map(getProjectNameListElement) .sort() .join('\n'); - return `Will run ${projectNames.length} projects:\n` + projectsList; + return `Will run ${projectConfigs.length} projects:\n` + projectsList; } -function getListElement(content: string): string { - return `- ${content}`; +function getProjectNameListElement( + projectConfig: Config.ProjectConfig, +): string { + const name = getProjectDisplayName(projectConfig); + const elementContent = name || ''; + return `- ${elementContent}`; } From 54362dc396beaa0caecfd3b99077f9d227920be5 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sun, 8 Dec 2019 12:56:42 +0100 Subject: [PATCH 05/19] copyright notice on top --- packages/jest-core/src/getProjectDisplayName.ts | 4 ++-- packages/jest-core/src/getProjectsRunningMessage.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/jest-core/src/getProjectDisplayName.ts b/packages/jest-core/src/getProjectDisplayName.ts index 301b331021b8..87023c3cdda3 100644 --- a/packages/jest-core/src/getProjectDisplayName.ts +++ b/packages/jest-core/src/getProjectDisplayName.ts @@ -1,5 +1,3 @@ -import {Config} from '@jest/types'; - /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * @@ -7,6 +5,8 @@ import {Config} from '@jest/types'; * LICENSE file in the root directory of this source tree. */ +import {Config} from '@jest/types'; + export default function getProjectDisplayName( projectConfig: Config.ProjectConfig, ): string | undefined { diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index da923424e00c..d7ced5fb76d3 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -1,6 +1,3 @@ -import {Config} from '@jest/types'; -import getProjectDisplayName from './getProjectDisplayName'; - /** * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. * @@ -8,6 +5,9 @@ import getProjectDisplayName from './getProjectDisplayName'; * LICENSE file in the root directory of this source tree. */ +import {Config} from '@jest/types'; +import getProjectDisplayName from './getProjectDisplayName'; + export default function getProjectsRunningMessage( projectConfigs: Array, ): string { From 9c5513e724841d1cef94909a7e3711382b6a89f8 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sun, 8 Dec 2019 13:00:55 +0100 Subject: [PATCH 06/19] changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2471a370e38..305e6dd836cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -265,6 +265,7 @@ - `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382)) - `[expect, jest-matcher-utils]` Display change counts in annotation lines ([#9035](https://github.com/facebook/jest/pull/9035)) - `[expect, jest-snapshot]` Support custom inline snapshot matchers ([#9278](https://github.com/facebook/jest/pull/9278)) +- `[jest-cli, jest-core]` Add `--runProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) - `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924)) - `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689)) - `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027)) From af1c2212019008dbf2ddc4174e9c78f3fde734d2 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sun, 8 Dec 2019 21:11:34 +0100 Subject: [PATCH 07/19] correct name -> displayName in description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bazyli Brzóska --- packages/jest-cli/src/cli/args.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 2e368900200a..06e36485a305 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -525,7 +525,7 @@ export const options = { runProjects: { description: 'Run only the tests of the specified projects.' + - 'Jest uses the attribute `name` in the configuration to identify each project.', + 'Jest uses the attribute `displayName` in the configuration to identify each project.', string: true, type: 'array', }, From b73f7e45e989e43a2168856a37399792ac430310 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Mon, 9 Dec 2019 09:37:02 +0100 Subject: [PATCH 08/19] Will run -> Running (1) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Michał Pierzchała --- packages/jest-core/src/getProjectsRunningMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index d7ced5fb76d3..4e2a1bce9d54 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -16,7 +16,7 @@ export default function getProjectsRunningMessage( } if (projectConfigs.length === 1) { const name = getProjectDisplayName(projectConfigs[0]); - return `Will run one project: ${name}`; + return `Running one project: ${name}`; } const projectsList = projectConfigs .map(getProjectNameListElement) From e8c78747cf5f8a3514770ffa678816246738fb8b Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Mon, 9 Dec 2019 09:37:18 +0100 Subject: [PATCH 09/19] Will run -> Running (2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Michał Pierzchała --- packages/jest-core/src/getProjectsRunningMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index 4e2a1bce9d54..1062bbf37652 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -22,7 +22,7 @@ export default function getProjectsRunningMessage( .map(getProjectNameListElement) .sort() .join('\n'); - return `Will run ${projectConfigs.length} projects:\n` + projectsList; + return `Running ${projectConfigs.length} projects:\n` + projectsList; } function getProjectNameListElement( From 1cd4d1a28f7d9898941005779c849d09482f7109 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Thu, 7 May 2020 21:50:11 +0200 Subject: [PATCH 10/19] Make CI pass --- e2e/__tests__/runProjects.test.ts | 44 ++++++++----------- .../src/getConfigsOfProjectsToRun.ts | 2 +- .../jest-core/src/getProjectDisplayName.ts | 2 +- .../src/getProjectsRunningMessage.ts | 2 +- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/e2e/__tests__/runProjects.test.ts b/e2e/__tests__/runProjects.test.ts index 45b9a2313d6f..b9bea78aa1c6 100644 --- a/e2e/__tests__/runProjects.test.ts +++ b/e2e/__tests__/runProjects.test.ts @@ -7,15 +7,15 @@ import {resolve} from 'path'; -import {RunJestJsonResult, json as runWithJson} from '../runJest'; +import {json as runWithJson} from '../runJest'; const dir = resolve(__dirname, '..', 'run-projects'); describe('when Jest is started with `--runProjects first-project`', () => { - let result: RunJestJsonResult; - beforeAll(() => { - result = runWithJson('run-projects', [`--runProjects`, 'first-project']); - }); + const result = runWithJson('run-projects', [ + '--runProjects', + 'first-project', + ]); it('runs the tests in the first project only', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(1); @@ -24,15 +24,15 @@ describe('when Jest is started with `--runProjects first-project`', () => { ]); }); it('prints that only first-project will run', () => { - expect(result.stderr).toMatch(/^Will run one project: first-project/); + expect(result.stderr).toMatch(/^Running one project: first-project/); }); }); describe('when Jest is started with `--runProjects second-project`', () => { - let result: RunJestJsonResult; - beforeAll(() => { - result = runWithJson('run-projects', [`--runProjects`, 'second-project']); - }); + const result = runWithJson('run-projects', [ + `--runProjects`, + 'second-project', + ]); it('runs the tests in the second project only', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(1); @@ -41,19 +41,16 @@ describe('when Jest is started with `--runProjects second-project`', () => { ]); }); it('prints that only second-project will run', () => { - expect(result.stderr).toMatch(/^Will run one project: second-project/); + expect(result.stderr).toMatch(/^Running one project: second-project/); }); }); describe('when Jest is started with `--runProjects first-project second-project`', () => { - let result: RunJestJsonResult; - beforeAll(() => { - result = runWithJson('run-projects', [ - `--runProjects`, - 'first-project', - 'second-project', - ]); - }); + const result = runWithJson('run-projects', [ + `--runProjects`, + 'first-project', + 'second-project', + ]); it('runs the tests in the first and second projects', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(2); @@ -64,16 +61,13 @@ describe('when Jest is started with `--runProjects first-project second-project` }); it('prints that both first-project and second-project will run', () => { expect(result.stderr).toMatch( - /^Will run 2 projects:\n- first-project\n- second-project/, + /^Running 2 projects:\n- first-project\n- second-project/, ); }); }); describe('when Jest is started without providing `--runProjects`', () => { - let result: RunJestJsonResult; - beforeAll(() => { - result = runWithJson('run-projects', []); - }); + const result = runWithJson('run-projects', []); it('runs the tests in the first and second projects', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(2); @@ -83,6 +77,6 @@ describe('when Jest is started without providing `--runProjects`', () => { ]); }); it('does not print which projects are run', () => { - expect(result.stderr).not.toMatch(/^Will run/); + expect(result.stderr).not.toMatch(/^Running/); }); }); diff --git a/packages/jest-core/src/getConfigsOfProjectsToRun.ts b/packages/jest-core/src/getConfigsOfProjectsToRun.ts index 977ed2a9357a..7defdb25efd9 100644 --- a/packages/jest-core/src/getConfigsOfProjectsToRun.ts +++ b/packages/jest-core/src/getConfigsOfProjectsToRun.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {Config} from '@jest/types'; +import type {Config} from '@jest/types'; import getProjectDisplayName from './getProjectDisplayName'; export default function getConfigsOfProjectsToRun( diff --git a/packages/jest-core/src/getProjectDisplayName.ts b/packages/jest-core/src/getProjectDisplayName.ts index 87023c3cdda3..24f55d6746ea 100644 --- a/packages/jest-core/src/getProjectDisplayName.ts +++ b/packages/jest-core/src/getProjectDisplayName.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {Config} from '@jest/types'; +import type {Config} from '@jest/types'; export default function getProjectDisplayName( projectConfig: Config.ProjectConfig, diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts index 1062bbf37652..fd13478d2e8f 100644 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ b/packages/jest-core/src/getProjectsRunningMessage.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {Config} from '@jest/types'; +import type {Config} from '@jest/types'; import getProjectDisplayName from './getProjectDisplayName'; export default function getProjectsRunningMessage( From d3180230066d88515ceb21f95a23186c9f4fca03 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Thu, 7 May 2020 23:34:52 +0200 Subject: [PATCH 11/19] Print warnings and stuff --- packages/jest-core/src/cli/index.ts | 8 +-- .../src/getProjectsRunningMessage.ts | 34 ---------- .../jest-core/src/getSelectProjectsMessage.ts | 64 +++++++++++++++++++ 3 files changed, 67 insertions(+), 39 deletions(-) delete mode 100644 packages/jest-core/src/getProjectsRunningMessage.ts create mode 100644 packages/jest-core/src/getSelectProjectsMessage.ts diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index 9236fb432282..c555523a3c15 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -27,7 +27,7 @@ import watch from '../watch'; import pluralize from '../pluralize'; import logDebugMessages from '../lib/log_debug_messages'; import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun'; -import getProjectsRunningMessage from '../getProjectsRunningMessage'; +import getSelectProjectsMessage from '../getSelectProjectsMessage'; const {print: preRunMessagePrint} = preRunMessage; @@ -71,10 +71,8 @@ export async function runCLI( } const configsOfProjectsToRun = getConfigsOfProjectsToRun(argv, configs); - if (argv.runProjects) { - outputStream.write( - getProjectsRunningMessage(configsOfProjectsToRun) + '\n', - ); + if (argv.selectProjects) { + outputStream.write(getSelectProjectsMessage(configsOfProjectsToRun)); } await _run( diff --git a/packages/jest-core/src/getProjectsRunningMessage.ts b/packages/jest-core/src/getProjectsRunningMessage.ts deleted file mode 100644 index fd13478d2e8f..000000000000 --- a/packages/jest-core/src/getProjectsRunningMessage.ts +++ /dev/null @@ -1,34 +0,0 @@ -/** - * 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 type {Config} from '@jest/types'; -import getProjectDisplayName from './getProjectDisplayName'; - -export default function getProjectsRunningMessage( - projectConfigs: Array, -): string { - if (projectConfigs.length === 0) { - return 'No project to run'; - } - if (projectConfigs.length === 1) { - const name = getProjectDisplayName(projectConfigs[0]); - return `Running one project: ${name}`; - } - const projectsList = projectConfigs - .map(getProjectNameListElement) - .sort() - .join('\n'); - return `Running ${projectConfigs.length} projects:\n` + projectsList; -} - -function getProjectNameListElement( - projectConfig: Config.ProjectConfig, -): string { - const name = getProjectDisplayName(projectConfig); - const elementContent = name || ''; - return `- ${elementContent}`; -} diff --git a/packages/jest-core/src/getSelectProjectsMessage.ts b/packages/jest-core/src/getSelectProjectsMessage.ts new file mode 100644 index 000000000000..e5519083c671 --- /dev/null +++ b/packages/jest-core/src/getSelectProjectsMessage.ts @@ -0,0 +1,64 @@ +/** + * 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 chalk = require('chalk'); +import type {Config} from '@jest/types'; +import getProjectDisplayName from './getProjectDisplayName'; + +export default function getSelectProjectsMessage( + projectConfigs: Array, +): string { + if (projectConfigs.length === 0) { + return getNoSelectionWarning(); + } + const numberOfProjectsWithoutAName = projectConfigs.filter( + config => !getProjectDisplayName(config), + ).length; + return ( + getNamesMissingWarning(numberOfProjectsWithoutAName) + + getProjectsRunningMessage(projectConfigs) + ); +} + +function getNoSelectionWarning(): string { + return chalk.yellow( + 'You provided values for --runProjects but no projects were found matching the selection.\n', + ); +} + +function getNamesMissingWarning(numberOfProjectsWithoutAName: number): string { + return chalk.yellow( + `You provided values for --runProjects but ${ + numberOfProjectsWithoutAName === 1 + ? 'a project has' + : `${numberOfProjectsWithoutAName} projects have` + } no name.\n` + + 'Set displayName in the config of all projects in order to disable this warning.\n', + ); +} + +function getProjectsRunningMessage( + projectConfigs: Array, +): string { + if (projectConfigs.length === 1) { + const name = getProjectDisplayName(projectConfigs[0]); + return `Running one project: ${chalk.bold(name)}\n`; + } + const projectsList = projectConfigs + .map(getProjectNameListElement) + .sort() + .join('\n'); + return `Running ${projectConfigs.length} projects:\n${projectsList}\n`; +} + +function getProjectNameListElement( + projectConfig: Config.ProjectConfig, +): string { + const name = getProjectDisplayName(projectConfig); + const elementContent = name ? chalk.bold(name) : ''; + return `- ${elementContent}`; +} From 1c35afb7a9e7265d9547f031b8ec6e1272824390 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Thu, 7 May 2020 23:35:55 +0200 Subject: [PATCH 12/19] runProjects -> selectProjects --- CHANGELOG.md | 2 +- docs/CLI.md | 2 +- ...rojects.test.ts => selectProjects.test.ts} | 24 +++++++++---------- .../__tests__/first-project.test.js | 2 +- .../__tests__/second-project.test.js | 2 +- .../package.json | 1 + .../jest-cli/src/__tests__/cli/args.test.ts | 6 ++--- packages/jest-cli/src/cli/args.ts | 20 ++++++++-------- .../src/getConfigsOfProjectsToRun.ts | 4 ++-- .../jest-core/src/getSelectProjectsMessage.ts | 4 ++-- packages/jest-types/src/Config.ts | 2 +- 11 files changed, 35 insertions(+), 34 deletions(-) rename e2e/__tests__/{runProjects.test.ts => selectProjects.test.ts} (77%) rename e2e/{run-projects => select-projects}/__tests__/first-project.test.js (77%) rename e2e/{run-projects => select-projects}/__tests__/second-project.test.js (77%) rename e2e/{run-projects => select-projects}/package.json (88%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 305e6dd836cb..60184a323541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -265,7 +265,7 @@ - `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382)) - `[expect, jest-matcher-utils]` Display change counts in annotation lines ([#9035](https://github.com/facebook/jest/pull/9035)) - `[expect, jest-snapshot]` Support custom inline snapshot matchers ([#9278](https://github.com/facebook/jest/pull/9278)) -- `[jest-cli, jest-core]` Add `--runProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) +- `[jest-cli, jest-core]` Add `--selectProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) - `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924)) - `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689)) - `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027)) diff --git a/docs/CLI.md b/docs/CLI.md index 06fa0376dc75..3c1d7b26aa33 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -254,7 +254,7 @@ 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 ... ` +### `--selectProjects ... ` Run only the tests of the specified projects. Jest uses the attribute `displayName` in the configuration to identify each project. diff --git a/e2e/__tests__/runProjects.test.ts b/e2e/__tests__/selectProjects.test.ts similarity index 77% rename from e2e/__tests__/runProjects.test.ts rename to e2e/__tests__/selectProjects.test.ts index b9bea78aa1c6..2b2434ddad2b 100644 --- a/e2e/__tests__/runProjects.test.ts +++ b/e2e/__tests__/selectProjects.test.ts @@ -9,11 +9,11 @@ import {resolve} from 'path'; import {json as runWithJson} from '../runJest'; -const dir = resolve(__dirname, '..', 'run-projects'); +const dir = resolve(__dirname, '..', 'select-projects'); -describe('when Jest is started with `--runProjects first-project`', () => { - const result = runWithJson('run-projects', [ - '--runProjects', +describe('when Jest is started with `--selectProjects first-project`', () => { + const result = runWithJson('select-projects', [ + '--selectProjects', 'first-project', ]); it('runs the tests in the first project only', () => { @@ -28,9 +28,9 @@ describe('when Jest is started with `--runProjects first-project`', () => { }); }); -describe('when Jest is started with `--runProjects second-project`', () => { - const result = runWithJson('run-projects', [ - `--runProjects`, +describe('when Jest is started with `--selectProjects second-project`', () => { + const result = runWithJson('select-projects', [ + `--selectProjects`, 'second-project', ]); it('runs the tests in the second project only', () => { @@ -45,9 +45,9 @@ describe('when Jest is started with `--runProjects second-project`', () => { }); }); -describe('when Jest is started with `--runProjects first-project second-project`', () => { - const result = runWithJson('run-projects', [ - `--runProjects`, +describe('when Jest is started with `--selectProjects first-project second-project`', () => { + const result = runWithJson('select-projects', [ + `--selectProjects`, 'first-project', 'second-project', ]); @@ -66,8 +66,8 @@ describe('when Jest is started with `--runProjects first-project second-project` }); }); -describe('when Jest is started without providing `--runProjects`', () => { - const result = runWithJson('run-projects', []); +describe('when Jest is started without providing `--selectProjects`', () => { + const result = runWithJson('select-projects', []); it('runs the tests in the first and second projects', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(2); diff --git a/e2e/run-projects/__tests__/first-project.test.js b/e2e/select-projects/__tests__/first-project.test.js similarity index 77% rename from e2e/run-projects/__tests__/first-project.test.js rename to e2e/select-projects/__tests__/first-project.test.js index 97565d91543e..f440fcd752c5 100644 --- a/e2e/run-projects/__tests__/first-project.test.js +++ b/e2e/select-projects/__tests__/first-project.test.js @@ -5,6 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -it('should run when first-project appears in runProjects', () => { +it('should run when first-project appears in selectProjects', () => { expect(true).toBe(true); }); diff --git a/e2e/run-projects/__tests__/second-project.test.js b/e2e/select-projects/__tests__/second-project.test.js similarity index 77% rename from e2e/run-projects/__tests__/second-project.test.js rename to e2e/select-projects/__tests__/second-project.test.js index 4f7d1f98a99f..c3db24a8b9a6 100644 --- a/e2e/run-projects/__tests__/second-project.test.js +++ b/e2e/select-projects/__tests__/second-project.test.js @@ -5,6 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -it('should run when second-project appears in runProjects', () => { +it('should run when second-project appears in selectProjects', () => { expect(true).toBe(true); }); diff --git a/e2e/run-projects/package.json b/e2e/select-projects/package.json similarity index 88% rename from e2e/run-projects/package.json rename to e2e/select-projects/package.json index 564c775b7cac..28e0bcc45e07 100644 --- a/e2e/run-projects/package.json +++ b/e2e/select-projects/package.json @@ -1,4 +1,5 @@ { + "description": "Testing the behaviour of --selectProjects", "jest": { "projects": [ { diff --git a/packages/jest-cli/src/__tests__/cli/args.test.ts b/packages/jest-cli/src/__tests__/cli/args.test.ts index 2bbf50f8cb57..46ff7a7e4aed 100644 --- a/packages/jest-cli/src/__tests__/cli/args.test.ts +++ b/packages/jest-cli/src/__tests__/cli/args.test.ts @@ -72,10 +72,10 @@ describe('check', () => { }, ); - it('raises an exception if runProjects is not provided any project names', () => { - const argv: Config.Argv = {runProjects: []} as Config.Argv; + it('raises an exception if selectProjects is not provided any project names', () => { + const argv: Config.Argv = {selectProjects: []} as Config.Argv; expect(() => check(argv)).toThrow( - 'The --runProjects option requires the name of at least one project to be specified.\n', + 'The --selectProjects option requires the name of at least one project to be specified.\n', ); }); diff --git a/packages/jest-cli/src/cli/args.ts b/packages/jest-cli/src/cli/args.ts index 06e36485a305..f7018d51ab10 100644 --- a/packages/jest-cli/src/cli/args.ts +++ b/packages/jest-cli/src/cli/args.ts @@ -49,10 +49,10 @@ export function check(argv: Config.Argv): true { ); } - if (argv.runProjects && argv.runProjects.length === 0) { + if (argv.selectProjects && argv.selectProjects.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', + 'The --selectProjects option requires the name of at least one project to be specified.\n' + + 'Example usage: jest --selectProjects my-first-project my-second-project', ); } @@ -522,13 +522,6 @@ export const options = { 'rare.', type: 'boolean', }, - runProjects: { - description: - 'Run only the tests of the specified projects.' + - 'Jest uses the attribute `displayName` in the configuration to identify each project.', - string: true, - type: 'array', - }, runTestsByPath: { default: false, description: @@ -542,6 +535,13 @@ export const options = { "Allows to use a custom runner instead of Jest's default test runner.", type: 'string', }, + selectProjects: { + description: + 'Run only the tests of the specified projects.' + + 'Jest uses the attribute `displayName` in the configuration to identify each project.', + string: true, + type: 'array', + }, setupFiles: { description: 'A list of paths to modules that run some code to configure or ' + diff --git a/packages/jest-core/src/getConfigsOfProjectsToRun.ts b/packages/jest-core/src/getConfigsOfProjectsToRun.ts index 7defdb25efd9..81d05c080ec9 100644 --- a/packages/jest-core/src/getConfigsOfProjectsToRun.ts +++ b/packages/jest-core/src/getConfigsOfProjectsToRun.ts @@ -12,10 +12,10 @@ export default function getConfigsOfProjectsToRun( argv: Config.Argv, projectConfigs: Array, ): Array { - if (!argv.runProjects) { + if (!argv.selectProjects) { return projectConfigs; } - const namesOfProjectsToRun = new Set(argv.runProjects); + const namesOfProjectsToRun = new Set(argv.selectProjects); return projectConfigs.filter(config => { const name = getProjectDisplayName(config); return name && namesOfProjectsToRun.has(name); diff --git a/packages/jest-core/src/getSelectProjectsMessage.ts b/packages/jest-core/src/getSelectProjectsMessage.ts index e5519083c671..8dc10df76369 100644 --- a/packages/jest-core/src/getSelectProjectsMessage.ts +++ b/packages/jest-core/src/getSelectProjectsMessage.ts @@ -26,13 +26,13 @@ export default function getSelectProjectsMessage( function getNoSelectionWarning(): string { return chalk.yellow( - 'You provided values for --runProjects but no projects were found matching the selection.\n', + 'You provided values for --selectProjects but no projects were found matching the selection.\n', ); } function getNamesMissingWarning(numberOfProjectsWithoutAName: number): string { return chalk.yellow( - `You provided values for --runProjects but ${ + `You provided values for --selectProjects but ${ numberOfProjectsWithoutAName === 1 ? 'a project has' : `${numberOfProjectsWithoutAName} projects have` diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index ee3581386ab7..8bfc411710f7 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -408,7 +408,7 @@ export type Argv = Arguments< rootDir: string; roots: Array; runInBand: boolean; - runProjects: Array; + selectProjects: Array; setupFiles: Array; setupFilesAfterEnv: Array; showConfig: boolean; From 9916880a369877a34500825923127089d75e904e Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Thu, 7 May 2020 23:36:46 +0200 Subject: [PATCH 13/19] Put changelog entry at the top --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60184a323541..c0ecbaae1ac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-cli, jest-core]` Add `--selectProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) + ### Fixes - `[jest-jasmine2]` Stop adding `:` after an error that has no message ([#9990](https://github.com/facebook/jest/pull/9990)) @@ -265,7 +267,6 @@ - `[expect]` Add `BigInt` support to `toBeGreaterThan`, `toBeGreaterThanOrEqual`, `toBeLessThan` and `toBeLessThanOrEqual` ([#8382](https://github.com/facebook/jest/pull/8382)) - `[expect, jest-matcher-utils]` Display change counts in annotation lines ([#9035](https://github.com/facebook/jest/pull/9035)) - `[expect, jest-snapshot]` Support custom inline snapshot matchers ([#9278](https://github.com/facebook/jest/pull/9278)) -- `[jest-cli, jest-core]` Add `--selectProjects` CLI argument to filter test suites by project name ([#8612](https://github.com/facebook/jest/pull/8612)) - `[jest-config]` Throw the full error message and stack when a Jest preset is missing a dependency ([#8924](https://github.com/facebook/jest/pull/8924)) - `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689)) - `[jest-config]` Merge preset globals with project globals ([#9027](https://github.com/facebook/jest/pull/9027)) From 100ccdd83fb9c2ceed5845124ed3556163fcee87 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Fri, 8 May 2020 00:33:53 +0200 Subject: [PATCH 14/19] Fix when there are no missing name --- packages/jest-core/src/getSelectProjectsMessage.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/jest-core/src/getSelectProjectsMessage.ts b/packages/jest-core/src/getSelectProjectsMessage.ts index 8dc10df76369..9f88e71483e1 100644 --- a/packages/jest-core/src/getSelectProjectsMessage.ts +++ b/packages/jest-core/src/getSelectProjectsMessage.ts @@ -31,6 +31,9 @@ function getNoSelectionWarning(): string { } function getNamesMissingWarning(numberOfProjectsWithoutAName: number): string { + if (numberOfProjectsWithoutAName === 0) { + return ''; + } return chalk.yellow( `You provided values for --selectProjects but ${ numberOfProjectsWithoutAName === 1 From 36e7294c4477177dfe0d1903df50e87b57dc7d42 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Fri, 8 May 2020 18:31:26 +0200 Subject: [PATCH 15/19] Fix warnings and add tests --- e2e/__tests__/selectProjects.test.ts | 194 +++++++++++++----- .../__tests__/first-project.test.js | 10 + .../__tests__/second-project.test.js | 10 + e2e/select-projects-missing-name/package.json | 20 ++ packages/jest-core/src/cli/index.ts | 11 +- .../src/getConfigsOfProjectsToRun.ts | 9 +- .../src/getProjectNamesMissingWarning.ts | 29 +++ .../jest-core/src/getSelectProjectsMessage.ts | 22 +- 8 files changed, 223 insertions(+), 82 deletions(-) create mode 100644 e2e/select-projects-missing-name/__tests__/first-project.test.js create mode 100644 e2e/select-projects-missing-name/__tests__/second-project.test.js create mode 100644 e2e/select-projects-missing-name/package.json create mode 100644 packages/jest-core/src/getProjectNamesMissingWarning.ts diff --git a/e2e/__tests__/selectProjects.test.ts b/e2e/__tests__/selectProjects.test.ts index 2b2434ddad2b..6f5f9043255e 100644 --- a/e2e/__tests__/selectProjects.test.ts +++ b/e2e/__tests__/selectProjects.test.ts @@ -9,74 +9,160 @@ import {resolve} from 'path'; import {json as runWithJson} from '../runJest'; -const dir = resolve(__dirname, '..', 'select-projects'); +describe('Given a config with two named projects, first-project and second-project', () => { + const dir = resolve(__dirname, '..', 'select-projects'); -describe('when Jest is started with `--selectProjects first-project`', () => { - const result = runWithJson('select-projects', [ - '--selectProjects', - 'first-project', - ]); - it('runs the tests in the first project only', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(1); - expect(result.json.testResults.map(({name}) => name)).toEqual([ - resolve(dir, '__tests__/first-project.test.js'), + describe('when Jest is started with `--selectProjects first-project`', () => { + const result = runWithJson('select-projects', [ + '--selectProjects', + 'first-project', ]); + it('runs the tests in the first project only', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(1); + expect(result.json.testResults.map(({name}) => name)).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + ]); + }); + it('prints that only first-project will run', () => { + expect(result.stderr).toMatch(/^Running one project: first-project/); + }); }); - it('prints that only first-project will run', () => { - expect(result.stderr).toMatch(/^Running one project: first-project/); + + describe('when Jest is started with `--selectProjects second-project`', () => { + const result = runWithJson('select-projects', [ + '--selectProjects', + 'second-project', + ]); + it('runs the tests in the second project only', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(1); + expect(result.json.testResults.map(({name}) => name)).toEqual([ + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('prints that only second-project will run', () => { + expect(result.stderr).toMatch(/^Running one project: second-project/); + }); }); -}); -describe('when Jest is started with `--selectProjects second-project`', () => { - const result = runWithJson('select-projects', [ - `--selectProjects`, - 'second-project', - ]); - it('runs the tests in the second project only', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(1); - expect(result.json.testResults.map(({name}) => name)).toEqual([ - resolve(dir, '__tests__/second-project.test.js'), + describe('when Jest is started with `--selectProjects first-project second-project`', () => { + const result = runWithJson('select-projects', [ + '--selectProjects', + 'first-project', + 'second-project', ]); + it('runs the tests in the first and second projects', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(2); + expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('prints that both first-project and second-project will run', () => { + expect(result.stderr).toMatch( + /^Running 2 projects:\n- first-project\n- second-project/, + ); + }); }); - it('prints that only second-project will run', () => { - expect(result.stderr).toMatch(/^Running one project: second-project/); + + describe('when Jest is started without providing `--selectProjects`', () => { + const result = runWithJson('select-projects', []); + it('runs the tests in the first and second projects', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(2); + expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('does not print which projects are run', () => { + expect(result.stderr).not.toMatch(/^Running/); + }); }); -}); -describe('when Jest is started with `--selectProjects first-project second-project`', () => { - const result = runWithJson('select-projects', [ - `--selectProjects`, - 'first-project', - 'second-project', - ]); - it('runs the tests in the first and second projects', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(2); - expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ - resolve(dir, '__tests__/first-project.test.js'), - resolve(dir, '__tests__/second-project.test.js'), + describe('when Jest is started with `--selectProjects third-project`', () => { + const result = runWithJson('select-projects', [ + '--passWithNoTests', // This is necessary to get a JSON output + '--selectProjects', + 'third-project', ]); - }); - it('prints that both first-project and second-project will run', () => { - expect(result.stderr).toMatch( - /^Running 2 projects:\n- first-project\n- second-project/, - ); + it('does not run any tests', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(0); + }); + it('prints that no project was found', () => { + expect(result.stderr).toMatch( + /^You provided values for --selectProjects but no projects were found matching the selection/, + ); + }); }); }); -describe('when Jest is started without providing `--selectProjects`', () => { - const result = runWithJson('select-projects', []); - it('runs the tests in the first and second projects', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(2); - expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ - resolve(dir, '__tests__/first-project.test.js'), - resolve(dir, '__tests__/second-project.test.js'), +describe('Given a config with two projects, first-project and an unnamed project', () => { + const dir = resolve(__dirname, '..', 'select-projects-missing-name'); + + describe('when Jest is started with `--selectProjects first-project`', () => { + const result = runWithJson('select-projects-missing-name', [ + '--selectProjects', + 'first-project', ]); + it('runs the tests in the first project only', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(1); + expect(result.json.testResults.map(({name}) => name)).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + ]); + }); + it('prints that a project does not have name', () => { + expect(result.stderr).toMatch( + /^You provided values for --selectProjects but a project does not a have name/, + ); + }); + it('prints that only first-project will run', () => { + const stderrThirdLine = result.stderr.split('\n')[2]; + expect(stderrThirdLine).toMatch(/^Running one project: first-project/); + }); + }); + + describe('when Jest is started without providing `--selectProjects`', () => { + const result = runWithJson('select-projects-missing-name', []); + it('runs the tests in the first and second projects', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(2); + expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ + resolve(dir, '__tests__/first-project.test.js'), + resolve(dir, '__tests__/second-project.test.js'), + ]); + }); + it('does not print that a project has no name', () => { + expect(result.stderr).not.toMatch( + /^You provided values for --selectProjects but a project does not a have name/, + ); + }); }); - it('does not print which projects are run', () => { - expect(result.stderr).not.toMatch(/^Running/); + + describe('when Jest is started with `--selectProjects third-project`', () => { + const result = runWithJson('select-projects-missing-name', [ + '--passWithNoTests', + '--selectProjects', + 'third-project', + ]); + it('does not run any tests', () => { + expect(result.json.success).toBe(true); + expect(result.json.numTotalTests).toBe(0); + }); + it('prints that a project does not have name', () => { + expect(result.stderr).toMatch( + /^You provided values for --selectProjects but a project does not a have name/, + ); + }); + it('prints that no project was found', () => { + const stderrThirdLine = result.stderr.split('\n')[2]; + expect(stderrThirdLine).toMatch( + /^You provided values for --selectProjects but no projects were found matching the selection/, + ); + }); }); }); diff --git a/e2e/select-projects-missing-name/__tests__/first-project.test.js b/e2e/select-projects-missing-name/__tests__/first-project.test.js new file mode 100644 index 000000000000..f440fcd752c5 --- /dev/null +++ b/e2e/select-projects-missing-name/__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 selectProjects', () => { + expect(true).toBe(true); +}); diff --git a/e2e/select-projects-missing-name/__tests__/second-project.test.js b/e2e/select-projects-missing-name/__tests__/second-project.test.js new file mode 100644 index 000000000000..c3db24a8b9a6 --- /dev/null +++ b/e2e/select-projects-missing-name/__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 selectProjects', () => { + expect(true).toBe(true); +}); diff --git a/e2e/select-projects-missing-name/package.json b/e2e/select-projects-missing-name/package.json new file mode 100644 index 000000000000..a9743659e309 --- /dev/null +++ b/e2e/select-projects-missing-name/package.json @@ -0,0 +1,20 @@ +{ + "description": "Testing the behaviour of --selectProjects when a project does not have a name", + "jest": { + "projects": [ + { + "displayName": "first-project", + "testMatch": [ + "/__tests__/first-project.test.js" + ], + "testEnvironment": "node" + }, + { + "testMatch": [ + "/__tests__/second-project.test.js" + ], + "testEnvironment": "node" + } + ] + } +} diff --git a/packages/jest-core/src/cli/index.ts b/packages/jest-core/src/cli/index.ts index c555523a3c15..011cadf4a203 100644 --- a/packages/jest-core/src/cli/index.ts +++ b/packages/jest-core/src/cli/index.ts @@ -27,6 +27,7 @@ import watch from '../watch'; import pluralize from '../pluralize'; import logDebugMessages from '../lib/log_debug_messages'; import getConfigsOfProjectsToRun from '../getConfigsOfProjectsToRun'; +import getProjectNamesMissingWarning from '../getProjectNamesMissingWarning'; import getSelectProjectsMessage from '../getSelectProjectsMessage'; const {print: preRunMessagePrint} = preRunMessage; @@ -70,8 +71,16 @@ export async function runCLI( exit(0); } - const configsOfProjectsToRun = getConfigsOfProjectsToRun(argv, configs); + let configsOfProjectsToRun = configs; if (argv.selectProjects) { + const namesMissingWarning = getProjectNamesMissingWarning(configs); + if (namesMissingWarning) { + outputStream.write(namesMissingWarning); + } + configsOfProjectsToRun = getConfigsOfProjectsToRun( + argv.selectProjects, + configs, + ); outputStream.write(getSelectProjectsMessage(configsOfProjectsToRun)); } diff --git a/packages/jest-core/src/getConfigsOfProjectsToRun.ts b/packages/jest-core/src/getConfigsOfProjectsToRun.ts index 81d05c080ec9..b8b8288ccc68 100644 --- a/packages/jest-core/src/getConfigsOfProjectsToRun.ts +++ b/packages/jest-core/src/getConfigsOfProjectsToRun.ts @@ -9,15 +9,12 @@ import type {Config} from '@jest/types'; import getProjectDisplayName from './getProjectDisplayName'; export default function getConfigsOfProjectsToRun( - argv: Config.Argv, + namesOfProjectsToRun: Array, projectConfigs: Array, ): Array { - if (!argv.selectProjects) { - return projectConfigs; - } - const namesOfProjectsToRun = new Set(argv.selectProjects); + const setOfProjectsToRun = new Set(namesOfProjectsToRun); return projectConfigs.filter(config => { const name = getProjectDisplayName(config); - return name && namesOfProjectsToRun.has(name); + return name && setOfProjectsToRun.has(name); }); } diff --git a/packages/jest-core/src/getProjectNamesMissingWarning.ts b/packages/jest-core/src/getProjectNamesMissingWarning.ts new file mode 100644 index 000000000000..596f5e8e7961 --- /dev/null +++ b/packages/jest-core/src/getProjectNamesMissingWarning.ts @@ -0,0 +1,29 @@ +/** + * 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 chalk = require('chalk'); +import type {Config} from '@jest/types'; +import getProjectDisplayName from './getProjectDisplayName'; + +export default function getProjectNamesMissingWarning( + projectConfigs: Array, +): string | undefined { + const numberOfProjectsWithoutAName = projectConfigs.filter( + config => !getProjectDisplayName(config), + ).length; + if (numberOfProjectsWithoutAName === 0) { + return undefined; + } + return chalk.yellow( + `You provided values for --selectProjects but ${ + numberOfProjectsWithoutAName === 1 + ? 'a project does not a have name' + : `${numberOfProjectsWithoutAName} projects do not have a name` + }.\n` + + 'Set displayName in the config of all projects in order to disable this warning.\n', + ); +} diff --git a/packages/jest-core/src/getSelectProjectsMessage.ts b/packages/jest-core/src/getSelectProjectsMessage.ts index 9f88e71483e1..5d3fff3577b4 100644 --- a/packages/jest-core/src/getSelectProjectsMessage.ts +++ b/packages/jest-core/src/getSelectProjectsMessage.ts @@ -15,13 +15,7 @@ export default function getSelectProjectsMessage( if (projectConfigs.length === 0) { return getNoSelectionWarning(); } - const numberOfProjectsWithoutAName = projectConfigs.filter( - config => !getProjectDisplayName(config), - ).length; - return ( - getNamesMissingWarning(numberOfProjectsWithoutAName) + - getProjectsRunningMessage(projectConfigs) - ); + return getProjectsRunningMessage(projectConfigs); } function getNoSelectionWarning(): string { @@ -30,20 +24,6 @@ function getNoSelectionWarning(): string { ); } -function getNamesMissingWarning(numberOfProjectsWithoutAName: number): string { - if (numberOfProjectsWithoutAName === 0) { - return ''; - } - return chalk.yellow( - `You provided values for --selectProjects but ${ - numberOfProjectsWithoutAName === 1 - ? 'a project has' - : `${numberOfProjectsWithoutAName} projects have` - } no name.\n` + - 'Set displayName in the config of all projects in order to disable this warning.\n', - ); -} - function getProjectsRunningMessage( projectConfigs: Array, ): string { From d5a681c558908a8d03370ae6f9ac45246a0a7f68 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Fri, 8 May 2020 18:34:03 +0200 Subject: [PATCH 16/19] Added advice in docs --- docs/CLI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CLI.md b/docs/CLI.md index 3c1d7b26aa33..b0c989dfba67 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -256,7 +256,7 @@ Alias: `-i`. Run all tests serially in the current process, rather than creating ### `--selectProjects ... ` -Run only the tests of the specified projects. Jest uses the attribute `displayName` in the configuration to identify each project. +Run only the tests of the specified projects. Jest uses the attribute `displayName` in the configuration to identify each project. If you use this option, you should provide a `displayName` to all your projects. ### `--runTestsByPath` From 52eaa670ce5b482d913c8a215ac7910d2bdf97dc Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sat, 9 May 2020 19:49:21 +0200 Subject: [PATCH 17/19] Refactor tests --- e2e/__tests__/selectProjects.test.ts | 117 ++++++++++++++++----------- e2e/runJest.ts | 2 +- 2 files changed, 70 insertions(+), 49 deletions(-) diff --git a/e2e/__tests__/selectProjects.test.ts b/e2e/__tests__/selectProjects.test.ts index 6f5f9043255e..ca4e67aeecbe 100644 --- a/e2e/__tests__/selectProjects.test.ts +++ b/e2e/__tests__/selectProjects.test.ts @@ -7,19 +7,26 @@ import {resolve} from 'path'; -import {json as runWithJson} from '../runJest'; +import run, { + RunJestJsonResult, + RunJestResult, + json as runWithJson, +} from '../runJest'; describe('Given a config with two named projects, first-project and second-project', () => { const dir = resolve(__dirname, '..', 'select-projects'); describe('when Jest is started with `--selectProjects first-project`', () => { - const result = runWithJson('select-projects', [ - '--selectProjects', - 'first-project', - ]); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects', [ + '--selectProjects', + 'first-project', + ]); + }); it('runs the tests in the first project only', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(1); + expect(result.json).toHaveProperty('success', true); + expect(result.json).toHaveProperty('numTotalTests', 1); expect(result.json.testResults.map(({name}) => name)).toEqual([ resolve(dir, '__tests__/first-project.test.js'), ]); @@ -30,13 +37,16 @@ describe('Given a config with two named projects, first-project and second-proje }); describe('when Jest is started with `--selectProjects second-project`', () => { - const result = runWithJson('select-projects', [ - '--selectProjects', - 'second-project', - ]); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects', [ + '--selectProjects', + 'second-project', + ]); + }); it('runs the tests in the second project only', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(1); + expect(result.json).toHaveProperty('success', true); + expect(result.json).toHaveProperty('numTotalTests', 1); expect(result.json.testResults.map(({name}) => name)).toEqual([ resolve(dir, '__tests__/second-project.test.js'), ]); @@ -47,14 +57,17 @@ describe('Given a config with two named projects, first-project and second-proje }); describe('when Jest is started with `--selectProjects first-project second-project`', () => { - const result = runWithJson('select-projects', [ - '--selectProjects', - 'first-project', - 'second-project', - ]); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects', [ + '--selectProjects', + 'first-project', + 'second-project', + ]); + }); it('runs the tests in the first and second projects', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(2); + expect(result.json).toHaveProperty('success', true); + expect(result.json).toHaveProperty('numTotalTests', 2); expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ resolve(dir, '__tests__/first-project.test.js'), resolve(dir, '__tests__/second-project.test.js'), @@ -68,10 +81,13 @@ describe('Given a config with two named projects, first-project and second-proje }); describe('when Jest is started without providing `--selectProjects`', () => { - const result = runWithJson('select-projects', []); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects', []); + }); it('runs the tests in the first and second projects', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(2); + expect(result.json).toHaveProperty('success', true); + expect(result.json).toHaveProperty('numTotalTests', 2); expect(result.json.testResults.map(({name}) => name).sort()).toEqual([ resolve(dir, '__tests__/first-project.test.js'), resolve(dir, '__tests__/second-project.test.js'), @@ -83,17 +99,15 @@ describe('Given a config with two named projects, first-project and second-proje }); describe('when Jest is started with `--selectProjects third-project`', () => { - const result = runWithJson('select-projects', [ - '--passWithNoTests', // This is necessary to get a JSON output - '--selectProjects', - 'third-project', - ]); - it('does not run any tests', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(0); + let result: RunJestResult; + beforeAll(() => { + result = run('select-projects', ['--selectProjects', 'third-project']); + }); + it('fails', () => { + expect(result).toHaveProperty('failed', true); }); it('prints that no project was found', () => { - expect(result.stderr).toMatch( + expect(result.stdout).toMatch( /^You provided values for --selectProjects but no projects were found matching the selection/, ); }); @@ -104,10 +118,13 @@ describe('Given a config with two projects, first-project and an unnamed project const dir = resolve(__dirname, '..', 'select-projects-missing-name'); describe('when Jest is started with `--selectProjects first-project`', () => { - const result = runWithJson('select-projects-missing-name', [ - '--selectProjects', - 'first-project', - ]); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects-missing-name', [ + '--selectProjects', + 'first-project', + ]); + }); it('runs the tests in the first project only', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(1); @@ -127,7 +144,10 @@ describe('Given a config with two projects, first-project and an unnamed project }); describe('when Jest is started without providing `--selectProjects`', () => { - const result = runWithJson('select-projects-missing-name', []); + let result: RunJestJsonResult; + beforeAll(() => { + result = runWithJson('select-projects-missing-name', []); + }); it('runs the tests in the first and second projects', () => { expect(result.json.success).toBe(true); expect(result.json.numTotalTests).toBe(2); @@ -144,23 +164,24 @@ describe('Given a config with two projects, first-project and an unnamed project }); describe('when Jest is started with `--selectProjects third-project`', () => { - const result = runWithJson('select-projects-missing-name', [ - '--passWithNoTests', - '--selectProjects', - 'third-project', - ]); - it('does not run any tests', () => { - expect(result.json.success).toBe(true); - expect(result.json.numTotalTests).toBe(0); + let result: RunJestResult; + beforeAll(() => { + result = run('select-projects-missing-name', [ + '--selectProjects', + 'third-project', + ]); + }); + it('does fails', () => { + expect(result).toHaveProperty('failed', true); }); it('prints that a project does not have name', () => { - expect(result.stderr).toMatch( + expect(result.stdout).toMatch( /^You provided values for --selectProjects but a project does not a have name/, ); }); it('prints that no project was found', () => { - const stderrThirdLine = result.stderr.split('\n')[2]; - expect(stderrThirdLine).toMatch( + const stdoutThirdLine = result.stdout.split('\n')[2]; + expect(stdoutThirdLine).toMatch( /^You provided values for --selectProjects but no projects were found matching the selection/, ); }); diff --git a/e2e/runJest.ts b/e2e/runJest.ts index 28eda5785672..44e5e5217894 100644 --- a/e2e/runJest.ts +++ b/e2e/runJest.ts @@ -95,7 +95,7 @@ function spawnJest( export type RunJestResult = execa.ExecaReturnValue; -interface RunJestJsonResult extends RunJestResult { +export interface RunJestJsonResult extends RunJestResult { json: FormattedTestResults; } From 67a66f8690898360354ff06ac607b8c3779a1e37 Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sat, 9 May 2020 19:52:00 +0200 Subject: [PATCH 18/19] Fix typo in test --- e2e/__tests__/selectProjects.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/__tests__/selectProjects.test.ts b/e2e/__tests__/selectProjects.test.ts index ca4e67aeecbe..d2c4ab3b9e55 100644 --- a/e2e/__tests__/selectProjects.test.ts +++ b/e2e/__tests__/selectProjects.test.ts @@ -171,7 +171,7 @@ describe('Given a config with two projects, first-project and an unnamed project 'third-project', ]); }); - it('does fails', () => { + it('fails', () => { expect(result).toHaveProperty('failed', true); }); it('prints that a project does not have name', () => { From c0262e9e605f0346fe382a1fa34fe3e11cfa850e Mon Sep 17 00:00:00 2001 From: Yacine Hmito Date: Sat, 9 May 2020 19:53:05 +0200 Subject: [PATCH 19/19] Fix more typos --- e2e/__tests__/selectProjects.test.ts | 10 +++++----- .../jest-core/src/getProjectNamesMissingWarning.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/selectProjects.test.ts b/e2e/__tests__/selectProjects.test.ts index d2c4ab3b9e55..472221575cc7 100644 --- a/e2e/__tests__/selectProjects.test.ts +++ b/e2e/__tests__/selectProjects.test.ts @@ -132,9 +132,9 @@ describe('Given a config with two projects, first-project and an unnamed project resolve(dir, '__tests__/first-project.test.js'), ]); }); - it('prints that a project does not have name', () => { + it('prints that a project does not have a name', () => { expect(result.stderr).toMatch( - /^You provided values for --selectProjects but a project does not a have name/, + /^You provided values for --selectProjects but a project does not have a name/, ); }); it('prints that only first-project will run', () => { @@ -158,7 +158,7 @@ describe('Given a config with two projects, first-project and an unnamed project }); it('does not print that a project has no name', () => { expect(result.stderr).not.toMatch( - /^You provided values for --selectProjects but a project does not a have name/, + /^You provided values for --selectProjects but a project does not have a name/, ); }); }); @@ -174,9 +174,9 @@ describe('Given a config with two projects, first-project and an unnamed project it('fails', () => { expect(result).toHaveProperty('failed', true); }); - it('prints that a project does not have name', () => { + it('prints that a project does not have a name', () => { expect(result.stdout).toMatch( - /^You provided values for --selectProjects but a project does not a have name/, + /^You provided values for --selectProjects but a project does not have a name/, ); }); it('prints that no project was found', () => { diff --git a/packages/jest-core/src/getProjectNamesMissingWarning.ts b/packages/jest-core/src/getProjectNamesMissingWarning.ts index 596f5e8e7961..53f0543d1cc8 100644 --- a/packages/jest-core/src/getProjectNamesMissingWarning.ts +++ b/packages/jest-core/src/getProjectNamesMissingWarning.ts @@ -21,7 +21,7 @@ export default function getProjectNamesMissingWarning( return chalk.yellow( `You provided values for --selectProjects but ${ numberOfProjectsWithoutAName === 1 - ? 'a project does not a have name' + ? 'a project does not have a name' : `${numberOfProjectsWithoutAName} projects do not have a name` }.\n` + 'Set displayName in the config of all projects in order to disable this warning.\n',