Skip to main content

๐Ÿงช Testing Overview

An introduction to testing strategies and tools in NestFlux applications.

๐ŸŽฏ Testing Philosophyโ€‹

NestFlux embraces comprehensive testing to ensure code quality and reliability:

  • Test-Driven Development: Writing tests alongside or before implementation
  • Fast Feedback: Quick test execution for immediate developer feedback
  • Comprehensive Coverage: Unit, integration, and end-to-end testing
  • Maintainable Tests: Clear, readable tests that serve as living documentation

๐Ÿ› ๏ธ Testing Stackโ€‹

Vitest - Core Testing Frameworkโ€‹

NestFlux uses Vitest as the primary testing framework across the entire monorepo:

  • Lightning Fast: Native ESM support with instant hot module replacement
  • TypeScript Native: First-class TypeScript support without additional setup
  • Jest Compatible: Familiar API for developers coming from Jest
  • Vite Integration: Leverages Vite's transformation and bundling capabilities

Vitest Workspaceโ€‹

The monorepo uses Vitest Workspace for coordinated testing:

  • Unified Configuration: Shared testing setup across all packages and applications
  • Parallel Execution: Run tests across multiple projects simultaneously
  • Selective Testing: Target specific packages or applications
  • Consistent Tooling: Same testing experience everywhere in the monorepo

๐Ÿ—๏ธ Testing Architectureโ€‹

Workspace Structureโ€‹

The testing setup is organized across the monorepo:

vitest.config.ts             # Shared test utilities and setup
โ”œโ”€โ”€ tests/ # Global tests
โ”œโ”€โ”€ apps/
โ”‚ โ”œโ”€โ”€ client/
โ”‚ โ”‚ โ””โ”€โ”€ vitest.config.ts # Client-specific test config
โ”‚ โ””โ”€โ”€ server/
โ”‚ โ””โ”€โ”€ vitest.config.ts # Server-specific test config
โ””โ”€โ”€ packages/
โ”œโ”€โ”€ api-definition/
โ”œโ”€โ”€ constants/
โ”œโ”€โ”€ models/
โ”œโ”€โ”€ types/
โ””โ”€โ”€ utils/
โ””โ”€โ”€ vitest.config.ts # Package-specific configs

Testing Typesโ€‹

Unit Testingโ€‹

  • Pure Functions: Business logic and utility functions
  • Components: React components in isolation
  • Services: NestJS services and controllers
  • Utilities: Shared package functions

Component Testingโ€‹

  • React Components: User interface components with user interactions
  • Integration: Component interactions with services and state
  • Accessibility: Ensuring components meet accessibility standards

Integration Testingโ€‹

  • API Endpoints: Testing server routes and middleware
  • Database Operations: Testing data layer interactions
  • Service Integration: Testing service-to-service communication

๐Ÿš€ Running Testsโ€‹

All Testsโ€‹

Run tests across the entire monorepo:

pnpm test

Watch Modeโ€‹

Run tests in watch mode for development:

pnpm test:dev

UI Modeโ€‹

Run tests with Vitest's interactive UI:

pnpm test:ui

๐Ÿ”ง Test Configurationโ€‹

Shared Configurationโ€‹

Common test setup is defined in vitest.shared.ts:

  • Global test utilities
  • Mock configurations
  • Common matchers and helpers
  • Environment setup

Project-Specific Configurationโ€‹

Each application and package can have its own vitest.config.ts:

  • Custom test environment settings
  • Project-specific mocks
  • Specialized testing utilities
  • Coverage configuration

๐Ÿ“Š Test Coverageโ€‹

Vitest provides built-in coverage reporting:

  • Line Coverage: Percentage of code lines executed
  • Branch Coverage: Percentage of code branches tested
  • Function Coverage: Percentage of functions called
  • Statement Coverage: Percentage of statements executed

Coverage reports help identify untested code and maintain quality standards.

๐Ÿงฉ Testing Ecosystemโ€‹

CI/CD Integrationโ€‹

Tests are automatically run in the CI/CD pipeline:

  • Pull Request Validation: All tests must pass before merging
  • Coverage Reporting: Track coverage trends over time
  • Parallel Execution: Fast test feedback in automated workflows

Good tests are an investment in code quality, developer confidence, and long-term maintainability.