useColor.test.js
This file
Path
tests/useColor.test.js
90 lines · 3.1 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–3why, one step back1import { useColor } from '../lib/command.js';2import { test, describe, beforeEach, after } from 'node:test';3import assert from 'node:assert/strict';Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 4–15why, one step back45describe('internal useColor environment variable support', () => {6 let holdNoColor = process.env.NO_COLOR;7 let holdForceColor = process.env.FORCE_COLOR;8 let holdCliColorForce = process.env.CLICOLOR_FORCE;910 beforeEach(() => {11 delete process.env.NO_COLOR;12 delete process.env.FORCE_COLOR;13 delete process.env.CLICOLOR_FORCE;14 });15Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 16why, one step back16 after(() => {Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 17–27why, one step back17 if (holdNoColor === undefined) delete process.env.NO_COLOR;18 else process.env.NO_COLOR = holdNoColor;1920 if (holdForceColor === undefined) delete process.env.FORCE_COLOR;21 else process.env.FORCE_COLOR = holdForceColor;2223 if (holdCliColorForce === undefined) delete process.env.CLICOLOR_FORCE;24 else process.env.CLICOLOR_FORCE = holdCliColorForce;25 });2627 test('when no ENV defined then returns undefined', () => {Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 28why, one step back28 assert.equal(useColor(), undefined);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 29–37why, one step back29 });3031 // https://no-color.org32 //33 // Command-line software which adds ANSI color to its output by default should check for a NO_COLOR environment variable that,34 // when present and not an empty string (regardless of its value), prevents the addition of ANSI color.3536 test('when NO_COLOR defined then returns false', () => {37 process.env.NO_COLOR = 'non-empty';Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 38why, one step back38 assert.equal(useColor(), false);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 39–42why, one step back39 });4041 test('when NO_COLOR empty then returns undefined', () => {42 process.env.NO_COLOR = '';Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 43why, one step back43 assert.equal(useColor(), undefined);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 44–49why, one step back44 });4546 // https://bixense.com/clicolors/4748 test('when CLICOLOR_FORCE defined then returns true', () => {49 process.env.CLICOLOR_FORCE = '1';Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 50why, one step back50 assert.equal(useColor(), true);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 51–55why, one step back51 });5253 test('when CLICOLOR_FORCE empty then returns true', () => {54 // Follow original Apple usage and test for existence, don't ignore empty value.55 process.env.CLICOLOR_FORCE = '';Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 56why, one step back56 assert.equal(useColor(), true);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 57–62why, one step back57 });5859 test('when CLICOLOR_FORCE and NO_COLOR defined then returns false', () => {60 // NO_COLOR trumps CLICOLOR_FORCE61 process.env.NO_COLOR = '1';62 process.env.CLICOLOR_FORCE = '1';Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 63why, one step back63 assert.equal(useColor(), false);Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 64–73why, one step back64 });6566 // chalk: https://github.com/chalk/supports-color/blob/c214314a14bcb174b12b3014b2b0a8de375029ae/index.js#L3367 // node: https://github.com/nodejs/node/blob/0a00217a5f67ef4a22384cfc80eb6dd9a917fdc1/lib/internal/tty.js#L10968 // (https://force-color.org recent web page from 2023, does not match major javascript implementations)69 //70 // Chalk ignores anything except for 0,1,2,3,4,true,false values.71 // Node somewhat follows Chalk with 0,1,2,3,true, but treats empty as true and unexpected values as false.72 // Test the expected Chalk values (which do produce same result in node).73Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 74–88why, one step back74 describe('FORCE_COLOR environment variable tests', () => {75 const testCases = [76 ['true', true],77 ['false', false],78 ['0', false],79 ['1', true],80 ['2', true],81 ['3', true],82 ];83 for (const [value, result] of testCases) {84 test(`when FORCE_COLOR=${value} then returns ${result}`, () => {85 process.env.FORCE_COLOR = value;86 assert.equal(useColor(), result);87 });88 }Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
lines 89–90why, one step back89 });90});