Why testing matters, what unit tests are, what to test versus what to skip, and how automated tests improve code quality, enable safe refactoring, and serve as living documentation.
Unit tests verify individual pieces of code in isolation, catching bugs early, enabling safe refactoring, and serving as living documentation for how code should behave.
Focus on business logic, edge cases, error handling, and state transitions. Avoid testing third-party internals, implementation details, and trivial code.
Every unit test follows three phases: set up preconditions (Arrange), execute the code (Act), and verify results (Assert). This pattern keeps tests readable and consistent.
Code that is hard to test often has design problems like tight coupling or hidden dependencies. Testability drives better software architecture.
Unit testing is the practice of writing automated checks that verify individual units of code -- typically functions, methods, or small modules -- work correctly in isolation. Understanding why testing matters and what to test is foundational knowledge that interviewers assess to gauge a candidate's engineering maturity.
A unit test exercises the smallest testable piece of code with known inputs and verifies the output matches expectations. The code under test is isolated from external dependencies (databases, APIs, file system) through mocking or stubbing. A well-written unit test is fast (milliseconds), deterministic (same result every run), and independent (does not rely on other tests).
Testing provides five core benefits that every developer should be able to articulate:
Focus testing efforts on code that provides the most value:
Every test follows the same three-phase pattern known as Arrange-Act-Assert (AAA):
Keeping this structure explicit makes tests readable and maintainable.
Good testers think about what could go wrong, not just the happy path. They ask: What if the input is empty? What if the network fails? What if this is called twice? This adversarial thinking is what makes unit testing a design activity, not just a verification step.
Fun Fact
The term 'unit test' was first used in the 1970s in the context of structured programming. Kent Beck popularized it for modern software development in 1998 when he created the SUnit framework for Smalltalk, which inspired JUnit, and eventually every xUnit framework including Jest.