mocking-and-fixtures
Tests must be **isolated** (independent of each other) and **deterministic** (same result every run). That means controlling everything that crosses a boundary or varies between runs: time, randomness, network, filesystem, and shared collaborators.
$ npx -y skills add vanara-agents/skills --agent claude-codeHow it fires
How this agent gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
Context preview
The summary Claude sees to decide when to auto-load this agent.
Tests must be **isolated** (independent of each other) and **deterministic** (same result every run). That means controlling everything that crosses a boundary or varies between runs: time, randomness, network, filesystem, and shared collaborators.
Agent definition
mocking-and-fixtures.mdMocking, Fixtures, and Determinism
Tests must be **isolated** (independent of each other) and **deterministic** (same result every run). That means controlling everything that crosses a boundary or varies between runs: time, randomness, network, filesystem, and shared collaborators.
Test doubles — know which one you need
| Double | Purpose | |---|---| | **Dummy** | Filler passed to satisfy a signature; never used. | | **Stub** | Returns canned answers to calls made during the test. | | **Spy** | A stub that also records how it was called. | | **Mock** | A double with pre-set expectations that verifies interactions. | | **Fake** | A working but lightweight implementation (in-memory DB, fake clock). |
Reach for the **simplest** double that does the job. Prefer **fakes and stubs** over strict mocks: asserting on exact call sequences couples the test to implementation and makes refactoring break green tests.
Mock at the boundary, only the boundary
// GOOD: mock the HTTP client (a true external boundary)
const httpGet = vi.fn().mockResolvedValue({ status: 200, body: { rate: 1.1 } });
const rate = await fetchExchangeRate(httpGet, 'USD', 'EUR');
expect(rate).toBe(1.1);// BAD: mocking the function under test leaves nothing real to verify
const fetchExchangeRate = vi.fn().mockResolvedValue(1.1); // tests the mock, not the code
Over-mocking is a top cause of tests that pass while the system is broken. If a test is almost entirely mock setup, you are testing the mock framework. Either widen the unit or move the check to an integration test with a real collaborator.
Controlling time
Wall-clock time is the most common flakiness source. Inject a clock or freeze it:
import { vi } from 'vitest';
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-06-29T12:00:00Z'));
expect(isExpired(token)).toBe(false);
vi.advanceTimersByTime(60_000); // jump 60s deterministically
expect(isExpired(token)).toBe(true);
vi.useRealTimers(); // always restoreNever assert on `Date.now()` or sleep with real timeouts — that makes tests slow and racy.
Controlling randomness
Seed the RNG or inject it. A test that depends on `Math.random()` passes-then-fails at random:
const rng = () => 0.42; // injected, deterministic
expect(pickWinner(players, rng)).toBe('alice');Fixtures and factories
- **Factories over literals.** A `makeOrder(overrides)` helper keeps tests readable and
resilient — when a required field is added, you change the factory once, not 50 tests.
- **Build only what the test needs.** Override just the fields relevant to the behavior;
let the factory default the rest. This makes the *intent* of each test obvious.
- **Isolate state.** Reset shared resources (DB rows, in-memory stores, module mocks) in
`beforeEach`/`afterEach` so tests can run in any order. Order-dependent tests are a bug.
function makeOrder(overrides = {}) {
return { id: 'ord_1', status: 'open', total: 100, createdAt: new Date(0), ...overrides };
}
it('marks a paid order as fulfilled', () => {
const order = makeOrder({ status: 'paid' }); // only the relevant field is set
expect(fulfill(order).status).toBe('fulfilled');
});Determinism checklist
- [ ] No real network, filesystem, or DB in unit tests (use fakes/stubs).
- [ ] Time and randomness are frozen or injected.
- [ ] No shared mutable state leaks between tests; setup/teardown resets it.
- [ ] All async work is awaited — no dangling promises or unhandled timers.
- [ ] The suite passes when run in a randomized order.
Read more
Mocking, Fixtures, and Determinism
Tests must be **isolated** (independent of each other) and **deterministic** (same result every run). That means controlling everything that crosses a boundary or varies between runs: time, randomness, network, filesystem, and shared collaborators.
Test doubles — know which one you need
| Double | Purpose | |---|---| | **Dummy** | Filler passed to satisfy a signature; never used. | | **Stub** | Returns canned answers to calls made during the test. | | **Spy** | A stub that also records how it was called. | | **Mock** | A double with pre-set expectations that verifies interactions. | | **Fake** | A working but lightweight implementation (in-memory DB, fake clock). |
Reach for the **simplest** double that does the job. Prefer **fakes and stubs** over strict mocks: asserting on exact call sequences couples the test to implementation and makes refactoring break green tests.
Mock at the boundary, only the boundary
// GOOD: mock the HTTP client (a true external boundary)
const httpGet = vi.fn().mockResolvedValue({ status: 200, body: { rate: 1.1 } });
const rate = await fetchExchangeRate(httpGet, 'USD', 'EUR');
expect(rate).toBe(1.1);// BAD: mocking the function under test leaves nothing real to verify const fetchExchangeRate = vi.fn().mockResolvedValue(1.1); // tests the mock, not the code
Over-mocking is a top cause of tests that pass while the system is broken. If a test is almost entirely mock setup, you are testing the mock framework. Either widen the unit or move the check to an integration test with a real collaborator.
Controlling time
Wall-clock time is the most common flakiness source. Inject a clock or freeze it:
import { vi } from 'vitest';
vi.useFakeTimers();
vi.setSystemTime(new Date('2026-06-29T12:00:00Z'));
expect(isExpired(token)).toBe(false);
vi.advanceTimersByTime(60_000); // jump 60s deterministically
expect(isExpired(token)).toBe(true);
vi.useRealTimers(); // always restoreNever assert on `Date.now()` or sleep with real timeouts — that makes tests slow and racy.
Controlling randomness
Seed the RNG or inject it. A test that depends on `Math.random()` passes-then-fails at random:
const rng = () => 0.42; // injected, deterministic
expect(pickWinner(players, rng)).toBe('alice');Fixtures and factories
- **Factories over literals.** A `makeOrder(overrides)` helper keeps tests readable and
resilient — when a required field is added, you change the factory once, not 50 tests.
- **Build only what the test needs.** Override just the fields relevant to the behavior;
let the factory default the rest. This makes the *intent* of each test obvious.
- **Isolate state.** Reset shared resources (DB rows, in-memory stores, module mocks) in
`beforeEach`/`afterEach` so tests can run in any order. Order-dependent tests are a bug.
function makeOrder(overrides = {}) {
return { id: 'ord_1', status: 'open', total: 100, createdAt: new Date(0), ...overrides };
}
it('marks a paid order as fulfilled', () => {
const order = makeOrder({ status: 'paid' }); // only the relevant field is set
expect(fulfill(order).status).toBe('fulfilled');
});Determinism checklist
- [ ] No real network, filesystem, or DB in unit tests (use fakes/stubs).
- [ ] Time and randomness are frozen or injected.
- [ ] No shared mutable state leaks between tests; setup/teardown resets it.
- [ ] All async work is awaited — no dangling promises or unhandled timers.
- [ ] The suite passes when run in a randomized order.
🐒 Free agents, skills & packs for Claude Code One subscription. An army of Claude Code agents. 30 production-grade agents, skills, and packs for Claude Code — free, Apache-2.0, install with one command.
Repo: vanara-agents/skills
Other agents on vanara-agents-skills.
- AGENT
Use when designing a new HTTP/GraphQL API or changing an existing one — modeling resources, defining endpoint contracts, choosing status codes, pagination, filtering, error envelopes, versioning, and idempotency. Produces a reviewable API contract plus an OpenAPI snippet, not
Open agent - review-notes
This shows how the api-designer agent reviews a flawed draft. Findings are severity-ranked so the implementer fixes the contract-breakers first. Severity legend: **CRITICAL** (breaks clients / data risk), **HIGH** (real bug or inconsistency), **MEDIUM** (maintainability),
Open agent - contract-and-openapi
The contract is the deliverable. Express it as an **OpenAPI 3.1** document so it is human-readable *and* machine-checkable. This reference covers how to structure that document and what `scripts/lint-openapi.mjs` enforces.
Open agent - design-checklist
Run through this before declaring an API contract done. It is ordered the way you should *design*: resources first, cross-cutting rules last. Every box is a place real APIs go wrong in production.
Open agent - versioning-and-evolution
APIs are forever once published: a consumer you've never met may depend on any field you expose. Design so you can **add without breaking**, and version explicitly when you must break.
Open agent - pr-comment-template
Copy-paste templates for leaving review comments. Keep each comment to one finding: an anchor, the problem, and the fix.
Open agent

