liveBook · Manning

Test isolation

As explained in Acing the Frontend Interview

Unit test isolation is the practice of testing a unit independently from its external dependencies and interactions.

Test isolation keeps a unit test focused on the code under test rather than the behavior of external dependencies. In an interview, a useful question is: how would you test a function that formats a date and calls a logger without performing real logging? Replace the logger with a mock, then check both the returned value and the logging call. section 5.4

Listing 5.15 Unit test with a mocked logger
import { formatDate } from '../utils/formatDate';
import { logger } from '../utils/logger';
jest.mock('../utils/logger'); #A
describe('formatDate', () => {
  it('formats the date to YYYY-MM-DD', () => { #B
    const result = formatDate(new Date('2025-07-25'));
    expect(result).toBe('2025-07-25');
  });
  it('logs the formatted date', () => { #C
    formatDate(new Date('2025-07-25'));
    expect(logger.log).toHaveBeenCalledWith('Formatted date: 2025-07-25');
  });
});

The Jest test mocks the logger module, asserts that the date formatter returns `2025-07-25`, and separately checks the message passed to `logger.log`. The mock records the call without producing the real side effect. section 5.4

The key distinction to articulate is that a mock lets the test verify an interaction without depending on the real service or API. That keeps the test fast and deterministic while leaving the formatter’s logic under scrutiny. section 5.4

How Acing the Frontend Interview covers it

How deep it goes
Works through a mocked module and two Jest assertions; does not elaborate a broader isolation procedure.
Examples
Uses a date-formatting utility and an external logger to demonstrate return-value and logging-call assertions.
What it assumes
Assumes familiarity with TypeScript functions and imports; introduces dependency mocking through a concrete Jest test.
What you can do after
Apply it via Jest to mock an imported logger and assert both formatted output and the expected call.
How it connects
Connects unit testing with mocks, stubs, spies, dependencies, and side effects.

Also explained in

Compare how 6 books explain test isolation →

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.