@nathapp/nestjs-prisma 3.3.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -7
- package/.eslintrc.js +0 -8
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -279
- package/docs/20260215-fix-plan-prisma.md +0 -37
- package/docs/20260215-review-prisma.md +0 -598
- package/docs/20260216-plan-prisma-a-minus.md +0 -70
- package/lib/errors/prisma-error.mapper.ts +0 -164
- package/lib/filter/prisma-exception.filter.ts +0 -67
- package/lib/index.ts +0 -12
- package/lib/pagination/index.ts +0 -1
- package/lib/pagination/paginate.ts +0 -44
- package/lib/prisma.constants.ts +0 -40
- package/lib/prisma.decorator.ts +0 -8
- package/lib/prisma.health.ts +0 -40
- package/lib/prisma.interfaces.ts +0 -51
- package/lib/prisma.module.ts +0 -281
- package/lib/prisma.service.ts +0 -179
- package/lib/repository/abstract-prisma.repository.ts +0 -138
- package/lib/repository/index.ts +0 -1
- package/lib/testing/index.ts +0 -1
- package/lib/testing/prisma-testing.utils.ts +0 -29
- package/lib/transaction/index.ts +0 -2
- package/lib/transaction/prisma-transaction-manager.ts +0 -77
- package/lib/transaction/prisma-transaction.constants.ts +0 -29
- package/test/error-mapping.spec.ts +0 -231
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -29
- package/test/lifecycle.spec.ts +0 -241
- package/test/multi-db-als.spec.ts +0 -142
- package/test/multi-db-transaction.spec.ts +0 -75
- package/test/pagination/paginate.spec.ts +0 -359
- package/test/prisma-exception-filter.spec.ts +0 -213
- package/test/prisma-transaction-manager.spec.ts +0 -285
- package/test/prisma.decorator.spec.ts +0 -257
- package/test/prisma.health.spec.ts +0 -226
- package/test/prisma.module.spec.ts +0 -506
- package/test/prisma.service.spec.ts +0 -359
- package/test/repository/abstract-prisma.repository.spec.ts +0 -157
- package/test/transaction-manager-e2e.spec.ts +0 -496
- package/test/transaction.spec.ts +0 -371
- package/test/transparent-transaction.spec.ts +0 -500
- package/tsconfig.json +0 -9
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import { InjectPrisma } from '../lib/prisma.decorator';
|
|
4
|
-
import { PrismaModule } from '../lib/prisma.module';
|
|
5
|
-
import { PrismaService } from '../lib/prisma.service';
|
|
6
|
-
import { getPrismaToken } from '../lib/prisma.constants';
|
|
7
|
-
import type { PrismaClientLike } from '../lib/prisma.interfaces';
|
|
8
|
-
|
|
9
|
-
describe('InjectPrisma decorator', () => {
|
|
10
|
-
let mockClient: jest.Mocked<PrismaClientLike>;
|
|
11
|
-
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
mockClient = {
|
|
14
|
-
$connect: jest.fn().mockResolvedValue(undefined),
|
|
15
|
-
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
16
|
-
$on: jest.fn(),
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('without name (default connection)', () => {
|
|
21
|
-
it('should inject default PrismaService', async () => {
|
|
22
|
-
@Injectable()
|
|
23
|
-
class TestService {
|
|
24
|
-
constructor(@InjectPrisma() public readonly prisma: PrismaService) {}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
28
|
-
const module = await Test.createTestingModule({
|
|
29
|
-
imports: [
|
|
30
|
-
PrismaModule.forRoot({
|
|
31
|
-
client: ClientClass as any,
|
|
32
|
-
}),
|
|
33
|
-
],
|
|
34
|
-
providers: [TestService],
|
|
35
|
-
}).compile();
|
|
36
|
-
|
|
37
|
-
const testService = module.get(TestService);
|
|
38
|
-
expect(testService.prisma).toBeInstanceOf(PrismaService);
|
|
39
|
-
expect(testService.prisma).toBe(module.get(getPrismaToken()));
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should inject the same instance as direct token injection', async () => {
|
|
43
|
-
@Injectable()
|
|
44
|
-
class TestServiceA {
|
|
45
|
-
constructor(@InjectPrisma() public readonly prisma: PrismaService) {}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@Injectable()
|
|
49
|
-
class TestServiceB {
|
|
50
|
-
constructor(@InjectPrisma() public readonly prisma: PrismaService) {}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
54
|
-
const module = await Test.createTestingModule({
|
|
55
|
-
imports: [
|
|
56
|
-
PrismaModule.forRoot({
|
|
57
|
-
client: ClientClass as any,
|
|
58
|
-
}),
|
|
59
|
-
],
|
|
60
|
-
providers: [TestServiceA, TestServiceB],
|
|
61
|
-
}).compile();
|
|
62
|
-
|
|
63
|
-
const serviceA = module.get(TestServiceA);
|
|
64
|
-
const serviceB = module.get(TestServiceB);
|
|
65
|
-
|
|
66
|
-
expect(serviceA.prisma).toBe(serviceB.prisma);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('with name (named connection)', () => {
|
|
71
|
-
it('should inject named PrismaService', async () => {
|
|
72
|
-
@Injectable()
|
|
73
|
-
class AnalyticsService {
|
|
74
|
-
constructor(
|
|
75
|
-
@InjectPrisma('analytics') public readonly prisma: PrismaService,
|
|
76
|
-
) {}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
80
|
-
const module = await Test.createTestingModule({
|
|
81
|
-
imports: [
|
|
82
|
-
PrismaModule.forRoot({
|
|
83
|
-
client: ClientClass as any,
|
|
84
|
-
name: 'analytics',
|
|
85
|
-
}),
|
|
86
|
-
],
|
|
87
|
-
providers: [AnalyticsService],
|
|
88
|
-
}).compile();
|
|
89
|
-
|
|
90
|
-
const analyticsService = module.get(AnalyticsService);
|
|
91
|
-
expect(analyticsService.prisma).toBeInstanceOf(PrismaService);
|
|
92
|
-
expect(analyticsService.prisma).toBe(module.get(getPrismaToken('analytics')));
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should inject different instances for different named connections', async () => {
|
|
96
|
-
@Injectable()
|
|
97
|
-
class DefaultService {
|
|
98
|
-
constructor(@InjectPrisma() public readonly prisma: PrismaService) {}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
@Injectable()
|
|
102
|
-
class AnalyticsService {
|
|
103
|
-
constructor(
|
|
104
|
-
@InjectPrisma('analytics') public readonly prisma: PrismaService,
|
|
105
|
-
) {}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const ClientClass1 = jest.fn(() => mockClient);
|
|
109
|
-
const ClientClass2 = jest.fn(() => mockClient);
|
|
110
|
-
|
|
111
|
-
const module = await Test.createTestingModule({
|
|
112
|
-
imports: [
|
|
113
|
-
PrismaModule.forRoot({
|
|
114
|
-
client: ClientClass1 as any,
|
|
115
|
-
name: 'default',
|
|
116
|
-
}),
|
|
117
|
-
PrismaModule.forRoot({
|
|
118
|
-
client: ClientClass2 as any,
|
|
119
|
-
name: 'analytics',
|
|
120
|
-
}),
|
|
121
|
-
],
|
|
122
|
-
providers: [DefaultService, AnalyticsService],
|
|
123
|
-
}).compile();
|
|
124
|
-
|
|
125
|
-
const defaultService = module.get(DefaultService);
|
|
126
|
-
const analyticsService = module.get(AnalyticsService);
|
|
127
|
-
|
|
128
|
-
expect(defaultService.prisma).not.toBe(analyticsService.prisma);
|
|
129
|
-
expect(defaultService.prisma).toBe(module.get(getPrismaToken('default')));
|
|
130
|
-
expect(analyticsService.prisma).toBe(module.get(getPrismaToken('analytics')));
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
describe('multi-database scenario', () => {
|
|
135
|
-
it('should allow multiple services to use different databases', async () => {
|
|
136
|
-
@Injectable()
|
|
137
|
-
class UserRepository {
|
|
138
|
-
constructor(
|
|
139
|
-
@InjectPrisma() public readonly prisma: PrismaService,
|
|
140
|
-
) {}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
@Injectable()
|
|
144
|
-
class AnalyticsRepository {
|
|
145
|
-
constructor(
|
|
146
|
-
@InjectPrisma('analytics') public readonly prisma: PrismaService,
|
|
147
|
-
) {}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
@Injectable()
|
|
151
|
-
class LogRepository {
|
|
152
|
-
constructor(
|
|
153
|
-
@InjectPrisma('logs') public readonly prisma: PrismaService,
|
|
154
|
-
) {}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const ClientClass1 = jest.fn(() => mockClient);
|
|
158
|
-
const ClientClass2 = jest.fn(() => mockClient);
|
|
159
|
-
const ClientClass3 = jest.fn(() => mockClient);
|
|
160
|
-
|
|
161
|
-
const module = await Test.createTestingModule({
|
|
162
|
-
imports: [
|
|
163
|
-
PrismaModule.forRoot({
|
|
164
|
-
client: ClientClass1 as any,
|
|
165
|
-
name: 'default',
|
|
166
|
-
}),
|
|
167
|
-
PrismaModule.forRoot({
|
|
168
|
-
client: ClientClass2 as any,
|
|
169
|
-
name: 'analytics',
|
|
170
|
-
}),
|
|
171
|
-
PrismaModule.forRoot({
|
|
172
|
-
client: ClientClass3 as any,
|
|
173
|
-
name: 'logs',
|
|
174
|
-
}),
|
|
175
|
-
],
|
|
176
|
-
providers: [UserRepository, AnalyticsRepository, LogRepository],
|
|
177
|
-
}).compile();
|
|
178
|
-
|
|
179
|
-
const userRepo = module.get(UserRepository);
|
|
180
|
-
const analyticsRepo = module.get(AnalyticsRepository);
|
|
181
|
-
const logRepo = module.get(LogRepository);
|
|
182
|
-
|
|
183
|
-
expect(userRepo.prisma).toBeInstanceOf(PrismaService);
|
|
184
|
-
expect(analyticsRepo.prisma).toBeInstanceOf(PrismaService);
|
|
185
|
-
expect(logRepo.prisma).toBeInstanceOf(PrismaService);
|
|
186
|
-
|
|
187
|
-
expect(userRepo.prisma).not.toBe(analyticsRepo.prisma);
|
|
188
|
-
expect(userRepo.prisma).not.toBe(logRepo.prisma);
|
|
189
|
-
expect(analyticsRepo.prisma).not.toBe(logRepo.prisma);
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
describe('token resolution', () => {
|
|
194
|
-
it('should resolve to PRISMA_SERVICE_DEFAULT for undefined name', async () => {
|
|
195
|
-
@Injectable()
|
|
196
|
-
class TestService {
|
|
197
|
-
constructor(@InjectPrisma(undefined) public readonly prisma: PrismaService) {}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
201
|
-
const module = await Test.createTestingModule({
|
|
202
|
-
imports: [
|
|
203
|
-
PrismaModule.forRoot({
|
|
204
|
-
client: ClientClass as any,
|
|
205
|
-
}),
|
|
206
|
-
],
|
|
207
|
-
providers: [TestService],
|
|
208
|
-
}).compile();
|
|
209
|
-
|
|
210
|
-
const testService = module.get(TestService);
|
|
211
|
-
expect(testService.prisma).toBe(module.get(getPrismaToken(undefined)));
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('should resolve to PRISMA_SERVICE_DEFAULT for "default" name', async () => {
|
|
215
|
-
@Injectable()
|
|
216
|
-
class TestService {
|
|
217
|
-
constructor(@InjectPrisma('default') public readonly prisma: PrismaService) {}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
221
|
-
const module = await Test.createTestingModule({
|
|
222
|
-
imports: [
|
|
223
|
-
PrismaModule.forRoot({
|
|
224
|
-
client: ClientClass as any,
|
|
225
|
-
name: 'default',
|
|
226
|
-
}),
|
|
227
|
-
],
|
|
228
|
-
providers: [TestService],
|
|
229
|
-
}).compile();
|
|
230
|
-
|
|
231
|
-
const testService = module.get(TestService);
|
|
232
|
-
expect(testService.prisma).toBe(module.get(getPrismaToken('default')));
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('should resolve to PRISMA_SERVICE_{NAME} for custom name', async () => {
|
|
236
|
-
@Injectable()
|
|
237
|
-
class TestService {
|
|
238
|
-
constructor(@InjectPrisma('custom') public readonly prisma: PrismaService) {}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
const ClientClass = jest.fn(() => mockClient);
|
|
242
|
-
const module = await Test.createTestingModule({
|
|
243
|
-
imports: [
|
|
244
|
-
PrismaModule.forRoot({
|
|
245
|
-
client: ClientClass as any,
|
|
246
|
-
name: 'custom',
|
|
247
|
-
}),
|
|
248
|
-
],
|
|
249
|
-
providers: [TestService],
|
|
250
|
-
}).compile();
|
|
251
|
-
|
|
252
|
-
const testService = module.get(TestService);
|
|
253
|
-
expect(testService.prisma).toBe(module.get(getPrismaToken('custom')));
|
|
254
|
-
expect(testService.prisma).toBe(module.get('PRISMA_SERVICE_custom'));
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
});
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { PrismaHealthIndicator } from '../lib/prisma.health';
|
|
2
|
-
import { PrismaService } from '../lib/prisma.service';
|
|
3
|
-
import type { PrismaClientLike } from '../lib/prisma.interfaces';
|
|
4
|
-
|
|
5
|
-
describe('PrismaHealthIndicator', () => {
|
|
6
|
-
let healthIndicator: PrismaHealthIndicator;
|
|
7
|
-
let mockPrismaService: jest.Mocked<PrismaService>;
|
|
8
|
-
let mockClient: jest.Mocked<PrismaClientLike & { $queryRaw?: jest.Mock }>;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
mockClient = {
|
|
12
|
-
$connect: jest.fn().mockResolvedValue(undefined),
|
|
13
|
-
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
14
|
-
$on: jest.fn(),
|
|
15
|
-
$queryRaw: jest.fn(),
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
mockPrismaService = {
|
|
19
|
-
client: mockClient,
|
|
20
|
-
isConnected: true,
|
|
21
|
-
onModuleInit: jest.fn(),
|
|
22
|
-
onModuleDestroy: jest.fn(),
|
|
23
|
-
} as any;
|
|
24
|
-
|
|
25
|
-
healthIndicator = new PrismaHealthIndicator(mockPrismaService);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe('isHealthy', () => {
|
|
29
|
-
it('should return "up" when connected and query succeeds', async () => {
|
|
30
|
-
mockClient.$queryRaw?.mockResolvedValue([{ '?column?': 1 }]);
|
|
31
|
-
|
|
32
|
-
const result = await healthIndicator.isHealthy();
|
|
33
|
-
|
|
34
|
-
expect(result.status).toBe('up');
|
|
35
|
-
expect(result.message).toBeUndefined();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it('should execute a simple query to verify connectivity', async () => {
|
|
39
|
-
mockClient.$queryRaw?.mockResolvedValue([{ '?column?': 1 }]);
|
|
40
|
-
|
|
41
|
-
await healthIndicator.isHealthy();
|
|
42
|
-
|
|
43
|
-
expect(mockClient.$queryRaw).toHaveBeenCalled();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should return "down" when query fails', async () => {
|
|
47
|
-
const error = new Error('Connection lost');
|
|
48
|
-
mockClient.$queryRaw?.mockRejectedValue(error);
|
|
49
|
-
|
|
50
|
-
const result = await healthIndicator.isHealthy();
|
|
51
|
-
|
|
52
|
-
expect(result.status).toBe('down');
|
|
53
|
-
expect(result.message).toBe('Connection lost');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('should return "down" with message when database is unreachable', async () => {
|
|
57
|
-
const error = new Error('ECONNREFUSED');
|
|
58
|
-
mockClient.$queryRaw?.mockRejectedValue(error);
|
|
59
|
-
|
|
60
|
-
const result = await healthIndicator.isHealthy();
|
|
61
|
-
|
|
62
|
-
expect(result.status).toBe('down');
|
|
63
|
-
expect(result.message).toBe('ECONNREFUSED');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should handle timeout errors', async () => {
|
|
67
|
-
const error = new Error('Query timeout');
|
|
68
|
-
mockClient.$queryRaw?.mockRejectedValue(error);
|
|
69
|
-
|
|
70
|
-
const result = await healthIndicator.isHealthy();
|
|
71
|
-
|
|
72
|
-
expect(result.status).toBe('down');
|
|
73
|
-
expect(result.message).toBe('Query timeout');
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should handle authentication errors', async () => {
|
|
77
|
-
const error = new Error('Authentication failed');
|
|
78
|
-
mockClient.$queryRaw?.mockRejectedValue(error);
|
|
79
|
-
|
|
80
|
-
const result = await healthIndicator.isHealthy();
|
|
81
|
-
|
|
82
|
-
expect(result.status).toBe('down');
|
|
83
|
-
expect(result.message).toBe('Authentication failed');
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe('fallback when $queryRaw is not available', () => {
|
|
88
|
-
it('should check isConnected when $queryRaw does not exist', async () => {
|
|
89
|
-
const clientWithoutQueryRaw = {
|
|
90
|
-
$connect: jest.fn().mockResolvedValue(undefined),
|
|
91
|
-
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const serviceWithoutQueryRaw = {
|
|
95
|
-
client: clientWithoutQueryRaw,
|
|
96
|
-
isConnected: true,
|
|
97
|
-
onModuleInit: jest.fn(),
|
|
98
|
-
onModuleDestroy: jest.fn(),
|
|
99
|
-
} as any;
|
|
100
|
-
|
|
101
|
-
const indicator = new PrismaHealthIndicator(serviceWithoutQueryRaw);
|
|
102
|
-
const result = await indicator.isHealthy();
|
|
103
|
-
|
|
104
|
-
// Should still return health status based on isConnected or other mechanism
|
|
105
|
-
expect(result.status).toBeDefined();
|
|
106
|
-
expect(['up', 'down']).toContain(result.status);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should return "down" when isConnected is false', async () => {
|
|
110
|
-
mockPrismaService.isConnected = false;
|
|
111
|
-
|
|
112
|
-
const result = await healthIndicator.isHealthy();
|
|
113
|
-
|
|
114
|
-
expect(result.status).toBe('down');
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
describe('edge cases', () => {
|
|
119
|
-
it('should handle null error message', async () => {
|
|
120
|
-
const error = new Error();
|
|
121
|
-
error.message = '';
|
|
122
|
-
mockClient.$queryRaw?.mockRejectedValue(error);
|
|
123
|
-
|
|
124
|
-
const result = await healthIndicator.isHealthy();
|
|
125
|
-
|
|
126
|
-
expect(result.status).toBe('down');
|
|
127
|
-
expect(result.message).toBeDefined();
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('should handle non-Error exceptions', async () => {
|
|
131
|
-
mockClient.$queryRaw?.mockRejectedValue('String error');
|
|
132
|
-
|
|
133
|
-
const result = await healthIndicator.isHealthy();
|
|
134
|
-
|
|
135
|
-
expect(result.status).toBe('down');
|
|
136
|
-
expect(result.message).toBeDefined();
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should not throw errors even when health check fails', async () => {
|
|
140
|
-
mockClient.$queryRaw?.mockRejectedValue(new Error('Critical failure'));
|
|
141
|
-
|
|
142
|
-
await expect(healthIndicator.isHealthy()).resolves.not.toThrow();
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
describe('multiple consecutive checks', () => {
|
|
147
|
-
it('should correctly report status across multiple checks', async () => {
|
|
148
|
-
// First check succeeds
|
|
149
|
-
mockClient.$queryRaw?.mockResolvedValueOnce([{ '?column?': 1 }]);
|
|
150
|
-
const result1 = await healthIndicator.isHealthy();
|
|
151
|
-
expect(result1.status).toBe('up');
|
|
152
|
-
|
|
153
|
-
// Second check fails
|
|
154
|
-
mockClient.$queryRaw?.mockRejectedValueOnce(new Error('Temporary failure'));
|
|
155
|
-
const result2 = await healthIndicator.isHealthy();
|
|
156
|
-
expect(result2.status).toBe('down');
|
|
157
|
-
|
|
158
|
-
// Third check succeeds again
|
|
159
|
-
mockClient.$queryRaw?.mockResolvedValueOnce([{ '?column?': 1 }]);
|
|
160
|
-
const result3 = await healthIndicator.isHealthy();
|
|
161
|
-
expect(result3.status).toBe('up');
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
describe('integration with PrismaService', () => {
|
|
166
|
-
it('should use the client from PrismaService', async () => {
|
|
167
|
-
mockClient.$queryRaw?.mockResolvedValue([{ '?column?': 1 }]);
|
|
168
|
-
|
|
169
|
-
await healthIndicator.isHealthy();
|
|
170
|
-
|
|
171
|
-
expect(mockPrismaService.client).toBe(mockClient);
|
|
172
|
-
expect(mockClient.$queryRaw).toHaveBeenCalled();
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should work with different PrismaService instances', async () => {
|
|
176
|
-
const anotherClient = {
|
|
177
|
-
$connect: jest.fn(),
|
|
178
|
-
$disconnect: jest.fn(),
|
|
179
|
-
$queryRaw: jest.fn().mockResolvedValue([{ '?column?': 1 }]),
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
const anotherService = {
|
|
183
|
-
client: anotherClient,
|
|
184
|
-
isConnected: true,
|
|
185
|
-
} as any;
|
|
186
|
-
|
|
187
|
-
const anotherIndicator = new PrismaHealthIndicator(anotherService);
|
|
188
|
-
const result = await anotherIndicator.isHealthy();
|
|
189
|
-
|
|
190
|
-
expect(result.status).toBe('up');
|
|
191
|
-
expect(anotherClient.$queryRaw).toHaveBeenCalled();
|
|
192
|
-
expect(mockClient.$queryRaw).not.toHaveBeenCalled();
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
describe('health check result structure', () => {
|
|
197
|
-
it('should return HealthCheckResult with status "up"', async () => {
|
|
198
|
-
mockClient.$queryRaw?.mockResolvedValue([{ '?column?': 1 }]);
|
|
199
|
-
|
|
200
|
-
const result = await healthIndicator.isHealthy();
|
|
201
|
-
|
|
202
|
-
expect(result).toHaveProperty('status');
|
|
203
|
-
expect(result.status).toBe('up');
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should return HealthCheckResult with status "down" and message', async () => {
|
|
207
|
-
mockClient.$queryRaw?.mockRejectedValue(new Error('Test error'));
|
|
208
|
-
|
|
209
|
-
const result = await healthIndicator.isHealthy();
|
|
210
|
-
|
|
211
|
-
expect(result).toHaveProperty('status');
|
|
212
|
-
expect(result).toHaveProperty('message');
|
|
213
|
-
expect(result.status).toBe('down');
|
|
214
|
-
expect(result.message).toBe('Test error');
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it('should not include message when status is "up"', async () => {
|
|
218
|
-
mockClient.$queryRaw?.mockResolvedValue([{ '?column?': 1 }]);
|
|
219
|
-
|
|
220
|
-
const result = await healthIndicator.isHealthy();
|
|
221
|
-
|
|
222
|
-
expect(result.status).toBe('up');
|
|
223
|
-
expect(result.message).toBeUndefined();
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
});
|