writing-tests

Guides writing bun:test unit tests with mock isolation and cross-platform CI rules.

Updated May 31, 2026
One-click install
npx skills add https://github.com/AlexanderNarbaev/agi --skill writing-tests-alexandernarbaev
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-tests
Source: https://github.com/AlexanderNarbaev/agi/tree/main/.opencode/skills/writing-tests
Command: npx skills add https://github.com/AlexanderNarbaev/agi --skill writing-tests-alexandernarbaev

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing tests in a Bun-based TypeScript repository is error-prone: module mocks leak across files in shared processes, Windows path and symlink behavior breaks CI, and misuse of the test_runner tool can stall the agent. This Skill encodes the repository's testing conventions so tests pass consistently on all three CI platforms. ## Core Features & Use Cases - Mock Isolation Rules: Enforces a three-tier mocking strategy (_test_exports, _internals DI seams, mock.module with spread-real-exports) to prevent cross-file mock pollution in Bun's shared module cache. - Cross-Platform Patterns: Covers Windows/macOS/Linux differences for paths, symlinks, temp directories, environment variable redirection (HOME, APPDATA, LOCALAPPDATA, XDG), and line-ending normalization. - CI Structure & File Placement: Documents the six-step CI pipeline, per-file isolation loops, naming conventions (.adversarial.test.ts), regression test format, and test_runner scope safety limits. - Use Case: Before adding a unit test for a new tool in src/tools/, load this Skill to choose the correct mock tier, place the file in tests/unit/tools/, and avoid the isolation bugs that previously caused hundreds of CI failures. ## Quick Start Load the writing-tests skill and help me write a bun:test unit test for my new module with proper mock isolation.

Frequently Asked Questions about writing-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests with bun:test in TypeScript?

Import describe, test, expect, mock, and spyOn from bun:test rather than using vitest APIs. Place unit tests under tests/unit/ mirroring the source directory, and follow the repository's three-tier mock convention for isolation.

How do I prevent mock.module leaks between test files in Bun?

Bun shares the module cache across test files in one process, so mock.module mocks leak globally. Spread the real module exports, add afterEach(mock.restore()), prefer _internals DI seams or _test_exports for pure functions, and run files in per-file isolation loops like CI does.

Why do my Bun tests pass individually but fail when run together?

This indicates mock pollution from a neighboring file, often caused by vi.mock inside beforeEach or hoist-time closure capture. Isolate the failing pair, classify the failure mode, and migrate the polluting file to the _internals DI seam pattern.

Does mock.module require stubbing all exports of a module?

Yes. Bun validates the export set at dynamic-import time and throws SyntaxError if any named export is missing. Stub every runtime export, including Zod schemas, even if your test only calls one of them; type-only exports need no stub.

How do I make filesystem tests work on Windows CI?

Build mock keys with path.resolve instead of hardcoded Unix paths, use os.tmpdir with path.join for temp files, redirect HOME plus APPDATA and LOCALAPPDATA on Windows, and normalize CRLF line endings when comparing file contents.

When should I not use the test_runner tool to run the full suite?

Never use scope 'all' in agent context, and avoid multi-file convention, graph, or impact scopes since guards reject them. Use per-file shell loops with bun --smol test instead, which matches CI behavior and avoids stalling the agent.