Unit test

Unit test

Unit testing is a testing method that individually verifies the smallest units of a program, such as functions and methods. By replacing external dependencies with mocks, it allows for rapid validation of the target logic in isolation.

What Constitutes a "Unit"

The "unit" in unit testing varies in granularity depending on the language or framework. It may refer to a single function, or it may refer to an entire class or module. What they have in common is the principle of "testing with external I/O (DB, network, file system) excluded."

External dependencies are replaced with mocks or stubs — for example, returning Supabase query results via a mock object, or replacing API calls with stubs. This keeps execution time within milliseconds, allowing hundreds to thousands of tests to be run on every commit in a CI pipeline.

The Limitations of Mocks

Because mocks can return objects with arbitrary properties, they cannot detect inconsistencies with the DB schema. The incident where "all tests are GREEN but production throws a column does not exist error" is not uncommon. Unit tests are strong at validating business logic, but consistency at system boundaries needs to be supplemented with functional tests or E2E tests.

Relationship with TDD

TDD (Test-Driven Development) is a development methodology that leverages unit tests as a "design tool." By writing tests first, the input/output specifications of a function are finalized before implementation. Since the existence of unit tests is a prerequisite, the two are closely intertwined.