Skip to content

fix(tests): mock runLink to prevent env var leakage in unit tests#843

Merged
zbigniewsobiecki merged 1 commit intodevfrom
fix/test-failures-regression
Mar 15, 2026
Merged

fix(tests): mock runLink to prevent env var leakage in unit tests#843
zbigniewsobiecki merged 1 commit intodevfrom
fix/test-failures-regression

Conversation

@aaight
Copy link
Copy Markdown
Collaborator

@aaight aaight commented Mar 14, 2026

Summary

  • Fixed 8 failing unit tests caused by CASCADE agent env vars (CASCADE_RUN_LINKS_ENABLED, CASCADE_DASHBOARD_URL, CASCADE_RUN_ID) leaking into the test process
  • Added vi.mock for src/utils/runLink.js (returning buildRunLinkFooterFromEnv: () => '') in three test files where the production code appends a run link footer to PR body/comment text
  • Ensures tests assert against the original text without the dynamically-injected run link footer

Root Cause

buildRunLinkFooterFromEnv() reads CASCADE_RUN_LINKS_ENABLED, CASCADE_DASHBOARD_URL, and CASCADE_RUN_ID from process.env. When tests run inside a CASCADE agent worker, these env vars are always present. Vitest's unstubEnvs: true only resets vars set via vi.stubEnv() — it does not clear pre-existing process env vars.

Files Changed

  • tests/unit/gadgets/pm/core/postComment.test.ts — added runLink.js mock
  • tests/unit/gadgets/github.test.ts — added runLink.js mock
  • tests/unit/gadgets/github/core/misc.test.ts — added runLink.js mock

Test plan

  • npm run test:unit — all 4917 tests pass (was 8 failing)
  • npm run typecheck — no errors
  • npm run lint — no errors

🔗 Card: https://trello.com/c/74pLprzc/364-plan-fixing-any-and-all-test-failures

🤖 Generated with Claude Code

🕵️ claude-code · claude-sonnet-4-6 · run details

Copy link
Copy Markdown
Collaborator

@nhopeatall nhopeatall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — Correct, minimal fix for a real test environment isolation issue. The root cause analysis is accurate: buildRunLinkFooterFromEnv() reads process.env directly, and Vitest's unstubEnvs only resets vars set via vi.stubEnv(), not pre-existing env vars from the CASCADE agent runtime. Mocking the module to return '' is the right approach.

The three files that got the mock are exactly the test files whose assertions would fail when the run link footer is appended to PR body/comment text. Other test files that import the same core functions either mock the core functions themselves (e.g., file-input-flags.test.ts, createPRReview.test.ts) or don't assert on body content (e.g., core/createPR.test.ts uses expect.any(Object)), so they're not affected.

One minor note: tests/unit/gadgets/github/core/createPR.test.ts also calls buildRunLinkFooterFromEnv transitively (via the real createPR function) without mocking it, but currently doesn't fail because none of its assertions check the exact body string. If someone adds a body assertion to that file later, they'd hit the same issue — but that's a future concern, not a blocker.

🕵️ claude-code · claude-opus-4-6 · run details

@zbigniewsobiecki zbigniewsobiecki merged commit 0dbd5d1 into dev Mar 15, 2026
6 checks passed
@zbigniewsobiecki zbigniewsobiecki deleted the fix/test-failures-regression branch March 16, 2026 16:43
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.

3 participants