@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 CHANGED
@@ -1,8 +1,14 @@
1
1
  # prismock
2
2
 
3
- [![npm](https://img.shields.io/npm/v/prismock)](https://www.npmjs.com/package/prismock)
4
- [![Build](https://circleci.com/gh/morintd/prismock.svg?style=shield)](https://app.circleci.com/pipelines/github/morintd/prismock)
5
- [![npm](https://img.shields.io/npm/dm/prismock)](https://www.npmjs.com/package/prismock)
3
+ [![npm](https://img.shields.io/npm/v/@pkgverse/prismock)](https://www.npmjs.com/package/@pkgverse/prismock)
4
+ [![npm](https://img.shields.io/npm/dm/@pkgverse/prismock)](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 { PrismockClient } from 'prismock';
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
- export * from '@prisma/client';
45
- export { PrismockClient as PrismaClient };
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 Jest & ViTest)
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/morintd/prismock/blob/master/src/__tests__/example-prismock.test.ts)):
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
- jest.mock('@prisma/client', () => {
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
- ...jest.requireActual('@prisma/client'),
58
- PrismaClient: jest.requireActual('prismock').PrismockClient,
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 { PrismockClient } from 'prismock';
75
+ import { Prisma } from '${your_prisma_client_directory}';
76
+ import { createPrismockClient } from 'prismock';
69
77
 
70
- import { PrismaService } from './prisma.service';
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`. In some edge-case, we need to directly access, or reset, the data store management by _prismock_.
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(prismaModule) {
5245
- const { Prisma: Prisma2 } = await import("@prisma/client");
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(Prisma2);
5254
+ super(prismaModule);
5249
5255
  }
5250
5256
  };
5251
5257
  return c;
@@ -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(prismaModule?: PrismaModule): Promise<typeof PrismaClient>;
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 {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pkgverse/prismock",
3
- "version": "2.0.0-beta.10",
3
+ "version": "2.0.0-beta.12",
4
4
  "description": "A mock for PrismaClient, dedicated to unit testing.",
5
5
  "repository": {
6
6
  "url": "https://github.com/JQuezada0/prismock"