Skip to content

fix(ui-web): skip dev-only diagnostics under Vitest - #1897

Merged
jerome-benoit merged 1 commit into
mainfrom
fix/token-contract-vitest-guard
Jun 14, 2026
Merged

jerome-benoit merged 1 commit into
mainfrom
fix/token-contract-vitest-guard

Conversation

@jerome-benoit

@jerome-benoit jerome-benoit commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extract isDev() helper in ui/web/src/core/env.ts returning true only when running in real DEV (excludes production build and Vitest). Migrate the 7 existing import.meta.env.DEV check sites to the helper.

Root cause

validateTokenContract schedules a requestAnimationFrame whose body calls console.warn when CSS custom properties are missing. jsdom does not resolve --color-* tokens, so every theme/skin switch under Vitest 4 queued a console.warn that fired after the environment was torn down, surfacing as:

EnvironmentTeardownError: [vitest-worker]: Closing rpc while "onUserConsoleLog" was pending

Reproducibly fails on Linux/Node 22 in CI; passes on macOS/Node 24 due to rAF timing differences. The vi.dynamicImportSettled() global drain added in #1893 covers dynamic imports, not rAF callbacks.

Scope

Single helper at the canonical source. No production-API change.

Site Before After
core/env.ts (new file) isDev() helper
shared/tokens/contract.ts:55 (rAF leak) if (!import.meta.env.DEV || ...) if (!isDev() || ...)
core/storage.ts:8,24,34,59 if (import.meta.env.DEV) × 4 if (isDev()) × 4
core/providers.ts:17 if (import.meta.env.DEV) if (isDev())
main.ts:43 if (import.meta.env.DEV) if (isDev())
skins/classic/components/buttons/ToggleButton.vue:51 if (import.meta.env.DEV) if (isDev())

Side effect: silences console.debug / console.info noise from storage.ts, providers.ts, ToggleButton.vue, main.ts under Vitest. dev/build behaviour unchanged.

Verification

  • New regression test in ui/web/tests/unit/shared/tokens/contract.test.ts asserts requestAnimationFrame is not called by validateTokenContract under Vitest
  • pnpm test — 532/532
  • pnpm typecheck, pnpm lint, pnpm build — clean
  • grep import.meta.env.DEV ui/web/src/ — single occurrence, in core/env.ts (single source of truth)

PR Checklist

Does this PR introduce a breaking change?

  • Yes
  • No

@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


fix(ui-web): Skip validateTokenContract under Vitest to prevent environment teardown errors

Bug Fix

🐛 Resolves an EnvironmentTeardownError in CI (Linux/Node 22) caused by validateTokenContract scheduling requestAnimationFrame callbacks that fire console.warn after the Vitest environment is torn down. The fix adds a Vitest-mode guard so the function becomes a no-op during test runs, eliminating the rAF leak for all current and future callers (useTheme.ts, useSkin.ts).

Root cause: jsdom does not resolve --color-* CSS custom properties, so every theme/skin switch under Vitest queued console.warn calls via requestAnimationFrame. Vitest 4 tightened teardown to fail on pending RPC calls, surfacing this as:

EnvironmentTeardownError: [vitest-worker]: Closing rpc while "onUserConsoleLog" was pending

This was reproducible on Linux/Node 22 in CI but not on macOS/Node 24 due to rAF timing differences.

Changes

  • ui/web/src/shared/tokens/contract.ts: Extended the existing DEV-only guard with import.meta.env.MODE === 'test' so validateTokenContract returns early under Vitest. Production and dev builds are completely unaffected. Updated the JSDoc comment to document the Vitest no-op behavior.
  • ui/web/tests/unit/shared/tokens/contract.test.ts: Added a regression test suite for validateTokenContract asserting that neither requestAnimationFrame nor console.warn is invoked when called under Vitest. Updated imports to include afterEach, vi, and validateTokenContract.

Images and Links (ONLY add this section if the links are explicitly provided in the original content)

  • #1884: Mock useTheme/useSkin in SimulatorBar tests to fix Vitest 4.x teardown RPC leak
  • #1893: Drain dynamic imports globally for async components

  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.22.9

@hyperspace-pr-bot hyperspace-pr-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix itself is correct and well-scoped — the MODE === 'test' guard cleanly resolves the rAF/teardown race without touching production paths. Two minor issues were flagged in the new test: a redundant afterEach block (already covered by the global restoreMocks: true config), and a vacuous console.warn assertion that can never fail independently of the requestAnimationFrame spy check.

PR Bot Information

Version: 1.22.9

Comment on lines +28 to +30
afterEach(() => {
vi.restoreAllMocks()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Best Practices: afterEach(() => vi.restoreAllMocks()) is redundant

vitest.config.ts already sets restoreMocks: true globally, which automatically restores all spies after each test. This local afterEach block duplicates that behaviour and adds noise.

Consider removing it entirely.

Suggested change
afterEach(() => {
vi.restoreAllMocks()
})

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in 30565ba. Single-test file makes belt-and-braces convention from #1884 unnecessary here.

Comment on lines +32 to +38
it('should be a no-op under Vitest', () => {
const rafSpy = vi.spyOn(globalThis, 'requestAnimationFrame')
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
validateTokenContract('useTheme', 'tokyo-night-storm')
expect(rafSpy).not.toHaveBeenCalled()
expect(warnSpy).not.toHaveBeenCalled()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Logic Error: warnSpy assertion provides no additional coverage

console.warn is only reachable inside the requestAnimationFrame callback, which is never scheduled because the early-return guard fires first. expect(warnSpy).not.toHaveBeenCalled() can never fail independently of expect(rafSpy).not.toHaveBeenCalled() — if rAF is not called, warn cannot be called either. The assertion is therefore vacuous and misleads future readers into thinking console.warn is directly guarded.

The spy on console.warn and its assertion can be removed, leaving only the rafSpy assertion to test the guard behaviour.

Suggested change
it('should be a no-op under Vitest', () => {
const rafSpy = vi.spyOn(globalThis, 'requestAnimationFrame')
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
validateTokenContract('useTheme', 'tokyo-night-storm')
expect(rafSpy).not.toHaveBeenCalled()
expect(warnSpy).not.toHaveBeenCalled()
})
it('should be a no-op under Vitest', () => {
const rafSpy = vi.spyOn(globalThis, 'requestAnimationFrame')
validateTokenContract('useTheme', 'tokyo-night-storm')
expect(rafSpy).not.toHaveBeenCalled()
})

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied in 30565ba. console.warn is unreachable when requestAnimationFrame is not called; the rAF spy alone suffices.

@jerome-benoit
jerome-benoit force-pushed the fix/token-contract-vitest-guard branch from 27a6dc4 to c03c125 Compare June 14, 2026 01:19
@jerome-benoit jerome-benoit changed the title fix(ui-web): skip token contract validation under Vitest fix(ui-web): skip dev-only diagnostics under Vitest Jun 14, 2026
@jerome-benoit
jerome-benoit force-pushed the fix/token-contract-vitest-guard branch from c03c125 to 30565ba Compare June 14, 2026 01:24
Extract isDev() helper in core/env.ts that returns true only in real
DEV (excludes production build and Vitest). Migrate the 7 existing
import.meta.env.DEV check sites to the helper.

Primary motivation: validateTokenContract schedules a
requestAnimationFrame whose body calls console.warn for missing CSS
tokens. jsdom does not resolve --color-* tokens, so every theme/skin
switch under Vitest 4 queued a console.warn that fired after
environment teardown — surfacing as EnvironmentTeardownError on
Linux/Node 22; passes on macOS/Node 24.

Side effect: silences console.debug noise from storage.ts,
providers.ts, ToggleButton.vue under Vitest. dev/build behaviour
unchanged.

Adds regression test in tests/unit/shared/tokens/contract.test.ts.
@jerome-benoit
jerome-benoit force-pushed the fix/token-contract-vitest-guard branch from 30565ba to 8aeb395 Compare June 14, 2026 01:28
@jerome-benoit
jerome-benoit merged commit 18c3847 into main Jun 14, 2026
34 checks passed
@jerome-benoit
jerome-benoit deleted the fix/token-contract-vitest-guard branch June 15, 2026 19:49
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