React › Testing

What is the difference between a unit test and a component test in a React app?

Beginner

Answer

A unit test checks a small, isolated piece of logic, like a single function that formats a price, completely on its own. A component test renders an actual React component and checks that it behaves correctly, often simulating user interactions like clicks and typing, giving you confidence that the component works as a whole.
// Unit test, testing a plain function
test('formats price correctly', () => {
  expect(formatPrice(9.5)).toBe('$9.50');
});

// Component test, testing a full rendered component
test('shows formatted price', () => {
  render(<PriceTag amount={9.5} />);
  expect(screen.getByText('$9.50')).toBeInTheDocument();
});
Real-world example A team writes a quick unit test for their price formatting function, and a separate component test to confirm the PriceTag component actually displays that formatted price correctly to the user on screen.

Common follow-ups: Why is it useful to have both kinds of tests instead of relying on just one type?;How much of an app's total test coverage should typically be unit tests versus component tests?