Unit testing
Agent skills library for AI coding assistants. Includes coding conventions, npm package preferences, and project bootstrapping tools.
npx -y skills add r-portas/skills --skill unit-testingAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 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.
What its author says it does
Copied from the file, not written here
Roy's conventions for writing tests — Bun test runner, mocking with Bun's built-in mock API, and React component testing with React Testing Library and HappyDOM. Consult whenever writing, editing, or reviewing any test file (*.test.ts, *.test.tsx), or when asked about mocking, test structure, or testing setup.
SKILL.md
4.0 KB, 900 tokens by cl100k_base, as published. Nobody here has run it
Unit Testing
Test runner
Use Bun's built-in test runner — no install needed.
bun test # run all tests
bun test --watch # re-run on file changes
bun test src/lib/foo.test.ts # run a specific file
File naming and location
Place test files next to the source file they test:
src/
├── lib/
│ ├── format-date.ts
│ └── format-date.test.ts
└── components/
├── search-input.tsx
└── search-input.test.tsx
Use *.test.ts for logic and *.test.tsx for React components.
Test structure
Import everything from bun:test:
import { describe, test, expect, beforeEach, afterEach } from "bun:test";
describe("formatDate", () => {
test("formats a date with the default locale", () => {
expect(formatDate(new Date("2024-01-15"))).toBe("Jan 15, 2024");
});
test("returns an empty string for null", () => {
expect(formatDate(null)).toBe("");
});
});
- Use
describeto group related cases; nest only when there's a real hierarchy - Use
testoverit - One logical assertion per
testkeeps failures easy to diagnose
Mocking
Mock at system boundaries — anything that reaches outside the process or produces non-deterministic output: outbound HTTP, file I/O, time, external services.
Do not mock internal modules. If internal modules need isolation, extract a pure function instead.
Example: mocking a module dependency
Given a function that reads a file:
// get-post.ts
import { readFileSync } from "node:fs";
export function getPost(id: string): string {
return readFileSync(`posts/${id}.md`, "utf-8");
}
Mock node:fs with mock.module() before importing the module under test. Define the mock function separately so you have a reference for assertions.
// get-post.test.ts
import { describe, test, expect, mock, beforeEach } from "bun:test";
import { getPost } from "./get-post";
const mockReadFileSync = mock(() => "# Hello World");
mock.module("node:fs", () => ({
readFileSync: mockReadFileSync,
}));
describe("getPost", () => {
beforeEach(() => {
mockReadFileSync.mockClear();
});
test("reads the correct file path and returns content", () => {
expect(getPost("my-post")).toBe("# Hello World");
expect(mockReadFileSync).toHaveBeenCalledWith("posts/my-post.md", "utf-8");
});
});
Key points:
mock.module()overrides persist for the entire file and cannot be undone withmock.restore()- Call
mockClear()inbeforeEachto reset call counts between tests - For per-test return value variation, re-call
mockReadFileSync.mockImplementation(...)inbeforeEach
Spying on an existing method
Use spyOn when you want to observe calls on an object you already have, without replacing the whole module:
import { test, expect, spyOn, afterEach, mock } from "bun:test";
const spy = spyOn(console, "error");
afterEach(() => {
mock.restore(); // restores spied-on functions; does NOT reset mock.module() overrides
});
test("logs an error on invalid input", () => {
processInput(null);
expect(spy).toHaveBeenCalledTimes(1);
});
React component testing
For testing React components with React Testing Library and HappyDOM —
setup, userEvent interactions, and query priority — see
references/react-testing.md. Use the bootstrap
skill to install and configure both.
Before finishing
After writing or editing any test file, verify:
-
bun testpasses with no errors - No
.onlycalls left in the file - Mocks are restored in
afterEachwhere relevant - No
getByTestIdused when a role or label query would work