From 916fd8093ebb62af05078ec899c9f60cd569a8f0 Mon Sep 17 00:00:00 2001 From: Phillip9587 Date: Thu, 9 Jan 2025 12:43:56 +0100 Subject: [PATCH] cleanup(testing): migrate jest to `picocolors` migrates `@nx/jest` from `chalk` to `picocolors` --- packages/jest/.eslintrc.json | 19 +++++++- packages/jest/package.json | 2 +- packages/jest/src/executors/jest/summary.ts | 50 ++++++++++----------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/packages/jest/.eslintrc.json b/packages/jest/.eslintrc.json index 9c91d83d993d0..c1815f915c384 100644 --- a/packages/jest/.eslintrc.json +++ b/packages/jest/.eslintrc.json @@ -4,13 +4,28 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} + "rules": { + "no-restricted-imports": [ + "error", + { + "name": "chalk", + "message": "Please use `picocolors` in place of `chalk` for rendering terminal colors" + } + ] + } }, { "files": ["**/*.ts"], "excludedFiles": ["./src/migrations/**"], "rules": { - "no-restricted-imports": ["error", "@nx/workspace"] + "no-restricted-imports": [ + "error", + "@nx/workspace", + { + "name": "chalk", + "message": "Please use `picocolors` in place of `chalk` for rendering terminal colors" + } + ] } }, { diff --git a/packages/jest/package.json b/packages/jest/package.json index b378d8b72eec3..237c1068f1871 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -40,12 +40,12 @@ "@nx/devkit": "file:../devkit", "@nx/js": "file:../js", "@phenomnomnominal/tsquery": "~5.0.1", - "chalk": "^4.1.0", "identity-obj-proxy": "3.0.0", "jest-config": "^29.4.1", "jest-resolve": "^29.4.1", "jest-util": "^29.4.1", "minimatch": "9.0.3", + "picocolors": "^1.1.0", "resolve.exports": "2.0.3", "semver": "^7.5.3", "tslib": "^2.3.0", diff --git a/packages/jest/src/executors/jest/summary.ts b/packages/jest/src/executors/jest/summary.ts index b578629f596c9..581247a85d682 100644 --- a/packages/jest/src/executors/jest/summary.ts +++ b/packages/jest/src/executors/jest/summary.ts @@ -1,6 +1,6 @@ import { AggregatedResult } from '@jest/reporters'; import { pluralize, formatTime } from 'jest-util'; -import * as chalk from 'chalk'; +import * as pc from 'picocolors'; /** * Copied from the jest repo because these utility functions are not exposed through the package @@ -50,9 +50,9 @@ const renderTime = (runTime: number, estimatedTime: number, width: number) => { // If we are more than one second over the estimated time, highlight it. const renderedTime = estimatedTime && runTime >= estimatedTime + 1 - ? chalk.bold.yellow(formatTime(runTime, 0)) + ? pc.bold(pc.yellow(formatTime(runTime, 0))) : formatTime(runTime, 0); - let time = chalk.bold(`Time:`) + ` ${renderedTime}`; + let time = pc.bold(`Time:`) + ` ${renderedTime}`; if (runTime < estimatedTime) { time += `, estimated ${formatTime(estimatedTime, 0)}`; } @@ -68,8 +68,8 @@ const renderTime = (runTime: number, estimatedTime: number, width: number) => { if (availableWidth >= 2) { time += '\n' + - chalk.green('█').repeat(length) + - chalk.white('█').repeat(availableWidth - length); + pc.green('█').repeat(length) + + pc.white('█').repeat(availableWidth - length); } } return time; @@ -120,12 +120,12 @@ export const getSummary = ( const width = options?.width || 0; const suites = - chalk.bold('Test Suites: ') + - (suitesFailed ? chalk.bold.red(`${suitesFailed} failed`) + ', ' : '') + + pc.bold('Test Suites: ') + + (suitesFailed ? pc.bold(pc.red(`${suitesFailed} failed`)) + ', ' : '') + (suitesPending - ? chalk.bold.yellow(`${suitesPending} skipped`) + ', ' + ? pc.bold(pc.yellow(`${suitesPending} skipped`)) + ', ' : '') + - (suitesPassed ? chalk.bold.green(`${suitesPassed} passed`) + ', ' : '') + + (suitesPassed ? pc.bold(pc.green(`${suitesPassed} passed`)) + ', ' : '') + (suitesRun !== suitesTotal ? suitesRun + ' of ' + suitesTotal : suitesTotal) + @@ -138,50 +138,50 @@ export const getSummary = ( const updatedTestsTotal = testsTotal + numTotalTests; const tests = - chalk.bold('Tests: ') + + pc.bold('Tests: ') + (updatedTestsFailed > 0 - ? chalk.bold.red(`${updatedTestsFailed} failed`) + ', ' + ? pc.bold(pc.red(`${updatedTestsFailed} failed`)) + ', ' : '') + (updatedTestsPending > 0 - ? chalk.bold.yellow(`${updatedTestsPending} skipped`) + ', ' + ? pc.bold(pc.yellow(`${updatedTestsPending} skipped`)) + ', ' : '') + (updatedTestsTodo > 0 - ? chalk.bold.magenta(`${updatedTestsTodo} todo`) + ', ' + ? pc.bold(pc.magenta(`${updatedTestsTodo} todo`)) + ', ' : '') + (updatedTestsPassed > 0 - ? chalk.bold.green(`${updatedTestsPassed} passed`) + ', ' + ? pc.bold(pc.green(`${updatedTestsPassed} passed`)) + ', ' : '') + `${updatedTestsTotal} total`; const snapshots = - chalk.bold('Snapshots: ') + + pc.bold('Snapshots: ') + (snapshotsFailed - ? chalk.bold.red(`${snapshotsFailed} failed`) + ', ' + ? pc.bold(pc.red(`${snapshotsFailed} failed`)) + ', ' : '') + (snapshotsOutdated && !snapshotsDidUpdate - ? chalk.bold.yellow(`${snapshotsOutdated} obsolete`) + ', ' + ? pc.bold(pc.yellow(`${snapshotsOutdated} obsolete`)) + ', ' : '') + (snapshotsOutdated && snapshotsDidUpdate - ? chalk.bold.green(`${snapshotsOutdated} removed`) + ', ' + ? pc.bold(pc.green(`${snapshotsOutdated} removed`)) + ', ' : '') + (snapshotsFilesRemoved && !snapshotsDidUpdate - ? chalk.bold.yellow( + ? pc.bold(pc.yellow( pluralize('file', snapshotsFilesRemoved) + ' obsolete' - ) + ', ' + )) + ', ' : '') + (snapshotsFilesRemoved && snapshotsDidUpdate - ? chalk.bold.green( + ? pc.bold(pc.green( pluralize('file', snapshotsFilesRemoved) + ' removed' - ) + ', ' + )) + ', ' : '') + (snapshotsUpdated - ? chalk.bold.green(`${snapshotsUpdated} updated`) + ', ' + ? pc.bold(pc.green(`${snapshotsUpdated} updated`)) + ', ' : '') + (snapshotsAdded - ? chalk.bold.green(`${snapshotsAdded} written`) + ', ' + ? pc.bold(pc.green(`${snapshotsAdded} written`)) + ', ' : '') + (snapshotsPassed - ? chalk.bold.green(`${snapshotsPassed} passed`) + ', ' + ? pc.bold(pc.green(`${snapshotsPassed} passed`)) + ', ' : '') + `${snapshotsTotal} total`;