README
Testing patterns and best practices for NestJS applications using Bun’s test runner.
Quick Start
Use raw bun:test mocks. Don’t use bun-mock-extended.
import { beforeEach, describe, expect, it, mock } from "bun:test";
let mockService: any;
beforeEach(() => { mockService = { getData: mock(() => Promise.resolve(["result"])), save: mock(() => Promise.resolve()), };});See Raw Mocks Guide for complete patterns.
Documents
| Document | Description |
|---|---|
| Meaningful Tests | Philosophy: test behavior, not implementation |
| Raw Mocks Guide | Recommended - Simple mocking with bun:test |
| bun-mock-extended Evaluation | Why we don’t recommend this library |
Core Principles
1. Test Behavior, Not Implementation
Verify what services do, not how they do it internally.
2. Use Raw Mocks
External mocking libraries add complexity without meaningful benefit:
- Tests catch errors at runtime anyway
- Type safety in mocks is ceremony when you run tests frequently
- Library bugs (calledWith, mockDeep) create friction
3. Mock at Side Effect Boundaries
| Dependency Type | Mock? | Reason |
|---|---|---|
| Database/HTTP/File I/O | Yes | Side effects, external |
| ConfigService | No | Pure data provider |
| Value objects | No | No side effects |
4. Fewer Meaningful Tests > Many Meaningless Tests
Before writing a test, ask:
- Does this verify observable behavior?
- Would this fail if the service broke for a real user?