liveBook · Manning

Test double

As explained in Acing the Frontend Interview

A test double is a substitute for a production dependency used to control or observe interactions during testing.

A test double substitutes for a production dependency so unit tests can control interactions, avoid external side effects, and verify return values or calls. section 5.4

Use test doubles to:

  • Replace slow, unpredictable, or externally connected dependencies. section 5.4
  • Verify side effects such as whether a logger received the expected message. section 5.4
  • Keep the test focused on the function or component under test. section 5.4

Mocking an external logger with Jest

A date-formatting utility calls logger.log after producing a date string. jest.mock('../utils/logger') replaces the real logger; the test checks both the formatted result and the expected logger call.

How Acing the Frontend Interview covers it

How deep it goes
Explains mocks, stubs, and spies through a Jest example, stopping short of detailed double taxonomy.
Examples
Uses a date formatter, external logger, Jest module mock, return-value assertion, and call assertion.
What it assumes
Assumes unit-testing fundamentals and familiarity with JavaScript modules, while demonstrating dependency isolation through a small function.
What you can do after
Apply Jest module mocking to isolate a function and assert both its result and dependency interactions.
How it connects
Connects test doubles with unit testing, mocks, stubs, spies, Jest, and external dependencies.

Also explained in

Compare how 9 books explain test double →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Frontend Interview. The section links point to the parts of the book it draws on.