Skip to content

Stub

In one line

A stub is a simplified replacement that just returns canned responses — it doesn't verify how it was called.

In simple words

A stub is the simplest kind of test double. It replaces a real function but only does one thing: return a value you pre-configured. It doesn't record calls or verify anything.

Think of a stub like a recorded message. When you call it, it plays back the same response every time. It doesn't care how many times you called or what arguments you passed — it just returns what you told it to return.

Stubs are useful when you need to control what a dependency returns but don't need to verify how your code interacted with it. For example, stubbing a config function to always return 'production' mode.

Example

javascript
// Stub: always returns 42
const stub = jest.fn().mockReturnValue(42);
console.log(stub()); // 42
console.log(stub()); // 42
// No verification of call count or args

The stub always returns 42 regardless of how or how many times it's called.

Common confusions

  • Confused with: Mock

    The difference: A stub only provides return values; a mock also sets up expectations about how it should be called.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary