liveBook · Manning

Mocking

As explained in Acing the Frontend Interview

Mocking replaces a dependency with a programmable substitute that records or controls interactions during a test.

Mocking replaces a dependency with a programmable substitute so a unit test can isolate the logic under test and verify interactions. section 5.4

A strong interview explanation should cover:

  • Replace external dependencies such as loggers, services, or APIs instead of executing their real side effects. section 5.4
  • Use mocks, stubs, or spies to control dependency behavior and inspect calls. section 5.4
  • Verify both returned values and side effects while keeping tests fast, deterministic, and isolated. section 5.4

Mocking a logger with Jest

A date-formatting utility returns YYYY-MM-DD and calls logger.log. Mocking the logger prevents real logging; one test checks the formatted value, while another asserts the logged message.

How Acing the Frontend Interview covers it

How deep it goes
Explains programmable substitutes, interaction tracking, and assertions, but does not develop broader test-debugging procedures.
Examples
Uses a date formatter, an external logger, Jest module mocking, and assertions for output and logging.
What it assumes
Assumes unit-testing context and distinguishes mocks, stubs, spies, and real dependencies through the example.
What you can do after
Apply it via Jest to isolate a function, control dependencies, and verify return values and side effects.
How it connects
Connects unit testing with test doubles, stubs, spies, side effects, external services, and APIs.

Also explained in

Compare how 26 books explain mocking →

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.