.agents/skills/tdd/mocking.md
Mock at system boundaries only:
Don't mock:
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: