Skill Tracer
Skills › Skills For Real Engineers › tdd › mocking.md

mocking.md

59 lines · skills/engineering/tdd/mocking.md at 4ae2317

Open on GitHub
1# When to Mock
2
3Mock at **system boundaries** only:
4
5- External APIs (payment, email, etc.)
6- Databases (sometimes - prefer test DB)
7- Time/randomness
8- File system (sometimes)
9
10Don't mock:
12- Your own classes/modules
13- Internal collaborators
14- Anything you control

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
Raw lines 1 to 15
16## Designing for Mockability
18At system boundaries, design interfaces that are easy to mock:
20**1. Use dependency injection**
22Pass external dependencies in rather than creating them internally:
24```typescript
25// Easy to mock
26function processPayment(order, paymentClient) {
27 return paymentClient.charge(order.total);
30// Hard to mock
31function processPayment(order) {
32 const client = new StripeClient(process.env.STRIPE_KEY);
33 return client.charge(order.total);
35```
37**2. Prefer SDK-style interfaces over generic fetchers**
39Create specific functions for each external operation instead of one generic function with conditional logic:
41```typescript
42// GOOD: Each function is independently mockable
43const api = {
44 getUser: (id) => fetch(`/users/${id}`),
45 getOrders: (userId) => fetch(`/users/${userId}/orders`),
46 createOrder: (data) => fetch('/orders', { method: 'POST', body: data }),
47};
49// BAD: Mocking requires conditional logic inside the mock
50const api = {
51 fetch: (endpoint, options) => fetch(endpoint, options),
52};
53```
55The SDK approach means:
56- Each mock returns one specific shape
57- No conditional logic in test setup
58- Easier to see which endpoints a test exercises
59- Type safety per endpoint

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
Raw lines 16 to 59