From b491489cb2c3d7c8238faf5ba06543049ca2e14c Mon Sep 17 00:00:00 2001 From: Phillip Barta Date: Thu, 9 Jan 2025 15:58:44 +0100 Subject: [PATCH] cleanup(testing): migrate jest to `picocolors` (#29561) migrates `@nx/jest` from `chalk` to `picocolors` Part of https://github.com/es-tooling/ecosystem-cleanup/issues/117 ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- packages/jest/.eslintrc.json | 14 +++--- packages/jest/package.json | 2 +- packages/jest/src/executors/jest/summary.ts | 50 ++++++++++----------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/packages/jest/.eslintrc.json b/packages/jest/.eslintrc.json index 9c91d83d993d0..fb144644e6c3b 100644 --- a/packages/jest/.eslintrc.json +++ b/packages/jest/.eslintrc.json @@ -4,13 +4,15 @@ "overrides": [ { "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], - "rules": {} - }, - { - "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..8e09ea52c0c78 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( - pluralize('file', snapshotsFilesRemoved) + ' obsolete' + ? pc.bold( + pc.yellow(pluralize('file', snapshotsFilesRemoved) + ' obsolete') ) + ', ' : '') + (snapshotsFilesRemoved && snapshotsDidUpdate - ? chalk.bold.green( - pluralize('file', snapshotsFilesRemoved) + ' removed' + ? 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`;