Skip to content
Development
Agent

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.

From plugin
skills
954 skills54 agents

Claude Code

$ npx -y skills add vanara-agents/skills --agent claude-code

Agent definition

mocking-and-fixtures.md

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 restore

Never 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
@vanara-agents
Ships withskills

๐Ÿ’ 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.

917 skills54 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
9
Star history of vanara-agents/skills on GitHub, a chart by star-history.com

Other agents on skills.

AGENT

AGENT

Agent

Use when designing a new HTTP/GraphQL API or changing an existing one โ€” modeling resources, defining endpoint contracts, choosingโ€ฆ

review-notes

review-notes

Agent

This shows how the api-designer agent reviews a flawed draft. Findings are severity-ranked so the implementer fixes theโ€ฆ

contract-and-openapi

contract-and-openapi

Agent

The contract is the deliverable. Express it as an **OpenAPI 3.1** document so it is human-readable *and* machine-checkable. Thisโ€ฆ

design-checklist

design-checklist

Agent

Run through this before declaring an API contract done. It is ordered the way you should *design*: resources first, cross-cuttingโ€ฆ

versioning-and-evolution

versioning-and-evolutiโ€ฆ

Agent

APIs are forever once published: a consumer you've never met may depend on any field you expose. Design so you can **add withoutโ€ฆ

pr-comment-template

pr-comment-template

Agent

Copy-paste templates for leaving review comments. Keep each comment to one finding: an anchor, the problem, and the fix.

More agents from other plugins.

a11y-architect
Auto-invoked

a11y-architect

Agent

Accessibility Architect specializing in WCAG 2.2 compliance for Web and Native platforms. Use PROACTIVELY when designing UIโ€ฆ

ecc2w293k
@affaan-m@affaan-mInstall Agent
analyze-pass-impact

analyze-pass-impact

Agent

Analyzes how a specific topic affects a group of compiler passes. Used by the /plan-update skill to parallelize research acrossโ€ฆ

react5d252k
@facebook@facebookInstall Agent
orchestrator

orchestrator

Agent

- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such asโ€ฆ

codex5d135k
@openai@openaiInstall Agent
cavecrew-builder

cavecrew-builder

Agent

Surgical 1-2 file edit. Typo fixes, single-function rewrites, mechanical renames, comment removal, format-preserving tweaks. Hardโ€ฆ

caveman6d119k
@juliusbrussee@juliusbrusseeInstall Agent

Questions about mocking-and-fixtures.

Which coding agents can use mocking-and-fixtures?
mocking-and-fixtures installs in Claude Code and Cursor: each loads subagents from .claude/agents, so copy the file there. Flowy's auto-invocation runs in Claude Code only.