Bun mock module isolation
Skill kjuhwa/skills-hub/skills/testing/bun-mock-module-isolation
Split per-package Bun tests into multiple `bun test` invocations to avoid irreversible `mock.module()` cache pollution.From its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill bun-mock-module-isolationAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
3.3 KB, 732 tokens by cl100k_base, as published. Nobody here has run it
Bun mock.module() Test Batching for Cross-File Isolation
When to use
- You are on Bun and have multiple test files that each call
mock.module(path, ...)for the same module path with different implementations. - You see ~dozens of unexplained failures only when tests are run together, but they pass individually.
- You cannot migrate off Bun's mock API but need reliable CI runs.
Bun's mock.module() permanently rewrites the module in the process-wide cache. mock.restore() does not undo it (oven-sh/bun#7823). The only reliable isolation is a fresh process per conflicting group.
Steps
- Identify conflict groups. Grep your test files for
mock.module('<path>'. Any path that appears in two or more files with different return shapes is a conflict group. - Assign each conflict group its own
bun testinvocation inside the package'stestscript. Keep non-conflicting files grouped together for speed. - Keep the root
bun test-from-repo-root command off-limits. Document it inCLAUDE.md/CONTRIBUTING.md— root-levelbun testdiscovers everything and reintroduces pollution. - Prefer
spyOn()for intra-package mocking.spy.mockRestore()does work; use it whenever the target is imported by another file in the same package. - When adding a new test file that calls
mock.module(), check whether the same path is mocked elsewhere and, if so, add the new file to its ownbun testinvocation inpackage.json.
Example package.json shape (shortened):
{
"scripts": {
"test": "bun test src/a.test.ts && bun test src/b.test.ts && bun test src/group-c/"
}
}
Archon's @archon/core splits into 7 batches, @archon/workflows into 5, @archon/adapters into 3, @archon/isolation into 3.
Counter / Caveats
- Each extra invocation adds ~JVM-like Bun startup cost; don't split gratuitously. Group non-conflicting files together.
spyOnis not a substitute when the target is in a different package imported via workspace resolution — in practicemock.module()is the only Bun tool that rewires transitive imports.- Root-level
bun testis still useful for quick single-file checks; the batching rule is about CI andbun run test.
Evidence
- Archon root
CLAUDE.md(lines 131-133) codifies the "Do NOT runbun testfrom the repo root" rule and explains why: discovering all files in one process causes ~135 mock pollution failures. packages/core/package.jsonscripts.testchains ~8&&-separatedbun testinvocations, grouping files that conflict on shared mocks.- Archon issue reference in the CLAUDE.md comment: Bun oven-sh/bun#7823.
- Commit SHA: d89bc767d291f52687beea91c9fcf155459be0d9.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.