Simple user management example demonstrating Suites with NestJS and Vitest. Vitest provides faster test execution with native ESM support.
- Node.js 18 or higher
- pnpm installed globally
- ✅ 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
pnpm install
pnpm testAll tests should pass, demonstrating both solitary and sociable testing strategies.
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)
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);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);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
- nestjs-jest - Same framework with Jest
- nestjs-sinon - Same framework with Sinon/Mocha
- inversify-vitest - InversifyJS with Vitest