Unit testing isn't just about finding bugs—it's about design, speed, and reliability. Learn the Red-Green-Refactor cycle, understand the critical difference between Mocks and Stubs, and explore the modern ecosystem.
A unit test verifies the behavior of the smallest testable part of an application—usually a single function, method, or class—in complete isolation.
Unlike integration tests which check how pieces work together, unit tests are focused, fast, and deterministic. They act as a safety net for refactoring and documentation for how your code is supposed to work.
Test-Driven Development (TDD) relies on a simple, rhythmic cycle known as Red-Green-Refactor.
Click the lights to explore the cycle
The implementation of `calculateDiscount` has a bug. The test is failing. Can you fix the code?
function calculateDiscount(price, isMember) { // Bug: Members should get 20% off if (isMember) { return price; <-- ERROR HERE } return price; }
This is the most confusing topic in unit testing. The key distinction, as defined by Martin Fowler, is State Verification vs Behavior Verification.
Definition: A "dumb" object that provides canned answers to calls made during the test.
Zero-config, snapshot testing, great for React.
Vite-native, ultra-fast, modern Jest alternative.
The industry standard. Modular and powerful.
Essential for creating mocks in Java.
Simple syntax, powerful fixtures, huge plugin ecosystem.
Standard library, xUnit style, no install needed.
Community focused, extensible, modern standard.
Robust, rich feature set, widely used in legacy.