Software testing checks whether code behaves as expected. In a React application, unit, integration, end-to-end, and snapshot tests verify different aspects of quality and reliability. An interview answer should go beyond naming test types: explain what you would assert, what dependencies you would isolate, and how you would investigate a failure. section 5.5 section 5.4
Test a result and a side effect
Suppose formatDate converts a Date to YYYY-MM-DD and calls an external logger. A Jest test can mock the logger module, assert that the returned string is '2025-07-25', and separately assert that logger.log received 'Formatted date: 2025-07-25'. The mock avoids real logging while recording the call. This keeps the test focused, fast, and deterministic. 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');
});
});
Compare the two assertions in the mocked-logger test: one checks the return value; the other checks a side effect. section 5.4
A useful follow-up is: “How would you debug a failing unit test?” For a React Testing Library query failure, inspect the DOM printed by the test runner, call screen.debug() for the whole document or a selected element, or use screen.logTestingPlaygroundURL() to inspect the DOM and refine the query. If printed output is truncated, increase DEBUG_PRINT_LIMIT. section 5.4
Testing also belongs in the wider toolchain. Jest and React Testing Library appear alongside Cypress and Playwright; CI test runners can automate execution. More advanced workflows add visual regression, coverage tracking, and test orchestration. Choose the test and tool for the behavior you need to verify, then be ready to justify that choice. section 5.4 section 5.5


































































