Skip to content

Commit

Permalink
feature(jest-config): Add colors configuration option (#13624)
Browse files Browse the repository at this point in the history
notoriousmango authored Jan 1, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8709b7d commit 63a4583
Showing 23 changed files with 147 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

### Features

- `[jest-config]` Add colors configuration option [#13624](https://github.com/facebook/jest/pull/13624)
- `[jest-haste-map]` ignore Sapling vcs directories (`.sl/`) ([#13674](https://github.com/facebook/jest/pull/13674))
- `[jest-resolve]` Support subpath imports ([#13705](https://github.com/facebook/jest/pull/13705))
- `[jest-runtime]` Add `jest.isolateModulesAsync` for scoped module initialization of asynchronous functions ([#13680](https://github.com/facebook/jest/pull/13680))
6 changes: 6 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
@@ -194,6 +194,12 @@ The `babel` and `v8` coverage providers use `/* istanbul ignore next */` and `/*

:::

### `colors` \[boolean]

Default: `false`

Indicates whether to force test results output highlighting even if stdout is not a TTY.

### `collectCoverageFrom` \[array]

Default: `undefined`
1 change: 1 addition & 0 deletions e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"cache": false,
"cacheDirectory": "/tmp/jest",
"clearMocks": false,
"colors": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
14 changes: 14 additions & 0 deletions e2e/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -75,3 +75,17 @@ test('negated flags override previous flags', () => {

expect(globalConfig.silent).toBe(true);
});

test('colors is true when it is specified in package.json', () => {
const {configs} = getConfig('colors/with-colors');

expect(configs).toHaveLength(1);
expect(configs[0].colors).toBe(true);
});

test('colors is false when it is not specified in package.json', () => {
const {configs} = getConfig('colors/without-colors');

expect(configs).toHaveLength(1);
expect(configs[0].colors).toBe(false);
});
19 changes: 19 additions & 0 deletions e2e/colors/with-colors/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.
*/

'use strict';

const TestClass = require('..');
const localClass = new TestClass();

test('first test', () => {
expect(localClass.test).toHaveBeenCalledTimes(1);
});

test('second test', () => {
expect(localClass.test()).toBe('12345');
});
12 changes: 12 additions & 0 deletions e2e/colors/with-colors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

module.exports = class Test {
test() {
return '12345';
}
};
6 changes: 6 additions & 0 deletions e2e/colors/with-colors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"colors": true
}
}
19 changes: 19 additions & 0 deletions e2e/colors/without-colors/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.
*/

'use strict';

const TestClass = require('..');
const localClass = new TestClass();

test('first test', () => {
expect(localClass.test).toHaveBeenCalledTimes(1);
});

test('second test', () => {
expect(localClass.test()).toBe('12345');
});
12 changes: 12 additions & 0 deletions e2e/colors/without-colors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

module.exports = class Test {
test() {
return '12345';
}
};
5 changes: 5 additions & 0 deletions e2e/colors/without-colors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
Original file line number Diff line number Diff line change
@@ -140,6 +140,9 @@ module.exports = {
// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: undefined,
// Forces test results output highlighting even if stdout is not a TTY.
// colors: false,
// The directory where Jest should output its coverage files
// coverageDirectory: undefined,
16 changes: 14 additions & 2 deletions packages/jest-cli/src/init/generateConfigFile.ts
Original file line number Diff line number Diff line change
@@ -30,8 +30,14 @@ const generateConfigFile = (
results: Record<string, unknown>,
generateEsm = false,
): string => {
const {useTypescript, coverage, coverageProvider, clearMocks, environment} =
results;
const {
useTypescript,
coverage,
coverageProvider,
clearMocks,
environment,
colors,
} = results;

const overrides: Record<string, unknown> = {};

@@ -60,6 +66,12 @@ const generateConfigFile = (
});
}

if (colors) {
Object.assign(overrides, {
colors: true,
});
}

const overrideKeys = Object.keys(overrides) as Array<
keyof Config.InitialOptions
>;
1 change: 1 addition & 0 deletions packages/jest-config/src/Defaults.ts
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ const defaultOptions: Config.DefaultOptions = {
ci: isCI,
clearMocks: false,
collectCoverage: false,
colors: false,
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageProvider: 'babel',
coverageReporters: ['json', 'text', 'lcov', 'clover'],
2 changes: 2 additions & 0 deletions packages/jest-config/src/Descriptions.ts
Original file line number Diff line number Diff line change
@@ -18,6 +18,8 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
'Indicates whether the coverage information should be collected while executing the test',
collectCoverageFrom:
'An array of glob patterns indicating a set of files for which coverage information should be collected',
colors:
'Forces test results output highlighting even if stdout is not a TTY.',
coverageDirectory:
'The directory where Jest should output its coverage files',
coveragePathIgnorePatterns:
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export const initialOptions: Config.InitialOptions = {
clearMocks: false,
collectCoverage: true,
collectCoverageFrom: ['src', '!public'],
colors: false,
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageProvider: 'v8',
11 changes: 11 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -1786,6 +1786,17 @@ describe('Defaults', () => {
});
});

describe('colors', () => {
it('should pass option through', async () => {
const {options} = await normalize(
{colors: true, rootDir: '/root/'},
{} as Config.Argv,
);

expect(options.colors).toBe(true);
});
});

describe('displayName', () => {
test.each<{displayName: Config.DisplayName; description: string}>`
displayName | description
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -146,6 +146,7 @@ const groupOptions = (
cache: options.cache,
cacheDirectory: options.cacheDirectory,
clearMocks: options.clearMocks,
colors: options.colors,
coveragePathIgnorePatterns: options.coveragePathIgnorePatterns,
cwd: options.cwd,
dependencyExtractor: options.dependencyExtractor,
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
@@ -888,6 +888,7 @@ export default async function normalize(
case 'changedFilesWithAncestor':
case 'clearMocks':
case 'collectCoverage':
case 'colors':
case 'coverageProvider':
case 'coverageReporters':
case 'coverageThreshold':
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ exports[`prints the config object 1`] = `
"cache": false,
"cacheDirectory": "/test_cache_dir/",
"clearMocks": false,
"colors": false,
"coveragePathIgnorePatterns": [],
"cwd": "/test_root_dir/",
"detectLeaks": false,

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
@@ -155,6 +155,7 @@ export type DefaultOptions = {
ci: boolean;
clearMocks: boolean;
collectCoverage: boolean;
colors: boolean;
coveragePathIgnorePatterns: Array<string>;
coverageReporters: Array<CoverageReporterName>;
coverageProvider: CoverageProvider;
@@ -228,6 +229,7 @@ export type InitialOptions = Partial<{
cacheDirectory: string;
ci: boolean;
clearMocks: boolean;
colors: boolean;
changedFilesWithAncestor: boolean;
changedSince: string;
collectCoverage: boolean;
@@ -426,6 +428,7 @@ export type ProjectConfig = {
cache: boolean;
cacheDirectory: string;
clearMocks: boolean;
colors: boolean;
coveragePathIgnorePatterns: Array<string>;
cwd: string;
dependencyExtractor?: string;
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export const defaultConfig = {
bail: 0,
cacheDirectory: path.join(tmpdir(), 'jest'),
clearMocks: false,
colors: false,
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageReporters: ['json', 'text', 'lcov', 'clover'],
expand: false,
@@ -63,6 +64,7 @@ export const validConfig = {
clearMocks: false,
collectCoverage: true,
collectCoverageFrom: ['src', '!public'],
colors: false,
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
coverageReporters: ['json', 'text', 'lcov', 'clover'],
1 change: 1 addition & 0 deletions packages/test-utils/src/config.ts
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
cache: false,
cacheDirectory: '/test_cache_dir/',
clearMocks: false,
colors: false,
coveragePathIgnorePatterns: [],
cwd: '/test_root_dir/',
detectLeaks: false,

0 comments on commit 63a4583

Please sign in to comment.