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

DocumentDescription
Meaningful TestsPhilosophy: test behavior, not implementation
Raw Mocks GuideRecommended - Simple mocking with bun:test
bun-mock-extended EvaluationWhy 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 TypeMock?Reason
Database/HTTP/File I/OYesSide effects, external
ConfigServiceNoPure data provider
Value objectsNoNo 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?