Unit Testing
Unit testing verifies the smallest testable piece of code — typically a single function or method — in isolation from the rest of the system, usually written by the developer alongside the code itself. It's the fastest, cheapest test level, and forms the base of the test automation pyramid.
A unit test isolates its target completely, typically using mocking or stubbing to replace any dependency (a database call, an external API, another function) with a controlled fake — so the test verifies exactly one unit's logic, with no chance that a failure elsewhere in the system causes a false result.
Because they're fast (milliseconds, not seconds) and don't need a full environment to run, unit tests are meant to make up the large majority of an automated test suite, run on every single commit — catching the cheapest-to-fix bugs at the earliest, cheapest possible point.
Example
function add(a, b) { return a + b; }
test('add sums two numbers', () => {
expect(add(2, 3)).toBe(5);
});A minimal unit test — one function, one behavior, one assertion.