JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicstestingUnit Testing Fundamentals
PrevNext
testing
beginner
8 min read

Unit Testing Fundamentals

aaa-pattern
basics
best-practices
fundamentals
jest
testing-fundamentals
unit-testing
vitest

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.

Key Points

1Purpose of Unit Testing

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.

2What to Test

Focus on business logic, edge cases, error handling, and state transitions. Avoid testing third-party internals, implementation details, and trivial code.

3Arrange-Act-Assert

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.

4Design Feedback

Code that is hard to test often has design problems like tight coupling or hidden dependencies. Testability drives better software architecture.

What You'll Learn

  • Explain why unit testing matters and articulate its five core benefits
  • Decide what code is worth testing and what should be skipped
  • Structure tests using the Arrange-Act-Assert pattern
  • Understand how testability relates to good software design

Deep Dive

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.

What Unit Tests Are

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).

Why Testing Matters

Testing provides five core benefits that every developer should be able to articulate:

  1. Bug prevention: Tests catch regressions before code reaches production. A failing test after a change tells you exactly what broke and where.
  2. Safe refactoring: With a solid test suite, you can restructure code confidently. If all tests still pass, the behavior is preserved.
  3. Living documentation: Tests demonstrate how code is intended to be used. New team members can read test cases to understand expected behavior faster than reading implementation code.
  4. Design feedback: Code that is difficult to test often has design problems -- tight coupling, hidden dependencies, or functions that do too much. Writing tests encourages better architecture.
  5. Deployment confidence: Automated tests in CI/CD pipelines prevent broken code from being deployed, reducing incident frequency and on-call burden.

What to Test

Focus testing efforts on code that provides the most value:

  • Business logic and calculations: Tax computations, pricing rules, validation logic
  • Edge cases and boundary conditions: Empty arrays, null inputs, maximum values
  • Error handling paths: What happens when an API call fails or input is invalid
  • State transitions: Shopping cart updates, form validation state changes
  • Pure functions: Functions without side effects are the easiest and most valuable to test

What NOT to Test

  • Third-party library internals: Trust that React, Lodash, or Axios work correctly
  • Implementation details: Do not test that a specific private method was called; test the observable output
  • Trivial code: Simple getters, constants, or pass-through functions rarely need dedicated tests
  • Framework boilerplate: Configuration files, type definitions, or CSS

Anatomy of a Unit Test

Every test follows the same three-phase pattern known as Arrange-Act-Assert (AAA):

  • Arrange: Set up test data and preconditions
  • Act: Execute the code under test
  • Assert: Verify the result matches expectations

Keeping this structure explicit makes tests readable and maintainable.

The Testing Mindset

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.

Continue Learning

Test Organization and Structure

beginner

Matchers and Assertions

beginner

Testing Strategy for Large Applications

advanced

Practice What You Learned

What is unit testing and why is it important?
junior
basics
Unit testing involves testing individual units of code (functions, methods, components) in isolation to verify they work correctly. It's important for catching bugs early, enabling safe refactoring, documenting behavior, and building confidence in code quality.
Previous
Testing Asynchronous Code
Next
Code Coverage Metrics
PrevNext