Skip to content

Commit 6c249c1

Browse files
committed
Add orm examples
1 parent d70c02e commit 6c249c1

53 files changed

Lines changed: 15243 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ If you are new to Suites, check out the [Getting Started](https://suites.dev/doc
1111
| Example | DI Framework | Test Runner | Use When |
1212
| ---------------------------------------------- | ------------ | ----------- | ------------------------------------------------- |
1313
| [nestjs-jest](./nestjs-jest) | NestJS | Jest | NestJS with Jest |
14+
| [nestjs-jest-drizzle](./nestjs-jest-drizzle) | NestJS | Jest | NestJS with Jest + Drizzle ORM |
15+
| [nestjs-jest-prisma](./nestjs-jest-prisma) | NestJS | Jest | NestJS with Jest + Prisma ORM |
16+
| [nestjs-jest-typeorm](./nestjs-jest-typeorm) | NestJS | Jest | NestJS with Jest + TypeORM |
17+
| [nestjs-jest-mikroorm](./nestjs-jest-mikroorm) | NestJS | Jest | NestJS with Jest + MikroORM |
1418
| [nestjs-vitest](./nestjs-vitest) | NestJS | Vitest | NestJS with Vitest |
1519
| [nestjs-sinon](./nestjs-sinon) | NestJS | Sinon | NestJS with Sinon/Mocha |
1620
| [inversify-jest](./inversify-jest) | InversifyJS | Jest | InversifyJS with Jest |
@@ -97,6 +101,10 @@ This consistent domain model makes it easy to compare different framework and te
97101
```
98102
examples/
99103
├── nestjs-jest/ # NestJS with Jest
104+
├── nestjs-jest-drizzle/ # NestJS with Jest + Drizzle ORM
105+
├── nestjs-jest-prisma/ # NestJS with Jest + Prisma ORM
106+
├── nestjs-jest-typeorm/ # NestJS with Jest + TypeORM
107+
├── nestjs-jest-mikroorm/ # NestJS with Jest + MikroORM
100108
├── nestjs-vitest/ # NestJS with Vitest
101109
├── nestjs-sinon/ # NestJS with Sinon
102110
├── inversify-jest/ # InversifyJS with Jest

nestjs-jest-drizzle/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+

nestjs-jest-drizzle/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="@suites/doubles.jest/unit" />
2+

nestjs-jest-drizzle/jest.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testRegex: 'tests/.*\\.spec\\.ts$',
4+
transform: {
5+
'^.+\\.ts$': ['ts-jest', { isolatedModules: true }]
6+
},
7+
collectCoverageFrom: [
8+
'src/**/*.ts',
9+
'!src/types.ts',
10+
'!src/schema.ts'
11+
]
12+
};
13+

nestjs-jest-drizzle/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "nestjs-jest-drizzle-example",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "Suites NestJS + Jest + Drizzle Example",
6+
"scripts": {
7+
"test": "tsc --noEmit && jest"
8+
},
9+
"dependencies": {
10+
"@nestjs/common": "^10.4.15",
11+
"@nestjs/core": "^10.4.15",
12+
"drizzle-orm": "^0.29.0",
13+
"reflect-metadata": "^0.2.2"
14+
},
15+
"devDependencies": {
16+
"@suites/di.nestjs": "^3.0.1",
17+
"@suites/doubles.jest": "^3.0.1",
18+
"@suites/unit": "^3.0.1",
19+
"@types/jest": "^29.5.13",
20+
"jest": "^29.7.0",
21+
"ts-jest": "^29.2.5",
22+
"typescript": "^5.7.2"
23+
},
24+
"packageManager": "pnpm@9.15.4+sha512.b2dc20e2fc72b3e18848459b37359a32064663e5627a51e4c74b2c29dd8e8e0491483c3abb40789cfd578bf362fb6ba8261b05f0387d76792ed6e23ea3b1b6a0"
25+
}
26+

0 commit comments

Comments
 (0)