From 1d245fd66b8338d150db079ef00027a645ab1fca Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Thu, 22 Aug 2019 09:15:41 +0200 Subject: [PATCH] set default display name color based on runner (#8689) --- CHANGELOG.md | 2 + .../__snapshots__/normalize.test.js.snap | 10 +++++ .../src/__tests__/normalize.test.js | 26 +++++++++++- packages/jest-config/src/color.ts | 30 ++++++++++++++ packages/jest-config/src/normalize.ts | 41 +++++++------------ 5 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 packages/jest-config/src/color.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e7cb0197dfaf..a69936a8fb79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689)) + ### Fixes - `[expect]` Display expectedDiff more carefully in toBeCloseTo ([#8389](https://github.com/facebook/jest/pull/8389)) diff --git a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap index 96d7622c963e..890e0f7087cf 100644 --- a/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap +++ b/packages/jest-config/src/__tests__/__snapshots__/normalize.test.js.snap @@ -17,6 +17,16 @@ exports[`Upgrade help logs a warning when \`scriptPreprocessor\` and/or \`prepro " `; +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`] = ` "Validation Error: diff --git a/packages/jest-config/src/__tests__/normalize.test.js b/packages/jest-config/src/__tests__/normalize.test.js index 0fc406830e29..3620be8a1603 100644 --- a/packages/jest-config/src/__tests__/normalize.test.js +++ b/packages/jest-config/src/__tests__/normalize.test.js @@ -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; @@ -1579,6 +1581,28 @@ describe('displayName', () => { }).toThrowErrorMatchingSnapshot(); }, ); + + it.each([ + undefined, + 'jest-runner', + 'jest-runner-eslint', + 'jest-runner-tslint', + 'jest-runner-tsc', + ])('generates a default color for the runner %s', 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', () => { diff --git a/packages/jest-config/src/color.ts b/packages/jest-config/src/color.ts new file mode 100644 index 000000000000..ff55010cf181 --- /dev/null +++ b/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 = [ + '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]; +}; diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index 634e506faf11..01e3999aff1d 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -16,7 +16,6 @@ import micromatch = require('micromatch'); import {sync as realpath} from 'realpath-native'; import Resolver = require('jest-resolve'); import {replacePathSepForRegex} from 'jest-regex-util'; -import getType = require('jest-get-type'); import validatePattern from './validatePattern'; import getMaxWorkers from './getMaxWorkers'; import { @@ -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'; @@ -761,35 +761,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') { - const errorMessage = - ` Option "${chalk.bold('displayName')}" must be of type:\n\n` + - ' {\n' + - ' name: string;\n' + - ' color: string;\n' + - ' }\n'; + if (typeof displayName === 'object') { const {name, color} = displayName; if ( !name || @@ -797,10 +773,21 @@ export default function normalize( typeof name !== 'string' || typeof color !== 'string' ) { + const errorMessage = + ` Option "${chalk.bold('displayName')}" must be of type:\n\n` + + ' {\n' + + ' name: string;\n' + + ' color: string;\n' + + ' }\n'; throw createConfigError(errorMessage); } + value = oldOptions[key]; + } else { + value = { + color: getDisplayNameColor(options.runner), + name: displayName, + }; } - value = oldOptions[key]; break; } case 'testTimeout': {