Skip to content

How to write one

A good stub is deterministic, minimal and aligned with the real contract; anything else is a future false positive.

Writing a stub that ages well

  1. Fix the contract

    Copy the real signature. If the real method returns a Promise, the stub returns a Promise.

  2. Hardcode the answer

    No randomness, no time, no network. Same input, same output, always.

  3. Do not assert on the stub

    A stub is not a spy. If you need to check a call, you want a mock.

  4. Keep it close to the test

    A stub that lives in a shared folder drifts. Keep it next to the test that uses it.