Skip to content

Commit 1e02600

Browse files
committed
chore(jest-reporters): replace chalk with picocolors
1 parent 35a9822 commit 1e02600

12 files changed

+89
-88
lines changed

packages/jest-reporters/src/DefaultReporter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type {WriteStream} from 'tty';
9-
import chalk = require('chalk');
9+
import * as pc from 'picocolors';
1010
import {getConsoleOutput} from '@jest/console';
1111
import type {
1212
AggregatedResult,
@@ -30,7 +30,7 @@ import type {ReporterOnStartOptions} from './types';
3030
type write = WriteStream['write'];
3131
type FlushBufferedOutput = () => void;
3232

33-
const TITLE_BULLET = chalk.bold('\u25CF ');
33+
const TITLE_BULLET = pc.bold('\u25CF ');
3434

3535
export default class DefaultReporter extends BaseReporter {
3636
private _clear: string; // ANSI clear sequence for the last printed status
@@ -206,20 +206,20 @@ export default class DefaultReporter extends BaseReporter {
206206
const testRetryReasons = testResult.retryReasons;
207207
if (testRetryReasons && testRetryReasons.length > 0) {
208208
this.log(
209-
`${chalk.reset.inverse.bold.yellow(' LOGGING RETRY ERRORS ')} ${chalk.bold(testResult.fullName)}`,
209+
`${pc.reset(pc.inverse(pc.bold(pc.yellow(' LOGGING RETRY ERRORS '))))} ${pc.bold(testResult.fullName)}`,
210210
);
211211
for (const [index, retryReasons] of testRetryReasons.entries()) {
212212
let {message, stack} = separateMessageFromStack(retryReasons);
213213
stack = this._globalConfig.noStackTrace
214214
? ''
215-
: chalk.dim(
215+
: pc.dim(
216216
formatStackTrace(stack, config, this._globalConfig, testPath),
217217
);
218218

219219
message = indentAllLines(message);
220220

221221
this.log(
222-
`${chalk.reset.inverse.bold.blueBright(` RETRY ${index + 1} `)}\n`,
222+
`${pc.reset(pc.inverse(pc.bold(pc.blueBright(` RETRY ${index + 1} `))))}\n`,
223223
);
224224
this.log(`${message}\n${stack}\n`);
225225
}

packages/jest-reporters/src/GitHubActionsReporter.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import stripAnsi = require('strip-ansi');
1010
import type {
1111
AggregatedResult,
@@ -314,23 +314,23 @@ export default class GitHubActionsReporter extends BaseReporter {
314314
private printResultTree(resultTree: ResultTree): void {
315315
let perfMs;
316316
if (resultTree.performanceInfo.slow) {
317-
perfMs = ` (${chalk.red.inverse(
318-
`${resultTree.performanceInfo.runtime} ms`,
317+
perfMs = ` (${pc.red(
318+
pc.inverse(`${resultTree.performanceInfo.runtime} ms`),
319319
)})`;
320320
} else {
321321
perfMs = ` (${resultTree.performanceInfo.runtime} ms)`;
322322
}
323323
if (resultTree.passed) {
324324
this.startGroup(
325-
`${chalk.bold.green.inverse('PASS')} ${resultTree.name}${perfMs}`,
325+
`${pc.bold(pc.green(pc.inverse('PASS')))} ${resultTree.name}${perfMs}`,
326326
);
327327
for (const child of resultTree.children) {
328328
this.recursivePrintResultTree(child, true, 1);
329329
}
330330
this.endGroup();
331331
} else {
332332
this.log(
333-
` ${chalk.bold.red.inverse('FAIL')} ${resultTree.name}${perfMs}`,
333+
` ${pc.bold(pc.red(pc.inverse('FAIL')))} ${resultTree.name}${perfMs}`,
334334
);
335335
for (const child of resultTree.children) {
336336
this.recursivePrintResultTree(child, false, 1);
@@ -355,17 +355,17 @@ export default class GitHubActionsReporter extends BaseReporter {
355355
let resultSymbol;
356356
switch (resultTree.status) {
357357
case 'passed':
358-
resultSymbol = chalk.green(ICONS.success);
358+
resultSymbol = pc.green(ICONS.success);
359359
break;
360360
case 'failed':
361-
resultSymbol = chalk.red(ICONS.failed);
361+
resultSymbol = pc.red(ICONS.failed);
362362
break;
363363
case 'todo':
364-
resultSymbol = chalk.magenta(ICONS.todo);
364+
resultSymbol = pc.magenta(ICONS.todo);
365365
break;
366366
case 'pending':
367367
case 'skipped':
368-
resultSymbol = chalk.yellow(ICONS.pending);
368+
resultSymbol = pc.yellow(ICONS.pending);
369369
break;
370370
}
371371
this.log(

packages/jest-reporters/src/Status.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import stringLength = require('string-length');
1010
import type {
1111
AggregatedResult,
@@ -21,7 +21,7 @@ import type {ReporterOnStartOptions} from './types';
2121
import wrapAnsiString from './wrapAnsiString';
2222

2323
const RUNNING_TEXT = ' RUNS ';
24-
const RUNNING = `${chalk.reset.inverse.yellow.bold(RUNNING_TEXT)} `;
24+
const RUNNING = `${pc.reset(pc.inverse(pc.yellow(pc.bold(RUNNING_TEXT))))} `;
2525

2626
/**
2727
* This class is a perf optimization for sorting the list of currently

packages/jest-reporters/src/SummaryReporter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import type {
1010
AggregatedResult,
1111
SnapshotSummary,
@@ -131,7 +131,7 @@ export default class SummaryReporter extends BaseReporter {
131131
if (!this._globalConfig.silent) {
132132
message += `\n${
133133
wasInterrupted
134-
? chalk.bold.red('Test run was interrupted.')
134+
? pc.bold(pc.red('Test run was interrupted.'))
135135
: this._getTestSummary(testContexts, this._globalConfig)
136136
}`;
137137
}
@@ -194,7 +194,7 @@ export default class SummaryReporter extends BaseReporter {
194194
failedTests + runtimeErrors > 0 &&
195195
aggregatedResults.numTotalTestSuites > this._summaryThreshold
196196
) {
197-
this.log(chalk.bold('Summary of all failing tests'));
197+
this.log(pc.bold('Summary of all failing tests'));
198198
for (const testResult of aggregatedResults.testResults) {
199199
const {failureMessage} = testResult;
200200
if (failureMessage) {
@@ -218,15 +218,15 @@ export default class SummaryReporter extends BaseReporter {
218218
? ' related to files matching '
219219
: ' matching ';
220220

221-
return chalk.dim(prefix) + testPathPatterns.toPretty();
221+
return pc.dim(prefix) + testPathPatterns.toPretty();
222222
};
223223

224224
let testInfo = '';
225225

226226
if (globalConfig.runTestsByPath) {
227-
testInfo = chalk.dim(' within paths');
227+
testInfo = pc.dim(' within paths');
228228
} else if (globalConfig.onlyChanged) {
229-
testInfo = chalk.dim(' related to changed files');
229+
testInfo = pc.dim(' related to changed files');
230230
} else if (testPathPatterns.isSet()) {
231231
testInfo = getMatchingTestsInfo();
232232
}
@@ -236,22 +236,22 @@ export default class SummaryReporter extends BaseReporter {
236236
if (globalConfig.runTestsByPath) {
237237
nameInfo = ` ${globalConfig.nonFlagArgs.map(p => `"${p}"`).join(', ')}`;
238238
} else if (globalConfig.testNamePattern) {
239-
nameInfo = `${chalk.dim(' with tests matching ')}"${
239+
nameInfo = `${pc.dim(' with tests matching ')}"${
240240
globalConfig.testNamePattern
241241
}"`;
242242
}
243243

244244
const contextInfo =
245245
testContexts.size > 1
246-
? chalk.dim(' in ') + testContexts.size + chalk.dim(' projects')
246+
? pc.dim(' in ') + testContexts.size + pc.dim(' projects')
247247
: '';
248248

249249
return (
250-
chalk.dim('Ran all test suites') +
250+
pc.dim('Ran all test suites') +
251251
testInfo +
252252
nameInfo +
253253
contextInfo +
254-
chalk.dim('.')
254+
pc.dim('.')
255255
);
256256
}
257257
}

packages/jest-reporters/src/VerboseReporter.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type {WriteStream} from 'tty';
9-
import chalk = require('chalk');
9+
import * as pc from 'picocolors';
1010
import type {
1111
AggregatedResult,
1212
AssertionResult,
@@ -115,13 +115,13 @@ export default class VerboseReporter extends DefaultReporter {
115115

116116
private _getIcon(status: string) {
117117
if (status === 'failed') {
118-
return chalk.red(ICONS.failed);
118+
return pc.red(ICONS.failed);
119119
} else if (status === 'pending') {
120-
return chalk.yellow(ICONS.pending);
120+
return pc.yellow(ICONS.pending);
121121
} else if (status === 'todo') {
122-
return chalk.magenta(ICONS.todo);
122+
return pc.magenta(ICONS.todo);
123123
} else {
124-
return chalk.green(ICONS.success);
124+
return pc.green(ICONS.success);
125125
}
126126
}
127127

@@ -130,7 +130,7 @@ export default class VerboseReporter extends DefaultReporter {
130130
const time = test.duration
131131
? ` (${formatTime(Math.round(test.duration))})`
132132
: '';
133-
this._logLine(`${status} ${chalk.dim(test.title + time)}`, indentLevel);
133+
this._logLine(`${status} ${pc.dim(test.title + time)}`, indentLevel);
134134
}
135135

136136
private _logTests(tests: Array<AssertionResult>, indentLevel: number) {
@@ -171,7 +171,7 @@ export default class VerboseReporter extends DefaultReporter {
171171
const printedTestStatus =
172172
test.status === 'pending' ? 'skipped' : test.status;
173173
const icon = this._getIcon(test.status);
174-
const text = chalk.dim(`${printedTestStatus} ${test.title}`);
174+
const text = pc.dim(`${printedTestStatus} ${test.title}`);
175175
this._logLine(`${icon} ${text}`, indentLevel);
176176
};
177177
}

packages/jest-reporters/src/formatTestPath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import * as path from 'path';
9-
import chalk = require('chalk');
9+
import * as pc from 'picocolors';
1010
import slash = require('slash');
1111
import type {Config} from '@jest/types';
1212
import relativePath from './relativePath';
@@ -16,5 +16,5 @@ export default function formatTestPath(
1616
testPath: string,
1717
): string {
1818
const {dirname, basename} = relativePath(config, testPath);
19-
return slash(chalk.dim(dirname + path.sep) + chalk.bold(basename));
19+
return slash(pc.dim(dirname + path.sep) + pc.bold(basename));
2020
}

packages/jest-reporters/src/getResultHeader.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import type {TestResult} from '@jest/test-result';
1010
import type {Config} from '@jest/types';
1111
import {formatTime} from 'jest-util';
1212
import formatTestPath from './formatTestPath';
1313
import printDisplayName from './printDisplayName';
1414

15-
const LONG_TEST_COLOR = chalk.reset.bold.bgRed;
15+
const LONG_TEST_COLOR = (str: string) => pc.reset(pc.bold(pc.bgRed(str)));
1616
// Explicitly reset for these messages since they can get written out in the
1717
// middle of error logging
1818
const FAIL_TEXT = 'FAIL';
1919
const PASS_TEXT = 'PASS';
2020

21-
const FAIL = chalk.supportsColor
22-
? chalk.reset.inverse.bold.red(` ${FAIL_TEXT} `)
21+
const FAIL = pc.isColorSupported
22+
? pc.reset(pc.inverse(pc.bold(pc.red(` ${FAIL_TEXT} `))))
2323
: FAIL_TEXT;
2424

25-
const PASS = chalk.supportsColor
26-
? chalk.reset.inverse.bold.green(` ${PASS_TEXT} `)
25+
const PASS = pc.isColorSupported
26+
? pc.reset(pc.inverse(pc.bold(pc.green(` ${PASS_TEXT} `))))
2727
: PASS_TEXT;
2828

2929
export default function getResultHeader(

packages/jest-reporters/src/getSnapshotStatus.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import type {TestResult} from '@jest/test-result';
1010
import {pluralize} from 'jest-util';
1111

1212
const ARROW = ' \u203A ';
1313
const DOT = ' \u2022 ';
14-
const FAIL_COLOR = chalk.bold.red;
15-
const SNAPSHOT_ADDED = chalk.bold.green;
16-
const SNAPSHOT_UPDATED = chalk.bold.green;
17-
const SNAPSHOT_OUTDATED = chalk.bold.yellow;
14+
const FAIL_COLOR = (str: string) => pc.bold(pc.red(str));
15+
const SNAPSHOT_ADDED = (str: string) => pc.bold(pc.green(str));
16+
const SNAPSHOT_UPDATED = (str: string) => pc.bold(pc.green(str));
17+
const SNAPSHOT_OUTDATED = (str: string) => pc.bold(pc.yellow(str));
1818

1919
export default function getSnapshotStatus(
2020
snapshot: TestResult['snapshot'],

packages/jest-reporters/src/getSnapshotSummary.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
8+
import * as pc from 'picocolors';
99
import type {SnapshotSummary} from '@jest/test-result';
1010
import type {Config} from '@jest/types';
1111
import {pluralize} from 'jest-util';
@@ -14,13 +14,13 @@ import formatTestPath from './formatTestPath';
1414
const ARROW = ' \u203A ';
1515
const DOWN_ARROW = ' \u21B3 ';
1616
const DOT = ' \u2022 ';
17-
const FAIL_COLOR = chalk.bold.red;
18-
const OBSOLETE_COLOR = chalk.bold.yellow;
19-
const SNAPSHOT_ADDED = chalk.bold.green;
20-
const SNAPSHOT_NOTE = chalk.dim;
21-
const SNAPSHOT_REMOVED = chalk.bold.green;
22-
const SNAPSHOT_SUMMARY = chalk.bold;
23-
const SNAPSHOT_UPDATED = chalk.bold.green;
17+
const FAIL_COLOR = (str: string) => pc.bold(pc.red(str));
18+
const OBSOLETE_COLOR = (str: string) => pc.bold(pc.yellow(str));
19+
const SNAPSHOT_ADDED = (str: string) => pc.bold(pc.green(str));
20+
const SNAPSHOT_NOTE = pc.dim;
21+
const SNAPSHOT_REMOVED = (str: string) => pc.bold(pc.green(str));
22+
const SNAPSHOT_SUMMARY = pc.bold;
23+
const SNAPSHOT_UPDATED = (str: string) => pc.bold(pc.green(str));
2424

2525
export default function getSnapshotSummary(
2626
snapshots: SnapshotSummary,

0 commit comments

Comments
 (0)