@pkgverse/prismock 2.0.0-beta.10 → 2.0.0-beta.12
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 +32 -33
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +9 -3
- package/dist/lib/client.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# prismock
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/prismock)
|
|
4
|
-
[](https://www.npmjs.com/package/@pkgverse/prismock)
|
|
4
|
+
[](https://www.npmjs.com/package/prismock)
|
|
5
|
+
|
|
6
|
+
## NOTE
|
|
7
|
+
Originally forked from https://github.com/morintd/prismock. This library is awesome, and I felt it could use some modernization to work with newer versions of prisma and add support for
|
|
8
|
+
client extensions. My current intention is to try to maintain this and stay up to speed with bug-fixes. The focus is ESM-first, so although both ESM and CJS exports are provided,
|
|
9
|
+
I haven't personally tested the CJS functionality.
|
|
10
|
+
|
|
11
|
+
---
|
|
6
12
|
|
|
7
13
|
This is a mock for `PrismaClient`. It actually reads your `schema.prisma` and generate models based on it.
|
|
8
14
|
|
|
@@ -19,43 +25,44 @@ After setting up [Prisma](https://www.prisma.io/docs/getting-started/setup-prism
|
|
|
19
25
|
yarn
|
|
20
26
|
|
|
21
27
|
```sh
|
|
22
|
-
$ yarn add -D prismock
|
|
28
|
+
$ yarn add -D @pkgverse/prismock
|
|
23
29
|
```
|
|
24
30
|
|
|
25
31
|
npm
|
|
26
32
|
|
|
27
33
|
```
|
|
28
|
-
$ npm add --save-dev prismock
|
|
34
|
+
$ npm add --save-dev @pkgverse/prismock
|
|
29
35
|
```
|
|
30
36
|
|
|
31
37
|
# Usage
|
|
32
38
|
|
|
33
|
-
There are a few options here, depending on your application architecture.
|
|
34
|
-
|
|
35
|
-
## Automatically (recommended)
|
|
36
|
-
|
|
37
|
-
You can create a `__mocks__` directory at the root of your project, with a sub-directory named `@prisma`. Inside the `@prisma` directory, create a `client.js` file (or `client.ts` for TypeScript).
|
|
38
|
-
|
|
39
|
-
Inside the `client` file, you can re-export the `@prisma/client` module, and replace `PrismaClient` by `PrismockClient`:
|
|
40
|
-
|
|
41
39
|
```ts
|
|
42
|
-
import {
|
|
40
|
+
import { Prisma, PrismaClient } from "${your_prisma_client_directory}"
|
|
41
|
+
import { createPrismockClient } from 'prismock';
|
|
42
|
+
|
|
43
|
+
// Pass in the Prisma namespace, and manually define the type of your prisma client
|
|
44
|
+
let mockedClient = await createPrismockClient<PrismaClient>(Prisma)
|
|
43
45
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
// Optionally apply your client extensions to the client
|
|
47
|
+
mockedClient = applyExtensions(mockedClient)
|
|
46
48
|
```
|
|
47
49
|
|
|
48
|
-
That's it, prisma will be mocked in all your tests (tested with
|
|
50
|
+
That's it, prisma will be mocked in all your tests (tested with ViTest)
|
|
49
51
|
|
|
50
52
|
## PrismaClient
|
|
51
53
|
|
|
52
|
-
You can mock the PrismaClient directly in your test, or setupTests ([Example](https://github.com/
|
|
54
|
+
You can mock the PrismaClient directly in your test, or setupTests ([Example](https://github.com/JQuezada0/prismock/blob/beta/src/__tests__/client/example-prismock.test.ts)):
|
|
53
55
|
|
|
54
56
|
```ts
|
|
55
|
-
|
|
57
|
+
import { vi } from "vitest"
|
|
58
|
+
|
|
59
|
+
vi.mock('@prisma/client', async () => {
|
|
60
|
+
const actual = await vi.importActual<typeof import("@prisma/client")>("@prisma/client")
|
|
61
|
+
const actualPrismock = await vi.importActual<typeof import("prismock")>("prismock")
|
|
62
|
+
|
|
56
63
|
return {
|
|
57
|
-
...
|
|
58
|
-
PrismaClient:
|
|
64
|
+
...actual,
|
|
65
|
+
PrismaClient: await actualPrismock.createPrismockClass(actual.Prisma),
|
|
59
66
|
};
|
|
60
67
|
});
|
|
61
68
|
```
|
|
@@ -65,29 +72,21 @@ jest.mock('@prisma/client', () => {
|
|
|
65
72
|
You can instantiate a `PrismockClient` directly and use it in your test, or pass it to a test version of your app.
|
|
66
73
|
|
|
67
74
|
```ts
|
|
68
|
-
import {
|
|
75
|
+
import { Prisma } from '${your_prisma_client_directory}';
|
|
76
|
+
import { createPrismockClient } from 'prismock';
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const prismock = new PrismockClient();
|
|
73
|
-
const app = createApp(prismock);
|
|
78
|
+
const client = await createPrismockClient(Prisma);
|
|
74
79
|
```
|
|
75
80
|
|
|
76
81
|
Then, you will be able to write your tests as if your app was using an in-memory Prisma client.
|
|
77
82
|
|
|
78
|
-
## Using custom client path
|
|
79
|
-
|
|
80
|
-
If you are using a custom [client path](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path), you need the [createPrismock](https://github.com/morintd/prismock/blob/master/docs/using-custom-client-path.md) method.
|
|
81
|
-
|
|
82
83
|
## Use with decimal.js
|
|
83
84
|
|
|
84
85
|
See [use with decimal.js](https://github.com/morintd/prismock/blob/master/docs/use-with-decimal-js.md).
|
|
85
86
|
|
|
86
87
|
## Internal data
|
|
87
88
|
|
|
88
|
-
Two additional functions are returned compared to the PrismaClient, `getData`, and `reset`.
|
|
89
|
-
|
|
90
|
-
Most of the time, you won't need it in your test, but keep in mind they exist
|
|
89
|
+
Two additional functions are returned compared to the PrismaClient, `getData`, and `reset`.
|
|
91
90
|
|
|
92
91
|
```ts
|
|
93
92
|
const prismock = new PrismockClient();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { generatePrismock, generatePrismockSync } from './lib/prismock';
|
|
2
|
-
export { createPrismock, createPrismockClient, Prismock, type PrismaModule, createPrismockClass } from './lib/client';
|
|
2
|
+
export { createPrismock, createPrismockClient, Prismock, type PrismaModule, createPrismockClass, PrismockClientType } from './lib/client';
|
package/dist/index.mjs
CHANGED
|
@@ -5241,11 +5241,17 @@ function generateClient(delegates, getData, setData) {
|
|
|
5241
5241
|
}
|
|
5242
5242
|
};
|
|
5243
5243
|
}
|
|
5244
|
-
async function createPrismockClass(
|
|
5245
|
-
const
|
|
5244
|
+
async function createPrismockClass(prismaModuleInput) {
|
|
5245
|
+
const prismaModule = await (async () => {
|
|
5246
|
+
if (prismaModuleInput) {
|
|
5247
|
+
return prismaModuleInput;
|
|
5248
|
+
}
|
|
5249
|
+
const { Prisma: Prisma2 } = await import("@prisma/client");
|
|
5250
|
+
return Prisma2;
|
|
5251
|
+
})();
|
|
5246
5252
|
const c = class PrismockClientDefault extends Prismock {
|
|
5247
5253
|
constructor() {
|
|
5248
|
-
super(
|
|
5254
|
+
super(prismaModule);
|
|
5249
5255
|
}
|
|
5250
5256
|
};
|
|
5251
5257
|
return c;
|
package/dist/lib/client.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ export declare function generateClient<T = PrismaClient>(delegates: Record<strin
|
|
|
33
33
|
export type PrismaModule<PC = PrismaClient> = {
|
|
34
34
|
dmmf: runtime.BaseDMMF;
|
|
35
35
|
};
|
|
36
|
-
export declare function createPrismockClass(
|
|
36
|
+
export declare function createPrismockClass<PC extends (new (...args: any[]) => any) = typeof PrismaClient>(prismaModuleInput?: PrismaModule): Promise<typeof PrismaClient>;
|
|
37
37
|
export declare function createPrismock(): Promise<PrismaClient<import(".prisma").Prisma.PrismaClientOptions, never, runtime.DefaultArgs> & PrismockData>;
|
|
38
38
|
export declare function createPrismockClient<PC = PrismaClient>(prismaModule: PrismaModule<PC>): Promise<PrismockClientType<PC>>;
|
|
39
39
|
export {};
|