Skip to main content

Unit Testing

Unit testing plays an important role in functional testing by validating individual components and helping maintain code quality and reliability.

Why Unit Testing is Important

  • Early defect detection: Catch bugs at the earliest stage, reducing cost and effort later.
  • Supports code quality: Encourages clean, modular, and maintainable code.
  • Prevents regressions: Ensures existing features are not broken by changes.
  • Speeds up development: Gives developers confidence when modifying code.

For JavaScript and TypeScript projects, popular unit testing tools include Jest and Vitest.

Jest is widely used, especially in React projects, offering zero-configuration setup, snapshot testing, built-in mocking, and fast execution. Vitest is a modern, high-speed alternative compatible with Jest’s API, with excellent TypeScript support and seamless integration with modern frontend frameworks.

Ownership & Responsibilities

  • Created by Developers:
    Developers are responsible for writing unit tests when implementing new features or fixing bugs. Tests should cover both happy paths and edge cases.

  • Reviewed by QA:
    QA reviews unit tests to ensure:

    • All requirements and acceptance criteria are covered
    • Edge cases, negative scenarios, and error handling are included
    • Tests are meaningful, deterministic, and maintainable
    • Opportunities to optimize integration or system tests are identified

See the Unit Test Review Checklist for detailed QA guidance.

Where Unit Tests Are Run

Unit tests are typically executed in multiple environments to ensure code quality before it reaches production.

1. Local Environment

Developers run unit tests locally on their machines during development. This helps catch issues early, verify code changes, and ensure that the logic behaves as expected before committing code.

2. Pull Request (PR) Environment

When a developer opens a pull request, unit tests are automatically triggered in a PR environment. This ensures that new changes do not break existing functionality and that all test cases pass before merging into the main branch.

3. CI/CD Pipeline

Unit tests are also run as part of the CI/CD workflow on a dedicated build server. Running tests in CI/CD provides:

  • Consistent test execution across all environments
  • Automated verification of new code before deployment
  • Early detection of regressions or integration issues

By running unit tests in local, PR, and CI/CD environments, teams ensure high confidence in the quality and stability of the code throughout the development lifecycle.

Summary

Unit testing is crucial for maintaining code quality and preventing regressions. Developers create tests, while QA ensures completeness, correctness, and coverage from a customer perspective. Effective unit tests and QA reviews strengthen the foundation for all higher-level testing and help deliver reliable, maintainable software.

References