Skip to content

Mock

Easy — everyone uses thisTesting

ELI5 — The Vibe Check

A mock is a fake version of something your code talks to. If your code calls a weather API, you don't want real weather in your tests — you use a mock that pretends to be the API and returns whatever you tell it to. Fake, but controllable.

Real Talk

A mock is a test double that replaces a real dependency with a programmable fake. You can control what the mock returns and verify how it was called. Mocks are used to isolate the code under test from external dependencies like APIs, databases, or services.

Show Me The Code

import { jest } from '@jest/globals';

const fetchWeather = jest.fn().mockResolvedValue({ temp: 22, unit: 'C' });

test('displays temperature', async () => {
  const result = await getWeatherDisplay(fetchWeather);
  expect(result).toBe('22°C');
  expect(fetchWeather).toHaveBeenCalledTimes(1);
});

When You'll Hear This

"Mock the payment API so tests don't actually charge cards." / "The mock returns a 500 so we can test the error handling path."

Made with passive-aggressive love by manoga.digital. Powered by Claude.