Raw view: Writing Tests
No invisible, zero-width, or bidi-control characters detected.
---
name: writing-tests
description: >-
Write unit and integration tests for existing code in the project's own
ecosystem. Detects the test runner, identifies untested paths, and writes
tests with proper mocking, edge cases, and assertions. Use when the user asks
to add tests, improve coverage, or test a specific module.
user-invocable: true
disable-model-invocation: true
---
# Writing Tests
Use this skill when the user asks to add tests, improve coverage, or test a specific file or module. Match the project's ecosystem; do not assume JavaScript.
## Steps
1. **Detect the ecosystem and runner.** Check `AGENTS.md` first, then the manifests:
| Ecosystem | Manifest | Common runners | Run |
|-----------|----------|----------------|-----|
| JS / TS | `package.json` | Vitest, Jest, Playwright | `npm test` or the `test` script |
| Python | `pyproject.toml`, `requirements*.txt` | pytest, unittest | `pytest` |
| Rust / Anchor | `Cargo.toml`, `Anchor.toml` | cargo test, anchor test | `cargo test`, `anchor test` |
| Solidity | `foundry.toml`, `hardhat.config.*` | Foundry, Hardhat | `forge test`, `npx hardhat test` |
| Go | `go.mod` | go test | `go test ./...` |
Read two or three existing test files to learn the project's naming, layout, and assertion style before writing anything.
2. **If no runner exists,** ask the user which they prefer before installing anything, per the shared dependency rule. If they defer, recommend the ecosystem default (Vitest for TS, pytest for Python, Foundry for Solidity) and wait for approval.
3. **Analyze the target.** Identify the public API, the branches and error paths, the dependencies that need mocking (network, database, filesystem, chain RPC), and side effects.
4. **Place the file** where the project already puts tests: beside the source, in `__tests__/`, `tests/`, or `test/`. Follow the existing name pattern exactly.
5. **Cover, in this order:** happy path, edge cases (empty, null, zero, negative, boundary sizes), error cases, state transitions, async behavior. Name each test for the scenario: "returns 0 when cart is empty", not "test1".
6. **Mock external dependencies.** No real network, database, or chain calls in unit tests. Use the ecosystem's idiom: `vi.mock` or `jest.mock`, `monkeypatch` or `unittest.mock`, `vm.prank` and `vm.mockCall` in Foundry, fixtures with a local validator for Anchor.
7. **Run the tests** with the project's command and make them pass. If a test fails, decide whether the expectation or the implementation is wrong and say which. Never weaken a test to get green.
## Examples
Vitest:
```ts
import { describe, it, expect, vi } from "vitest";
vi.mock("../lib/db", () => ({ query: vi.fn().mockResolvedValue([{ id: 1 }]) }));
describe("formatPrice", () => {
it("formats cents as dollars", () => { expect(formatPrice(1234)).toBe("$12.34"); });
it("handles zero", () => { expect(formatPrice(0)).toBe("$0.00"); });
});
```
pytest:
```python
def test_parse_returns_empty_list_for_blank_input():
assert parse("") == []
def test_parse_raises_on_malformed(monkeypatch):
with pytest.raises(ValueError):
parse("not,valid")
```
Foundry:
```solidity
function test_RevertWhen_CallerNotOwner() public {
vm.prank(address(0xBEEF));
vm.expectRevert(Unauthorized.selector);
vault.withdraw(1 ether);
}
```
## What to test
- **Always:** public API, error handling, edge cases, state transitions, async behavior, access control on contracts
- **Skip:** private implementation details, third-party internals, trivial getters, type-only code
## Notes
- Test behavior and outputs, not internal calls.
- Several `expect` calls in one test are fine when they verify one behavior.
- For async code, always await or use `resolves` / `rejects`.