@nathapp/nestjs-prisma 4.1.0 → 4.1.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/README.md
CHANGED
|
@@ -123,6 +123,23 @@ advisories when you bump.
|
|
|
123
123
|
|
|
124
124
|
## Usage
|
|
125
125
|
|
|
126
|
+
### Test mocks
|
|
127
|
+
|
|
128
|
+
CommonJS Jest tests can call `createMockPrismaClient()` and
|
|
129
|
+
`createMockPrismaService()` directly, without assigning `globalThis.jest`.
|
|
130
|
+
For ESM tests, or another compatible runner, supply the mock factory:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import { jest } from '@jest/globals';
|
|
134
|
+
import { createMockPrismaClient, createMockPrismaService } from '@nathapp/nestjs-prisma';
|
|
135
|
+
|
|
136
|
+
const client = createMockPrismaClient({ fn: jest.fn });
|
|
137
|
+
const service = createMockPrismaService({ fn: jest.fn });
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Each method gets its own mock. Importing the package outside a runner remains
|
|
141
|
+
safe; requesting mocks without a factory or Jest raises a descriptive error.
|
|
142
|
+
|
|
126
143
|
Single-DB setup and a repository implementation:
|
|
127
144
|
|
|
128
145
|
```typescript
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { PrismaClientLike } from '../prisma.interfaces';
|
|
2
|
-
export
|
|
3
|
-
|
|
2
|
+
export interface PrismaMockOptions {
|
|
3
|
+
fn?: () => any;
|
|
4
|
+
}
|
|
5
|
+
export declare function createMockPrismaClient(options?: PrismaMockOptions): PrismaClientLike & Record<string, any>;
|
|
6
|
+
export declare function createMockPrismaService(options?: PrismaMockOptions): {
|
|
4
7
|
client: PrismaClientLike & Record<string, any>;
|
|
5
8
|
isConnected: boolean;
|
|
6
9
|
onModuleInit: any;
|
|
@@ -2,30 +2,34 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createMockPrismaClient = createMockPrismaClient;
|
|
4
4
|
exports.createMockPrismaService = createMockPrismaService;
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
function mockFactory(options) {
|
|
6
|
+
if (typeof options.fn === 'function')
|
|
7
|
+
return options.fn;
|
|
8
|
+
const runner = typeof jest !== 'undefined' ? jest : globalThis.jest;
|
|
9
|
+
if (typeof (runner === null || runner === void 0 ? void 0 : runner.fn) !== 'function') {
|
|
10
|
+
throw new Error('Prisma mock helpers require jest.fn; pass { fn: jest.fn } when using an imported test runner.');
|
|
9
11
|
}
|
|
10
|
-
return
|
|
12
|
+
return () => runner.fn();
|
|
11
13
|
}
|
|
12
|
-
function createMockPrismaClient() {
|
|
14
|
+
function createMockPrismaClient(options = {}) {
|
|
15
|
+
const fn = mockFactory(options);
|
|
13
16
|
return {
|
|
14
|
-
$connect:
|
|
15
|
-
$disconnect:
|
|
16
|
-
$on:
|
|
17
|
-
$queryRaw:
|
|
18
|
-
$executeRaw:
|
|
19
|
-
$transaction:
|
|
17
|
+
$connect: fn().mockResolvedValue(undefined),
|
|
18
|
+
$disconnect: fn().mockResolvedValue(undefined),
|
|
19
|
+
$on: fn(),
|
|
20
|
+
$queryRaw: fn(),
|
|
21
|
+
$executeRaw: fn(),
|
|
22
|
+
$transaction: fn(),
|
|
20
23
|
};
|
|
21
24
|
}
|
|
22
|
-
function createMockPrismaService() {
|
|
23
|
-
const
|
|
25
|
+
function createMockPrismaService(options = {}) {
|
|
26
|
+
const fn = mockFactory(options);
|
|
27
|
+
const mockClient = createMockPrismaClient({ fn });
|
|
24
28
|
return {
|
|
25
29
|
client: mockClient,
|
|
26
30
|
isConnected: true,
|
|
27
|
-
onModuleInit:
|
|
28
|
-
onModuleDestroy:
|
|
31
|
+
onModuleInit: fn().mockResolvedValue(undefined),
|
|
32
|
+
onModuleDestroy: fn().mockResolvedValue(undefined),
|
|
29
33
|
};
|
|
30
34
|
}
|
|
31
35
|
//# sourceMappingURL=prisma-testing.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma-testing.utils.js","sourceRoot":"","sources":["../../lib/testing/prisma-testing.utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"prisma-testing.utils.js","sourceRoot":"","sources":["../../lib/testing/prisma-testing.utils.ts"],"names":[],"mappings":";;AAuBA,wDAUC;AAKD,0DASC;AAvCD,SAAS,WAAW,CAAC,OAA0B;IAC7C,IAAI,OAAO,OAAO,CAAC,EAAE,KAAK,UAAU;QAAE,OAAO,OAAO,CAAC,EAAE,CAAC;IAGxD,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAE,UAA4C,CAAC,IAAI,CAAC;IACvG,IAAI,OAAO,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,EAAE,CAAA,KAAK,UAAU,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;IACnH,CAAC;IACD,OAAO,GAAG,EAAE,CAAC,MAAM,CAAC,EAAG,EAAE,CAAC;AAC5B,CAAC;AAMD,SAAgB,sBAAsB,CAAC,UAA6B,EAAE;IACpE,MAAM,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAChC,OAAO;QACL,QAAQ,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC3C,WAAW,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC9C,GAAG,EAAE,EAAE,EAAE;QACT,SAAS,EAAE,EAAE,EAAE;QACf,WAAW,EAAE,EAAE,EAAE;QACjB,YAAY,EAAE,EAAE,EAAE;KACnB,CAAC;AACJ,CAAC;AAKD,SAAgB,uBAAuB,CAAC,UAA6B,EAAE;IACrE,MAAM,EAAE,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAChC,MAAM,UAAU,GAAG,sBAAsB,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;IAClD,OAAO;QACL,MAAM,EAAE,UAAU;QAClB,WAAW,EAAE,IAAI;QACjB,YAAY,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;QAC/C,eAAe,EAAE,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;KACnD,CAAC;AACJ,CAAC"}
|