Move duration, redact, runtime, secure-file, and semver under core/util; embedded-skill and skill-version under core/skill; update-cache under core/update. Move the dev-override test next to the script it covers, and update the embedded-skill generator output path and regenerated manifest. No behavior change.
2.7 KiB
2.7 KiB
description, globs, trigger
| description | globs | trigger |
|---|---|---|
| Testing conventions, environment isolation, network mocking, and test hygiene using bun:test | src/**/*.test.ts | manual |
Testing Conventions & Test Hygiene
When writing or modifying automated tests in src/:
1. Test Framework & Colocation
- Runner: Use
bun:testprimitives (describe,test,it,expect,beforeEach,afterEach). - Colocation: Place test files directly next to the code they cover using the
*.test.tsnaming convention:src/core/update/update-cache.ts src/core/update/update-cache.test.ts - Do not create isolated
tests/directories unless testing full external integration fixtures.
2. Environment & State Isolation (Zero Leaks)
- Environment Variables:
Any test that overrides or adds
process.envkeys MUST restore them inafterEach()or afinallyblock:const prevEnv = process.env.MY_CUSTOM_VAR; try { process.env.MY_CUSTOM_VAR = "test-value"; // test logic } finally { if (prevEnv !== undefined) { process.env.MY_CUSTOM_VAR = prevEnv; } else { delete process.env.MY_CUSTOM_VAR; } } - Temporary Files & Directories:
Always create isolated temporary workspaces using
mkdtempSync:const testDir = mkdtempSync(join(tmpdir(), "seal-use-test-")); try { // file operations } finally { rmSync(testDir, { recursive: true, force: true }); } - Working Directory (
process.chdir): If a test changes cwd, it MUST captureconst cwd = process.cwd();and restore it inafterEach().
3. Network Mocking & Background Child Processes
- HTTP Requests (
fetch):- Mock
globalThis.fetchto isolate tests from live external networks. - Wrap mocked fetch cleanly to satisfy Bun/TypeScript types:
const originalFetch = globalThis.fetch; globalThis.fetch = (async () => new Response(JSON.stringify({ ok: true }), { status: 200, headers: { "Content-Type": "application/json" } }) ) as any; try { // test logic } finally { globalThis.fetch = originalFetch; }
- Mock
- Background Processes:
- Ensure background asynchronous tasks or detached worker spawns are suppressed in tests:
process.env.SEAL_DISABLE_BG_UPDATE = "1"; - This ensures test suites run fast and deterministically without hanging detached workers.
- Ensure background asynchronous tasks or detached worker spawns are suppressed in tests:
4. Test Execution & Quality Verification
Before committing changes or opening a PR:
bun test: Run all tests (must achieve 100% pass rate).bun test <specific.test.ts>: Target specific files during active development.bun run check: Confirm TypeScript type-checker reports 0 errors.