Skip to content

Commit

Permalink
add message
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed Nov 23, 2019
1 parent 1c130d8 commit 7289f31
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 42 deletions.
110 changes: 69 additions & 41 deletions e2e/__tests__/runProjects.test.ts
Expand Up @@ -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/);
});
});
7 changes: 6 additions & 1 deletion packages/jest-core/src/cli/index.ts
Expand Up @@ -19,14 +19,15 @@ 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';
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;

Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions 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>,
): 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}`;
}

0 comments on commit 7289f31

Please sign in to comment.