unit-tests
Use when writing, reviewing, speeding up, or fixing a flaky `bun test` unit test in any package (core, cli, server, ai, codemode, util, client, sdk, plugin, http-recorder, merman, app src and test-browser, desktop, gui-plugins, ui, session-ui, latex, ...), except OpenTUI renderer tests (see tui-tests). Also use when a test "timed out after 5000ms" on Windows CI, fails only on Windows, when the unit job is slow, when adding cli subprocess or service tests, or when adding a package's test task to turbo.json. Covers controlling time instead of sleeping, per-test timeouts, process and git spawn cost on Windows, Windows-only failures, temp dirs and process state, leaks between files, and reading CI timeouts.
SKILL.md 原文预览
Fast, Reliable Unit Tests
CI runs bun turbo test --affected --concurrency=3 on 4-vCPU Linux and Windows runners (unit job in .github/workflows/check-test.yml), so each suite shares the CPU with two others, and Windows is the slower leg. Most slow or flaky unit tests here wait on real time, spawn too many processes, or assert wall-clock speed.
Read these first; this skill does not repeat them:
- Root
AGENTS.md: the Testing and Style Guide sections. packages/app/AGENTS.mdTests (packages/desktopandpackages/gui-pluginsfollow it): one owner per contract, and no production seams for tests.packages/ai/AGENTS.md: cassettes. Theeffectskill:it.effectandit.live.
Sibling skills: tui-tests (OpenTUI renderer tests in tui and cli), app-e2e-tests (Playwright in packages/app/e2e), component-tests (Playwright on stories, packages/stories). This skill owns cli subprocess and service tests.
Know the budget
- Bun's default per-test timeout is 5000ms. Check the package's
testscript: - 30s: ai, cli, desktop, http-recorder, latex, merman, simulation, theme and tui pass
--timeout 30000; core'sscript/test.tspasses--timeout=30000. - 5s: client, httpapi-codegen and sdk pass
--timeout 5000; app, codemode, function, gui-plugins, plugin, plugin-browser, script, server, session-ui, ui and util use the default. - Size Windows risk by counting spawns, not by local wall time.
- npm.test.ts "installs a Git package from an npm ::path: subdirectory at a branch" takes 0.4s locally and hit 5000ms on Windows CI. Each Npm.add Git case makes about 75 git process events: 3
clone --recurse-submodules, which run the git-submodule shell script. - Assume roughly 40ms per Git-for-Windows spawn (estimated from an earlier git.test run on Windows CI: ~24s for 564 spawns). Some core file groups run 5-9x slower on Windows than locally.
- Those
test.eachcases (npm.test.ts:223,:251) hit 5000ms in 13 of 45 Windows core runs; that is why core's runner now defaults to 30s (comment atpackages/core/script/test.ts:38). - A timeout loosens the budget only when it exceeds the package default.
- In a 5s package, work that legitimately needs longer gets a per-test timeout and a comment naming the work:
packages/server/test/fetch.test.ts:406-407("Real retries wait 18 \* 200 ms ...",{ timeout: 10_000 }).test.each(table)(name, fn, 30_000)takes it as the third argument. Use file-widesetDefaultTimeoutonly when every test in the file does such work. - In core, the four file-level
setDefaultTimeout(30_000)calls only restate the runner default, and a per-test{ timeout: 15_000 }(still intool-shell.test.ts,session-generate.test.tsand others) is stricter than it. Do not add values at or below 30s there. - Never absorb a sleep, poll or production interval with a larger timeout,
--retry, orskipIf(win32); remove the wait. Skip on Windows only when the platform lacks the capability, with a comment (npm.test.ts:313, symlinks).
Control time; do not wait for it
- Effect code.
it.effectfrompackages/core/test/lib/effect.tsruns onTestClock;testEffect(layer)adds your layer. server and cli import it from../../core/test/lib/effect; ai haspackages/ai/test/lib/effect.ts. - Fork, then adjust. Fork with
Effect.forkChild({ startImmediately: true })(packages/ai/test/http-timeout.test.ts:58), or await a Deferred the code completes and thenEffect.yieldNow(:80-81). OtherwiseTestClock.adjustcan run before the sleep is registered, and the join hangs until Bun's timeout. - Debounced streams and watchers.
advance(condition)anddraininpackages/core/test/lib/clock.tsalternate one real 1ms macrotask with 500ms adjusts, because debounces register sleeps across fiber hops. Usedrain(4 such steps) before a "nothing happened" assertion instead ofBun.sleep(25).advanceis bounded by 15s of real time, not a step count, because its condition can also wait on slow real work (a plugin import, a child, a large write) that took more than 100 steps on a loaded Linux runner (supervisor-reload.test.ts"retains a discovered plugin change during initial activation"). - Real I/O between virtual steps.
TestClock.withLive(Effect.sleep("10 millis")).registered()atpackages/cli/test/service.test.ts:740tests the 5s registration watch with real file I/O underit.effect. This departs from the effect skill's "filesystem →it.live" on purpose: the timer is the contract. - Real events need
it.live. Underit.effectthe clock moves only when the test adjusts it, so an Effect timeout on a path driven by real processes, sockets or file events never fires and a missing event hangs instead of failing. The AppProcess tests (packages/core/test/process/process.test.ts:13-14) useit.livebecause the spawner's 1-second post-exit capture deadline (packages/util/src/cross-spawn-spawner.ts:322) never fired under TestClock. - Give a racing component its own clock. When a real-time deadline races slow setup, provide that component a
Clockthat only the test moves past the deadline.packages/core/test/browser-idle.test.ts:24-38wraps the live clock for `Locati
评论
