Stub
ELI5 — The Vibe Check
A stub is like a cardboard cutout of a function. It stands in for the real thing and always gives you the same canned response. Unlike a mock, you don't check HOW it was called — it just quietly returns whatever you set up.
Real Talk
A stub is a test double that provides predefined responses to method calls. Unlike mocks, stubs don't verify how they were called — they only provide controlled return values. Stubs are used to replace unpredictable dependencies like APIs or clocks.
Show Me The Code
// Stub always returns the same user
const userStub = {
findById: (id) => ({ id, name: 'Test User', email: 'test@example.com' })
};
const result = renderProfile(userStub, 42);
expect(result).toContain('Test User');
When You'll Hear This
"Stub out the database call so we can test the formatting logic." / "Use a stub when you just need a controlled return value."
Related Terms
Fixture
A fixture is like pre-built test furniture.
Mock
A mock is a fake version of something your code talks to.
Spy
A spy is like a double agent — it lets the real function still do its job, but secretly records everything: how many times it was called, with what argumen...
Test Double
Test double is the umbrella term for anything that replaces a real dependency in a test. Mocks, stubs, spies, fakes — they're all test doubles.