Back to results for “Debugging”
JavaScript › Debugging, Testing & Tooling

What is a unit test and what makes a good one?

Beginner

Answer

A unit test verifies that a single, isolated piece of code (usually a function) behaves correctly for given inputs. A good unit test is fast, deterministic, tests one behavior at a time, and doesn't depend on external systems like a real database or network.
test('add() sums two numbers', () => {
  expect(add(2, 3)).toBe(5);
});
Real-world example Testing a formatCurrency() utility function in isolation before it's used across the app.

Common follow-ups: What's the difference between a unit test and an integration test?