Skip to content

Commit 6faff95

Browse files
committed
set default display name color based on runner
BREAKING CHANGE: changes the internal config format for certain user-provided configs
1 parent ff9269b commit 6faff95

File tree

5 files changed

+81
-28
lines changed

5 files changed

+81
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Features
44

5+
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
6+
57
### Fixes
68

79
- `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389))

packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro
1717
<yellow></>"
1818
`;
1919
20+
exports[`displayName generates a default color for the runner jest-runner 1`] = `"yellow"`;
21+
22+
exports[`displayName generates a default color for the runner jest-runner-eslint 1`] = `"magenta"`;
23+
24+
exports[`displayName generates a default color for the runner jest-runner-tsc 1`] = `"red"`;
25+
26+
exports[`displayName generates a default color for the runner jest-runner-tslint 1`] = `"green"`;
27+
28+
exports[`displayName generates a default color for the runner undefined 1`] = `"white"`;
29+
2030
exports[`displayName should throw an error when displayName is is an empty object 1`] = `
2131
"<red><bold><bold>● <bold>Validation Error</>:</>
2232
<red></>

packages/jest-config/src/__tests__/normalize.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ let expectedPathFooQux;
2424
let expectedPathAbs;
2525
let expectedPathAbsAnother;
2626

27+
let virtualModuleRegexes;
28+
beforeEach(() => (virtualModuleRegexes = [/jest-jasmine2/, /babel-jest/]));
2729
const findNodeModule = jest.fn(name => {
28-
if (name.match(/jest-jasmine2|babel-jest/)) {
30+
if (virtualModuleRegexes.some(regex => regex.test(name))) {
2931
return name;
3032
}
3133
return null;
@@ -1579,6 +1581,28 @@ describe('displayName', () => {
15791581
}).toThrowErrorMatchingSnapshot();
15801582
},
15811583
);
1584+
1585+
it.each([
1586+
undefined,
1587+
'jest-runner',
1588+
'jest-runner-eslint',
1589+
'jest-runner-tslint',
1590+
'jest-runner-tsc',
1591+
])('generates a default color for the runner %s', runner => {
1592+
virtualModuleRegexes.push(/jest-runner-.+/);
1593+
const {
1594+
options: {displayName},
1595+
} = normalize(
1596+
{
1597+
rootDir: '/root/',
1598+
displayName: 'project',
1599+
runner,
1600+
},
1601+
{},
1602+
);
1603+
expect(displayName.name).toBe('project');
1604+
expect(displayName.color).toMatchSnapshot();
1605+
});
15821606
});
15831607

15841608
describe('testTimeout', () => {

packages/jest-config/src/color.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {createHash} from 'crypto';
9+
import chalk from 'chalk';
10+
11+
const colors: Array<keyof typeof chalk> = [
12+
'red',
13+
'green',
14+
'yellow',
15+
'blue',
16+
'magenta',
17+
'cyan',
18+
'white',
19+
];
20+
21+
export const getDisplayNameColor = (seed?: string) => {
22+
if (seed === undefined) {
23+
return 'white';
24+
}
25+
26+
const hash = createHash('sha256');
27+
hash.update(seed);
28+
const num = hash.digest().readUInt32LE(0);
29+
return colors[num % colors.length];
30+
};

packages/jest-config/src/normalize.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import micromatch from 'micromatch';
1616
import {sync as realpath} from 'realpath-native';
1717
import Resolver from 'jest-resolve';
1818
import {replacePathSepForRegex} from 'jest-regex-util';
19-
import getType from 'jest-get-type';
2019
import validatePattern from './validatePattern';
2120
import getMaxWorkers from './getMaxWorkers';
2221
import {
@@ -37,6 +36,7 @@ import DEFAULT_CONFIG from './Defaults';
3736
import DEPRECATED_CONFIG from './Deprecated';
3837
import setFromArgv from './setFromArgv';
3938
import VALID_CONFIG from './ValidConfig';
39+
import {getDisplayNameColor} from './color';
4040
const ERROR = `${BULLET}Validation Error`;
4141
const PRESET_EXTENSIONS = ['.json', '.js'];
4242
const PRESET_NAME = 'jest-preset';
@@ -761,46 +761,33 @@ export default function normalize(
761761
}
762762
case 'displayName': {
763763
const displayName = oldOptions[key] as Config.DisplayName;
764-
if (typeof displayName === 'string') {
765-
value = displayName;
766-
break;
767-
}
768764
/**
769765
* Ensuring that displayName shape is correct here so that the
770766
* reporters can trust the shape of the data
771-
* TODO: Normalize "displayName" such that given a config option
772-
* {
773-
* "displayName": "Test"
774-
* }
775-
* becomes
776-
* {
777-
* displayName: {
778-
* name: "Test",
779-
* color: "white"
780-
* }
781-
* }
782-
*
783-
* This can't be done now since this will be a breaking change
784-
* for custom reporters
785767
*/
786-
if (getType(displayName) === 'object') {
787-
const errorMessage =
788-
` Option "${chalk.bold('displayName')}" must be of type:\n\n` +
789-
' {\n' +
790-
' name: string;\n' +
791-
' color: string;\n' +
792-
' }\n';
768+
if (typeof displayName === 'object') {
793769
const {name, color} = displayName;
794770
if (
795771
!name ||
796772
!color ||
797773
typeof name !== 'string' ||
798774
typeof color !== 'string'
799775
) {
776+
const errorMessage =
777+
` Option "${chalk.bold('displayName')}" must be of type:\n\n` +
778+
' {\n' +
779+
' name: string;\n' +
780+
' color: string;\n' +
781+
' }\n';
800782
throw createConfigError(errorMessage);
801783
}
784+
value = oldOptions[key];
785+
} else {
786+
value = {
787+
color: getDisplayNameColor(options.runner),
788+
name: displayName,
789+
};
802790
}
803-
value = oldOptions[key];
804791
break;
805792
}
806793
case 'testTimeout': {

0 commit comments

Comments
 (0)