prisma-mock 1.1.0-alpha.0 → 1.1.0-alpha.2

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
@@ -14,9 +14,30 @@ yarn add prisma-mock --dev
14
14
 
15
15
  ## Usage
16
16
 
17
- ### Basic Example
17
+ ### Prisma 7
18
18
 
19
- Simple example of how to create a prisma mock instance:
19
+ With Prisma 7, you typically generate your client to a custom output directory (such as `./generated/client`), and import the generated `Prisma` namespace from your generated files. Use the `prisma-mock/client` entry point and pass the `Prisma` namespace when
20
+ initializing your mock client.
21
+
22
+ > **Note:** You will also need to configure and use the [DMMF Generator](#dmmf-generator) to produce the necessary metadata for mocking.
23
+
24
+ ```js
25
+ import createPrismaMock from "prisma-mock/client"
26
+ import { Prisma } from "./generated/client"
27
+ import * as dmmf from "./generated/dmmf"
28
+
29
+ let client
30
+
31
+ beforeEach(() => {
32
+ client = createPrismaMock(Prisma, {
33
+ datamodel: dmmf,
34
+ })
35
+ })
36
+ ```
37
+
38
+ ### Prisma 6 and below
39
+
40
+ Simple example of how to create a prisma mock instance using the default (node_module) export:
20
41
 
21
42
  ```js
22
43
  import createPrismaMock from "prisma-mock"
@@ -97,7 +118,11 @@ createPrismaMock<P extends PrismaClient = PrismaClient>(
97
118
  caseInsensitive?: boolean
98
119
  enableIndexes?: boolean
99
120
  }
100
- ): P & { $getInternalState: () => Required<PrismaMockData<P>> }
121
+ ): P & {
122
+ $getInternalState: () => Required<PrismaMockData<P>>
123
+ $setInternalState: (state: Required<PrismaMockData<P>>) => void
124
+ $clear: () => void
125
+ }
101
126
  ```
102
127
 
103
128
  ### Parameters
@@ -117,6 +142,8 @@ createPrismaMock<P extends PrismaClient = PrismaClient>(
117
142
  Returns a mock Prisma client with all standard model methods plus:
118
143
 
119
144
  - `$getInternalState()`: Method to access the internal data state for testing/debugging
145
+ - `$setInternalState(state)`: Method to replace the entire internal data state. Useful for resetting state to a specific snapshot or setting up complex test scenarios. The state is deep copied to avoid reference issues.
146
+ - `$clear()`: Method to reset the internal state back to the initial state
120
147
 
121
148
  ### Client Export (`prisma-mock/client`)
122
149
 
@@ -130,7 +157,11 @@ createPrismaMock<PClient extends PrismaClient, P extends typeof Prisma = typeof
130
157
  caseInsensitive?: boolean
131
158
  enableIndexes?: boolean
132
159
  }
133
- ): PClient & { $getInternalState: () => Required<PrismaMockData<PClient>> }
160
+ ): PClient & {
161
+ $getInternalState: () => Required<PrismaMockData<PClient>>
162
+ $setInternalState: (state: Required<PrismaMockData<PClient>>) => void
163
+ $clear: () => void
164
+ }
134
165
  ```
135
166
 
136
167
  #### Parameters
package/lib/client.d.ts CHANGED
@@ -2,5 +2,7 @@ import type { Prisma, PrismaClient } from "@prisma/client";
2
2
  import { MockPrismaOptions, PrismaMockData } from "./types";
3
3
  declare function createPrismaMock<PClient extends PrismaClient, P extends typeof Prisma = typeof Prisma>(prisma: P, options?: MockPrismaOptions<PClient, P>): PClient & {
4
4
  $getInternalState: () => Required<PrismaMockData<PClient>>;
5
+ $setInternalState: (state: Required<PrismaMockData<PClient>>) => void;
6
+ $clear: () => void;
5
7
  };
6
8
  export default createPrismaMock;
package/lib/client.js CHANGED
@@ -22,9 +22,10 @@ function createPrismaMock(prisma, options = {
22
22
  enableIndexes: true,
23
23
  data: {}
24
24
  }) {
25
+ let internalState = options.data ? (0, deepCopy_1.deepCopy)(options.data) : {};
25
26
  // Reference object to hold the mock data state
26
27
  let ref = {
27
- data: options.data ? (0, deepCopy_1.deepCopy)(options.data) : {},
28
+ data: internalState,
28
29
  };
29
30
  // Initialize the mock client (either use provided one or create new)
30
31
  let client = options.mockClient ? options.mockClient : {};
@@ -126,8 +127,12 @@ function createPrismaMock(prisma, options = {
126
127
  });
127
128
  // Add method to access internal state for testing/debugging
128
129
  client['$getInternalState'] = () => ref.data;
130
+ client['$setInternalState'] = (state) => {
131
+ internalState = (0, deepCopy_1.deepCopy)(state);
132
+ ref.data = internalState;
133
+ };
129
134
  client['$clear'] = () => {
130
- ref.data = options.data ? (0, deepCopy_1.deepCopy)(options.data) : {};
135
+ ref.data = internalState;
131
136
  };
132
137
  // @ts-ignore
133
138
  return client;
package/lib/index.d.ts CHANGED
@@ -5,5 +5,7 @@ type MockPrismaOptions<P extends PrismaClient = PrismaClient> = Omit<MockPrismaO
5
5
  };
6
6
  export default function createPrismaClient<P extends PrismaClient = PrismaClient>(options?: MockPrismaOptions<P>): P & {
7
7
  $getInternalState: () => Required<Partial<{ [key in import("./types").IsTable<Uncapitalize<import("./types").IsString<keyof P>>>]: import("./types").PrismaList<P, key>; }>>;
8
+ $setInternalState: (state: Required<Partial<{ [key in import("./types").IsTable<Uncapitalize<import("./types").IsString<keyof P>>>]: import("./types").PrismaList<P, key>; }>>) => void;
9
+ $clear: () => void;
8
10
  };
9
11
  export {};
package/lib/legacy.d.ts CHANGED
@@ -73,4 +73,74 @@ export default function createPrismaClient(data?: PrismaMockData<PrismaClient>,
73
73
  id: string;
74
74
  }>[];
75
75
  }>>;
76
+ $setInternalState: (state: Required<Partial<{
77
+ account: Partial<{
78
+ name: string;
79
+ id: number;
80
+ sort: number;
81
+ }>[];
82
+ user: Partial<{
83
+ name: string;
84
+ id: number;
85
+ sort: number;
86
+ accountId: number;
87
+ role: import(".prisma/client").$Enums.Role;
88
+ clicks: number;
89
+ deleted: boolean;
90
+ uniqueField: string;
91
+ age: number;
92
+ }>[];
93
+ answers: Partial<{
94
+ id: number;
95
+ title: string;
96
+ }>[];
97
+ element: Partial<{
98
+ value: string;
99
+ userId: number;
100
+ e_id: number;
101
+ json: Prisma.JsonValue;
102
+ }>[];
103
+ stripe: Partial<{
104
+ id: number;
105
+ sort: number;
106
+ accountId: number;
107
+ customerId: string;
108
+ active: boolean;
109
+ }>[];
110
+ userAnswers: Partial<{
111
+ value: string;
112
+ answerId: number;
113
+ userId: number;
114
+ }>[];
115
+ document: Partial<{
116
+ name: string;
117
+ id: string;
118
+ }>[];
119
+ post: Partial<{
120
+ id: number;
121
+ title: string;
122
+ published: boolean;
123
+ authorId: number;
124
+ updated: Date;
125
+ created: Date;
126
+ }>[];
127
+ pet: Partial<{
128
+ name: string;
129
+ id: number;
130
+ ownerId: number;
131
+ }>[];
132
+ toy: Partial<{
133
+ name: string;
134
+ id: number;
135
+ ownerId: number;
136
+ }>[];
137
+ transaction: Partial<{
138
+ id: string;
139
+ initiator: string;
140
+ }>[];
141
+ dbGeneratedId: Partial<{
142
+ id: string;
143
+ }>[];
144
+ }>>) => void;
145
+ $clear: () => void;
76
146
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prisma-mock",
3
- "version": "1.1.0-alpha.0",
3
+ "version": "1.1.0-alpha.2",
4
4
  "description": "Mock prisma for unit testing database",
5
5
  "main": "lib/index.js",
6
6
  "repository": {