Skip to content

test: make drift output test independent of colour detection - #7057

Draft
rrama wants to merge 1 commit into
mainfrom
test/drift-output-colour-independent
Draft

test: make drift output test independent of colour detection#7057
rrama wants to merge 1 commit into
mainfrom
test/drift-output-colour-independent

Conversation

@rrama

@rrama rrama commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Pull Request Submission Checklist

  • Follows CONTRIBUTING guidelines
  • Commit messages are release-note ready
  • Includes detailed description of changes
  • Contains risk assessment (Low)
  • Highlights breaking API changes (if applicable) — none
  • Links to automated tests covering new functionality — the change is the test
  • Includes manual testing instructions
  • Updates relevant GitBook documentation — n/a
  • Includes product update to be announced in the next stable release notes — n/a, test-only

What does this PR do?

Makes test/jest/unit/lib/iac/drift/drift.spec.tsTest describe output › test output for known analysis pass regardless of the terminal it runs in.

That test compares getHumanReadableAnalysis output against the fixtures/all.console golden file, which contains hard-coded ANSI escapes (\e[1m…\e[22m, \e[34m…\e[39m). Whether those escapes are emitted at all is decided by chalk from the ambient environment, so today the test passes only when chalk happens to think colour is supported.

Concretely: chalk 2.4.2 depends on supports-color 5.5.0, which returns level 0 for a non-TTY stream before it gets as far as checking CI vendor variables. CircleCI runs steps under a pty, so the test passes in CI. Anywhere jest output is piped — a plain local run, an editor-integrated runner, a container, a sandboxed agent — chalk emits plain text and the test fails with a large and fairly baffling whitespace-looking diff:

-   Managed Resources: [1m8[22m
+   Managed Resources: 8

This wasn't hypothetical: it's reproducible on a normal dev machine with full network access and valid credentials, and it was originally reported as an environment problem because the failure looks nothing like a colour issue.

The fix pins chalk's colour level for that describe block and restores the previous value afterwards, so the assertion covers the formatting logic rather than the terminal it happens to run in.

Where should the reviewer start?

The whole diff is ~16 lines in drift.spec.ts. Worth confirming you agree with the direction: this keeps the golden file asserting on colours (rather than stripping ANSI from both sides, which would silently stop testing them).

How should this be manually tested?

npx jest --runInBand test/jest/unit/lib/iac/drift/drift.spec.ts                 # passes (fails on main)
FORCE_COLOR=1 npx jest --runInBand test/jest/unit/lib/iac/drift/drift.spec.ts   # passes
FORCE_COLOR=0 npx jest --runInBand test/jest/unit/lib/iac/drift/drift.spec.ts   # passes (fails on main)

On main the first and third fail. Full npm run test:unit was also run: this suite passes and nothing else changed behaviour.

What's the product update that needs to be communicated to CLI users?

None — test-only change, no shipped code touched.

Risk assessment: Low

Test-only. The chalk level is scoped to one describe block and restored in afterAll, and jest gives each test file its own module registry, so no other suite is affected.

Made with Cursor

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 <cursoragent@cursor.com>
@snyk-io

snyk-io Bot commented Jul 30, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant