Skip to content

Commit

Permalink
use displayName instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed Dec 8, 2019
1 parent c744596 commit f33e371
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/CLI.md
Expand Up @@ -250,7 +250,7 @@ Alias: `-i`. Run all tests serially in the current process, rather than creating

### `--runProjects <project1> ... <projectN>`

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`

Expand Down
7 changes: 5 additions & 2 deletions e2e/run-projects/package.json
Expand Up @@ -2,14 +2,17 @@
"jest": {
"projects": [
{
"name": "first-project",
"displayName": "first-project",
"testMatch": [
"<rootDir>/__tests__/first-project.test.js"
],
"testEnvironment": "node"
},
{
"name": "second-project",
"displayName": {
"name": "second-project",
"color": "blue"
},
"testMatch": [
"<rootDir>/__tests__/second-project.test.js"
],
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-core/src/cli/index.ts
Expand Up @@ -76,8 +76,9 @@ export const runCLI = async (

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(
Expand Down
8 changes: 6 additions & 2 deletions packages/jest-core/src/getConfigsOfProjectsToRun.ts
Expand Up @@ -6,6 +6,7 @@
*/

import {Config} from '@jest/types';
import getProjectDisplayName from './getProjectDisplayName';

export default function getConfigsOfProjectsToRun(
argv: Config.Argv,
Expand All @@ -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<string>(argv.runProjects);
return projectConfigs.filter(config => {
const name = getProjectDisplayName(config);
return name && namesOfProjectsToRun.has(name);
});
}
24 changes: 24 additions & 0 deletions 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;
}
26 changes: 17 additions & 9 deletions 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.
*
Expand All @@ -6,21 +9,26 @@
*/

export default function getProjectsRunningMessage(
projectNames: Array<string>,
projectConfigs: Array<Config.ProjectConfig>,
): 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 || '<unnamed project>';
return `- ${elementContent}`;
}

0 comments on commit f33e371

Please sign in to comment.