How it works
A unit test might call a calculateTotal function with a cart of two items and a discount code, then assert that the result is exactly what it should be. Because it touches no network, database or browser, hundreds of them run in a second or two, so developers run them constantly while working and CI runs them on every change.
Anything that would make a test slow or unpredictable, such as an API call or the current time, is swapped for a mock or a fake. Unit tests form the base of the testing pyramid: many cheap tests at the bottom, fewer integration tests above them and a handful of end-to-end tests at the top. They are good at pinning down logic and edge cases, and weak at proving the whole app works.
Unit tests vs the alternatives
Related terms
More in Testing and quality
Kinds of tests