mattpocock-skills/skills/engineering/tdd/mocking.md
Matt Pocock 62f43a1817 Add new skills for TDD, issue management, PRD creation, and productivity tools
- Introduced TDD skills including deep modules, interface design, mocking, refactoring, and testing guidelines.
- Added skills for breaking plans into GitHub issues and creating PRDs from conversation context.
- Implemented productivity skills for scaffolding exercises, setting up pre-commit hooks, and managing notes in Obsidian.
- Created a caveman communication mode for concise technical responses and a grilling technique for thorough plan discussions.
- Developed a skill for writing new agent skills with structured templates and guidelines.
- Included git guardrails to prevent dangerous git commands and a migration guide for using @total-typescript/shoehorn in tests.
2026-04-28 09:42:34 +01:00

1.4 KiB

When to Mock

Mock at system boundaries only:

  • External APIs (payment, email, etc.)
  • Databases (sometimes - prefer test DB)
  • Time/randomness
  • File system (sometimes)

Don't mock:

  • Your own classes/modules
  • Internal collaborators
  • Anything you control

Designing for Mockability

At system boundaries, design interfaces that are easy to mock:

1. Use dependency injection

Pass external dependencies in rather than creating them internally:

// Easy to mock
function processPayment(order, paymentClient) {
  return paymentClient.charge(order.total);
}

// Hard to mock
function processPayment(order) {
  const client = new StripeClient(process.env.STRIPE_KEY);
  return client.charge(order.total);
}

2. Prefer SDK-style interfaces over generic fetchers

Create specific functions for each external operation instead of one generic function with conditional logic:

// GOOD: Each function is independently mockable
const api = {
  getUser: (id) => fetch(`/users/${id}`),
  getOrders: (userId) => fetch(`/users/${userId}/orders`),
  createOrder: (data) => fetch('/orders', { method: 'POST', body: data }),
};

// BAD: Mocking requires conditional logic inside the mock
const api = {
  fetch: (endpoint, options) => fetch(endpoint, options),
};

The SDK approach means:

  • Each mock returns one specific shape
  • No conditional logic in test setup
  • Easier to see which endpoints a test exercises
  • Type safety per endpoint