← ClaudeAtlas

mycobun-test-runtime-hardeninglisted

Apply this skill when hardening Bun test environments in Myco's test suite, diagnosing CI-only test failures, investigating hung test processes after all assertions pass, or adding new test files that import external SDK modules, use mock.module(), or declare module-level timers — even if the user doesn't explicitly ask about Bun runtime behavior. Covers three procedures: (1) isolating process-scoped mock.module() registrations that leak across test files and cause non-deterministic CI failures; (2) scoping module-level side effects (timers, stubs) to individual tests with explicit afterEach cleanup to prevent suite-exit hangs; and (3) lazily initializing external SDK clients (e.g., Anthropic) that eagerly construct at module load time under Bun's browser-like test environment. The shared root cause: Bun runs tests in a process-shared environment where module-scope state — mock registrations, timers, SDK clients — persists beyond the originating file.
goondocks-co/myco · ★ 13 · Testing & QA · score 79
Install: claude install-skill goondocks-co/myco
# Bun Test Runtime Hardening Bun runs its test suite in a shared process where module-scope state persists across test files. This creates three categories of hard-to-diagnose failures: process-scoped mock registrations that poison unrelated tests, module-level timers that block suite exit, and eager SDK construction that fails in Bun's browser-like environment. These issues typically surface only on CI — due to non-deterministic file execution order — or after extended test runs. ## Prerequisites - You are working within Myco's monorepo test suite, executed via `npm test` or direct `bun test` invocations - Understand that `npm test` delegates to `node scripts/run-bun-tests.mjs`, which orchestrates multiple Bun process invocations. Files in the same `NO_ISOLATE_NODE_GROUPS` entry share a Bun process. - To reproduce CI ordering locally, run `bun test --watch=false` on the same group of files that CI bundles together (e.g., the `tests-agent-stable` group in `scripts/run-bun-tests.mjs`). ## Procedure A: Isolating Process-Scoped mock.module() Registrations **When to apply:** A test file uses top-level `mock.module()` calls outside any `beforeEach`/`describe` block, and other test files that import the same module fail non-deterministically on CI. **Root cause:** Bun's `mock.module()` is **process-scoped, not file-scoped**. When multiple test files share one Bun process (i.e., they are in the same `NO_ISOLATE_NODE_GROUPS` entry in `scripts/run-bun-tests.mjs`), a to