Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Suites + NestJS + Vitest

Simple user management example demonstrating Suites with NestJS and Vitest. Vitest provides faster test execution with native ESM support.

Prerequisites

  • Node.js 18 or higher
  • pnpm installed globally

What This Demonstrates

  • Solitary unit tests - Test UserService in complete isolation
  • Sociable unit tests - Test components together with real validation, mocked I/O
  • Token injection - DATABASE_TOKEN as external boundary
  • Class injection - UserValidator and UserRepository

Running the Example

pnpm install
pnpm test

All tests should pass, demonstrating both solitary and sociable testing strategies.

Project Structure

src/ - Application code being tested:

src/
├── types.ts              # User types and interfaces
├── user.validator.ts     # Validation logic (no dependencies)
├── user.repository.ts    # Data access (token injection)
└── user.service.ts       # Business logic (class injections)

tests/ - Tests demonstrating Suites usage:

tests/
├── user.solitary.spec.ts # Solitary unit tests (all dependencies mocked)
└── user.sociable.spec.ts # Sociable unit tests (real collaborators)

Key Patterns

Solitary Unit Tests

Tests one class in complete isolation. All dependencies are mocked.

const { unit, unitRef } = await TestBed.solitary(UserService).compile();
const repository: Mocked<UserRepository> = unitRef.get(UserRepository);
repository.exists.mockResolvedValue(false);

Sociable Unit Tests

Tests multiple classes together with real collaborators. External I/O remains mocked.

const { unit, unitRef } = await TestBed.sociable(UserService)
  .expose(UserValidator)
  .expose(UserRepository)
  .compile();
const database: Mocked<Database> = unitRef.get(DATABASE_TOKEN);

Comparing Testing Strategies

When to use Solitary:

  • Testing component logic in isolation
  • Controlling all inputs for predictable results
  • Dependencies are slow or complex to set up

When to use Sociable:

  • Verifying components work together correctly
  • Testing interactions between business logic components
  • Dependencies are fast

Related Examples

Learn More