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





