Skip to content

Commit

Permalink
set default display name color based on runner
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
changes the internal config format for certain user-provided configs
  • Loading branch information
jeysal committed Jul 15, 2019
1 parent b76f01b commit 651a802
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
Expand Up @@ -17,6 +17,16 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro
<yellow></>"
`;
exports[`displayName generates a default color for the runner jest-runner 1`] = `"yellow"`;
exports[`displayName generates a default color for the runner jest-runner-eslint 1`] = `"magenta"`;
exports[`displayName generates a default color for the runner jest-runner-tsc 1`] = `"red"`;
exports[`displayName generates a default color for the runner jest-runner-tslint 1`] = `"green"`;
exports[`displayName generates a default color for the runner undefined 1`] = `"white"`;
exports[`displayName should throw an error when displayName is is an empty object 1`] = `
"<red><bold><bold>● <bold>Validation Error</>:</>
<red></>
Expand Down
27 changes: 26 additions & 1 deletion packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -24,8 +24,10 @@ let expectedPathFooQux;
let expectedPathAbs;
let expectedPathAbsAnother;

let virtualModuleRegexes;
beforeEach(() => (virtualModuleRegexes = [/jest-jasmine2/, /babel-jest/]));
const findNodeModule = jest.fn(name => {
if (name.match(/jest-jasmine2|babel-jest/)) {
if (virtualModuleRegexes.some(regex => regex.test(name))) {
return name;
}
return null;
Expand Down Expand Up @@ -1579,6 +1581,29 @@ describe('displayName', () => {
}).toThrowErrorMatchingSnapshot();
},
);

it.each`
runner | color
${undefined} | ${'white'}
${'jest-runner'} | ${'yellow'}
${'jest-runner-eslint'} | ${'yellow'}
${'jest-runner-tslint'} | ${'yellow'}
${'jest-runner-tsc'} | ${'yellow'}
`('generates a default color for the runner $runner', ({runner}) => {
virtualModuleRegexes.push(/jest-runner-.+/);
const {
options: {displayName},
} = normalize(
{
rootDir: '/root/',
displayName: 'project',
runner,
},
{},
);
expect(displayName.name).toBe('project');
expect(displayName.color).toMatchSnapshot();
});
});

describe('testTimeout', () => {
Expand Down
30 changes: 30 additions & 0 deletions packages/jest-config/src/color.ts
@@ -0,0 +1,30 @@
/**
* 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 {createHash} from 'crypto';
import chalk from 'chalk';

const colors: Array<keyof typeof chalk> = [
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white',
];

export const getDisplayNameColor = (seed?: string) => {
if (seed === undefined) {
return 'white';
}

const hash = createHash('sha256');
hash.update(seed);
const num = hash.digest().readUInt32LE(0);
return colors[num % colors.length];
};
29 changes: 8 additions & 21 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -16,7 +16,6 @@ import micromatch from 'micromatch';
import {sync as realpath} from 'realpath-native';
import Resolver from 'jest-resolve';
import {replacePathSepForRegex} from 'jest-regex-util';
import getType from 'jest-get-type';
import validatePattern from './validatePattern';
import getMaxWorkers from './getMaxWorkers';
import {
Expand All @@ -37,6 +36,7 @@ import DEFAULT_CONFIG from './Defaults';
import DEPRECATED_CONFIG from './Deprecated';
import setFromArgv from './setFromArgv';
import VALID_CONFIG from './ValidConfig';
import {getDisplayNameColor} from './color';
const ERROR = `${BULLET}Validation Error`;
const PRESET_EXTENSIONS = ['.json', '.js'];
const PRESET_NAME = 'jest-preset';
Expand Down Expand Up @@ -762,29 +762,11 @@ export default function normalize(
}
case 'displayName': {
const displayName = oldOptions[key] as Config.DisplayName;
if (typeof displayName === 'string') {
value = displayName;
break;
}
/**
* Ensuring that displayName shape is correct here so that the
* reporters can trust the shape of the data
* TODO: Normalize "displayName" such that given a config option
* {
* "displayName": "Test"
* }
* becomes
* {
* displayName: {
* name: "Test",
* color: "white"
* }
* }
*
* This can't be done now since this will be a breaking change
* for custom reporters
*/
if (getType(displayName) === 'object') {
if (typeof displayName === 'object') {
const errorMessage =
` Option "${chalk.bold('displayName')}" must be of type:\n\n` +
' {\n' +
Expand All @@ -800,8 +782,13 @@ export default function normalize(
) {
throw createConfigError(errorMessage);
}
value = oldOptions[key];
} else {
value = {
color: getDisplayNameColor(options.runner),
name: oldOptions[key],
};
}
value = oldOptions[key];
break;
}
case 'testTimeout': {
Expand Down

0 comments on commit 651a802

Please sign in to comment.