|
| 1 | +# Suites + NestJS + Jest (Advanced Mock Configuration) |
| 2 | + |
| 3 | +Simple user management example demonstrating [Suites](https://suites.dev) with NestJS and Jest, showcasing advanced mock configuration patterns using `.mock().final()` and `.mock().impl()`. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Node.js 18 or higher |
| 8 | +- pnpm installed globally |
| 9 | + |
| 10 | +## What This Demonstrates |
| 11 | + |
| 12 | +- ✅ **Solitary unit tests** - Test UserService in complete isolation |
| 13 | +- ✅ **Sociable unit tests** - Test components together with real validation, mocked I/O |
| 14 | +- ✅ **`.mock().final()`** - Immutable mock configuration with plain functions |
| 15 | +- ✅ **`.mock().impl()`** - Flexible mock configuration with stub functions |
| 16 | +- ✅ **Token injection** - DATABASE_TOKEN as external boundary |
| 17 | +- ✅ **Class injection** - UserValidator and UserRepository |
| 18 | + |
| 19 | +## Running the Example |
| 20 | + |
| 21 | +```bash |
| 22 | +pnpm install |
| 23 | +pnpm test |
| 24 | +``` |
| 25 | + |
| 26 | +All tests should pass, demonstrating both testing strategies with advanced mock configuration. |
| 27 | + |
| 28 | +## Project Structure |
| 29 | + |
| 30 | +``` |
| 31 | +src/ |
| 32 | +├── types.ts # User types and interfaces |
| 33 | +├── user.validator.ts # Validation logic (no dependencies) |
| 34 | +├── user.repository.ts # Data access (token injection) |
| 35 | +├── user.service.ts # Business logic (class injections) |
| 36 | +├── user.solitary.spec.ts # Solitary unit tests with mock config |
| 37 | +└── user.sociable.spec.ts # Sociable unit tests with mock config |
| 38 | +``` |
| 39 | + |
| 40 | +## Mock Configuration Patterns |
| 41 | + |
| 42 | +### `.mock().final()` - Immutable Configuration |
| 43 | + |
| 44 | +Use when you want to **lock down** mock behavior that should never change: |
| 45 | + |
| 46 | +```typescript |
| 47 | +const { unit, unitRef } = await TestBed.solitary(UserService) |
| 48 | + .mock(UserValidator) |
| 49 | + .final({ |
| 50 | + // Plain functions - cannot be reconfigured in tests |
| 51 | + validate: () => ({ isValid: true, errors: [] }) |
| 52 | + }) |
| 53 | + .compile(); |
| 54 | +``` |
| 55 | + |
| 56 | +**Key characteristics:** |
| 57 | + |
| 58 | +- Functions provided to `.final()` are plain functions, not Jest mocks |
| 59 | +- Behavior is locked - tests cannot use `mockReturnValue()` or similar |
| 60 | +- Call inspection (`toHaveBeenCalled()`) is not available |
| 61 | +- Best for: external services, logging, fixed configuration values |
| 62 | + |
| 63 | +### `.mock().impl()` - Flexible Configuration |
| 64 | + |
| 65 | +Use when you want **sensible defaults** that tests can override: |
| 66 | + |
| 67 | +```typescript |
| 68 | +const { unit, unitRef } = await TestBed.solitary(UserService) |
| 69 | + .mock(UserValidator) |
| 70 | + .impl((stubFn) => ({ |
| 71 | + // Stubs - can be reconfigured and inspected in tests |
| 72 | + validate: stubFn().mockReturnValue({ isValid: true, errors: [] }) |
| 73 | + })) |
| 74 | + .compile(); |
| 75 | + |
| 76 | +// Later in tests, you can override: |
| 77 | +validator.validate.mockReturnValue({ isValid: false, errors: ['Error'] }); |
| 78 | +``` |
| 79 | + |
| 80 | +**Key characteristics:** |
| 81 | + |
| 82 | +- Uses `stubFn()` factory to create Jest mock functions |
| 83 | +- Behavior is flexible - tests can reconfigure using `mockReturnValue()`, etc. |
| 84 | +- Call inspection (`toHaveBeenCalled()`, `toHaveBeenCalledWith()`) is available |
| 85 | +- Best for: most mocks where different tests need different behaviors |
| 86 | + |
| 87 | +## Comparing Testing Strategies |
| 88 | + |
| 89 | +**When to use `.final()`:** |
| 90 | + |
| 91 | +- External APIs (email, SMS, payments) - prevent accidental real calls |
| 92 | +- Configuration/settings - fixed test environment values |
| 93 | +- Logging/telemetry - consistent, predictable output |
| 94 | + |
| 95 | +**When to use `.impl()`:** |
| 96 | + |
| 97 | +- Database operations - need to simulate different query results |
| 98 | +- Collaborator services - need flexibility for different test scenarios |
| 99 | +- Any mock where behavior needs to vary per-test |
| 100 | + |
| 101 | +## Comparison Table |
| 102 | + |
| 103 | +| Feature | `.final()` | `.impl()` | |
| 104 | +| ---------------------- | ---------------- | ----------------- | |
| 105 | +| Reconfigurable | ❌ No | ✅ Yes | |
| 106 | +| Call inspection | ❌ No | ✅ Yes | |
| 107 | +| Function type | Plain functions | Jest stubs | |
| 108 | +| `mockReturnValue()` | ❌ Cannot use | ✅ Can use | |
| 109 | +| `toHaveBeenCalled()` | ❌ Cannot use | ✅ Can use | |
| 110 | + |
| 111 | +## Related Examples |
| 112 | + |
| 113 | +- [nestjs-jest](../nestjs-jest) - Basic NestJS + Jest example |
| 114 | +- [nestjs-vitest](../nestjs-vitest) - NestJS with Vitest |
| 115 | +- [inversify-jest](../inversify-jest) - InversifyJS with Jest |
| 116 | + |
| 117 | +## Learn More |
| 118 | + |
| 119 | +- [Suites Documentation](https://suites.dev) |
| 120 | +- [Mock Configuration API](https://suites.dev/docs/api-reference/mock-configuration) |
| 121 | +- [Test Doubles Guide](https://suites.dev/docs/guides/test-doubles) |
0 commit comments