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..c062992da482 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,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', () => { 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 d1f3201a700d..6f602cb0bd52 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -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 { @@ -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'; @@ -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' + @@ -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': {