|
| 1 | +# Suites + NestJS + Jest + Drizzle |
| 2 | + |
| 3 | +Simple user management example demonstrating [Suites](https://suites.dev) with NestJS, Jest, and Drizzle ORM. |
| 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 | +- ✅ **Token injection** - DATABASE_TOKEN as external boundary |
| 15 | +- ✅ **Class injection** - UserValidator and UserRepository |
| 16 | +- ✅ **Drizzle ORM integration** - Type-safe schema definitions with Drizzle |
| 17 | + |
| 18 | +## Running the Example |
| 19 | + |
| 20 | +```bash |
| 21 | +pnpm install |
| 22 | +pnpm test |
| 23 | +``` |
| 24 | + |
| 25 | +All tests should pass, demonstrating both solitary and sociable testing strategies. |
| 26 | + |
| 27 | +## Project Structure |
| 28 | + |
| 29 | +**`src/`** - Application code being tested: |
| 30 | + |
| 31 | +``` |
| 32 | +src/ |
| 33 | +├── types.ts # User types and interfaces |
| 34 | +├── schema.ts # Drizzle schema definitions |
| 35 | +├── user.validator.ts # Validation logic (no dependencies) |
| 36 | +├── user.repository.ts # Data access (token injection) |
| 37 | +└── user.service.ts # Business logic (class injections) |
| 38 | +``` |
| 39 | + |
| 40 | +**`tests/`** - Tests demonstrating Suites usage: |
| 41 | + |
| 42 | +``` |
| 43 | +tests/ |
| 44 | +├── user.solitary.spec.ts # Solitary unit tests (all dependencies mocked) |
| 45 | +└── user.sociable.spec.ts # Sociable unit tests (real collaborators) |
| 46 | +``` |
| 47 | + |
| 48 | +## Key Patterns |
| 49 | + |
| 50 | +### Solitary Unit Tests |
| 51 | + |
| 52 | +Tests one class in complete isolation. All dependencies are mocked. |
| 53 | + |
| 54 | +```typescript |
| 55 | +const { unit, unitRef } = await TestBed.solitary(UserService).compile(); |
| 56 | +const repository: Mocked<UserRepository> = unitRef.get(UserRepository); |
| 57 | +repository.exists.mockResolvedValue(false); |
| 58 | +``` |
| 59 | + |
| 60 | +### Sociable Unit Tests |
| 61 | + |
| 62 | +Tests multiple classes together with real collaborators. External I/O remains mocked. |
| 63 | + |
| 64 | +```typescript |
| 65 | +const { unit, unitRef } = await TestBed.sociable(UserService) |
| 66 | + .expose(UserValidator) |
| 67 | + .expose(UserRepository) |
| 68 | + .compile(); |
| 69 | +const database: Mocked<Database> = unitRef.get(DATABASE_TOKEN); |
| 70 | +``` |
| 71 | + |
| 72 | +## Drizzle ORM Integration |
| 73 | + |
| 74 | +This example uses Drizzle ORM for type-safe database schema definitions: |
| 75 | + |
| 76 | +```typescript |
| 77 | +import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core'; |
| 78 | + |
| 79 | +export const users = sqliteTable('users', { |
| 80 | + id: integer('id').primaryKey(), |
| 81 | + email: text('email').notNull().unique(), |
| 82 | + name: text('name').notNull(), |
| 83 | + isActive: integer('is_active', { mode: 'boolean' }).notNull().default(true) |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +Types are inferred from the schema: |
| 88 | + |
| 89 | +```typescript |
| 90 | +import { InferSelectModel, InferInsertModel } from 'drizzle-orm'; |
| 91 | +export type User = InferSelectModel<typeof users>; |
| 92 | +export type CreateUserDto = InferInsertModel<typeof users>; |
| 93 | +``` |
| 94 | + |
| 95 | +## Comparing Testing Strategies |
| 96 | + |
| 97 | +**When to use Solitary:** |
| 98 | +- Testing component logic in isolation |
| 99 | +- Controlling all inputs for predictable results |
| 100 | +- Dependencies are slow or complex to set up |
| 101 | + |
| 102 | +**When to use Sociable:** |
| 103 | +- Verifying components work together correctly |
| 104 | +- Testing interactions between business logic components |
| 105 | +- Dependencies are fast |
| 106 | + |
| 107 | +## Related Examples |
| 108 | + |
| 109 | +- [nestjs-jest](../nestjs-jest) - NestJS with Jest (no ORM) |
| 110 | +- [nestjs-vitest](../nestjs-vitest) - Same framework with Vitest (faster execution) |
| 111 | +- [nestjs-sinon](../nestjs-sinon) - Same framework with Sinon/Mocha |
| 112 | + |
| 113 | +## Learn More |
| 114 | + |
| 115 | +- [Suites Documentation](https://suites.dev) |
| 116 | +- [NestJS Integration](https://suites.dev/docs/nestjs) |
| 117 | +- [Testing Strategies](https://suites.dev/docs/testing-strategies) |
| 118 | +- [Drizzle ORM Documentation](https://orm.drizzle.team) |
| 119 | + |
0 commit comments