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 131d30c2efec..c24341514f7e 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 {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; @@ -74,6 +75,10 @@ export const runCLI = async ( } 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}`; +}