From b72187a17abcc36216aa48626d73eef951b2efbf Mon Sep 17 00:00:00 2001 From: Ben Durrans Date: Thu, 30 Jul 2026 10:37:19 +0100 Subject: [PATCH] test: make drift output test independent of colour detection The "Test describe output" case compares getHumanReadableAnalysis against the all.console fixture, which contains hard-coded ANSI escapes. Whether those escapes are produced is decided by chalk from the ambient environment, so the test only passes when chalk believes colour is supported. chalk 2.4.2 ships supports-color 5.5.0, which returns level 0 for a non-TTY stream before it considers CI vendor variables. CircleCI runs steps under a pty so the test passes there, but piping jest output anywhere else -- a local run, an editor test runner, a container -- produces plain text and a large confusing diff. Pin chalk's colour level for this block and restore it afterwards, so the assertion tests the formatting rather than the terminal it happens to run in. Verified passing with no FORCE_COLOR, FORCE_COLOR=1 and FORCE_COLOR=0. Co-authored-by: Cursor --- test/jest/unit/lib/iac/drift/drift.spec.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/jest/unit/lib/iac/drift/drift.spec.ts b/test/jest/unit/lib/iac/drift/drift.spec.ts index 224394c13c..7b3d134a9b 100644 --- a/test/jest/unit/lib/iac/drift/drift.spec.ts +++ b/test/jest/unit/lib/iac/drift/drift.spec.ts @@ -1,4 +1,5 @@ import * as mockFs from 'mock-fs'; +import chalk from 'chalk'; import { driftignoreFromPolicy, @@ -396,6 +397,22 @@ describe('Test describe output', () => { return fs.readFileSync(filePath, 'utf-8'); }; + // The .console fixtures contain ANSI colour codes, so the output has to be + // generated with colour on. Chalk otherwise decides that from the ambient + // environment and emits plain text whenever stdout is not a TTY. + const originalLevel = chalk.level; + const originalEnabled = chalk.enabled; + + beforeAll(() => { + chalk.level = 1; + chalk.enabled = true; + }); + + afterAll(() => { + chalk.level = originalLevel; + chalk.enabled = originalEnabled; + }); + it('test output for known analysis', () => { const analysis = JSON.parse(loadFile('all.json')); const options: DescribeOptions = {