prisma-mock 1.0.0-alpha.5 → 1.0.0-alpha.6
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 +129 -40
- package/lib/index.d.ts +6 -2
- package/lib/index.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A comprehensive mock of the Prisma API intended for unit testing. All data is stored in memory, providing fast and reliable test execution without external dependencies.
|
|
4
4
|
|
|
5
|
-
The library uses `jest-mock-extended` or `vitest-mock-extended
|
|
5
|
+
The library optionally uses `jest-mock-extended` or `vitest-mock-extended` if you provide a mock client. If functionality you need is not implemented yet, you can mock it yourself.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -28,9 +28,35 @@ beforeEach(() => {
|
|
|
28
28
|
})
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
###
|
|
31
|
+
### With Initial Data
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
You can optionally start with pre-filled data:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import createPrismaMock from "prisma-mock"
|
|
37
|
+
|
|
38
|
+
const client = createPrismaMock({
|
|
39
|
+
data: {
|
|
40
|
+
user: [
|
|
41
|
+
{
|
|
42
|
+
id: 1,
|
|
43
|
+
name: "John Doe",
|
|
44
|
+
accountId: 1,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
account: [
|
|
48
|
+
{
|
|
49
|
+
id: 1,
|
|
50
|
+
name: "Company",
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Example: Mocking a Global Prisma Instance
|
|
58
|
+
|
|
59
|
+
Example of how to mock a global prisma instance, for instance when it's the default export in a "db" directory (like in BlitzJS):
|
|
34
60
|
|
|
35
61
|
```js
|
|
36
62
|
import createPrismaMock from "prisma-mock"
|
|
@@ -42,68 +68,87 @@ jest.mock("db", () => ({
|
|
|
42
68
|
default: mockDeep(),
|
|
43
69
|
}))
|
|
44
70
|
|
|
45
|
-
import db
|
|
71
|
+
import db from "db"
|
|
46
72
|
|
|
47
73
|
beforeEach(() => {
|
|
48
74
|
mockReset(db)
|
|
49
|
-
createPrismaMock({}
|
|
75
|
+
createPrismaMock({ mockClient: db })
|
|
50
76
|
})
|
|
51
77
|
```
|
|
52
78
|
|
|
53
|
-
###
|
|
79
|
+
### Exports
|
|
54
80
|
|
|
55
|
-
|
|
81
|
+
The library provides three different exports:
|
|
82
|
+
|
|
83
|
+
- **`prisma-mock`** (default): The recommended way to use the library. Automatically uses the Prisma client from `@prisma/client/default`, so you don't need to pass Prisma as an argument.
|
|
84
|
+
- **`prisma-mock/client`**: Use this when you need to explicitly pass the Prisma namespace (try the default export first).
|
|
85
|
+
- **`prisma-mock/legacy`**: The old API for backward compatibility. This export is deprecated but maintained for existing codebases.
|
|
86
|
+
|
|
87
|
+
### Legacy Export
|
|
88
|
+
|
|
89
|
+
The legacy export maintains the old API signature for backward compatibility:
|
|
56
90
|
|
|
57
91
|
```js
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
id: 1,
|
|
69
|
-
name: "Company",
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
})
|
|
92
|
+
import createPrismaMock from "prisma-mock/legacy"
|
|
93
|
+
import { mockDeep } from "jest-mock-extended"
|
|
94
|
+
|
|
95
|
+
const client = createPrismaMock(
|
|
96
|
+
{ user: [{ id: 1, name: "John" }] }, // data
|
|
97
|
+
Prisma.dmmf.datamodel, // datamodel (optional)
|
|
98
|
+
mockDeep(), // mockClient (optional)
|
|
99
|
+
{ enableIndexes: true } // options (optional)
|
|
100
|
+
)
|
|
73
101
|
```
|
|
74
102
|
|
|
103
|
+
**Note**: If you're starting a new project, use the default export instead. The legacy export is only for maintaining existing codebases that haven't migrated yet.
|
|
104
|
+
|
|
75
105
|
## API
|
|
76
106
|
|
|
107
|
+
### Default Export (`prisma-mock`)
|
|
108
|
+
|
|
77
109
|
```ts
|
|
78
|
-
createPrismaMock(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
110
|
+
createPrismaMock<P extends PrismaClient = PrismaClient>(
|
|
111
|
+
options?: {
|
|
112
|
+
data?: PrismaMockData<P>
|
|
113
|
+
datamodel?: Prisma.DMMF.Datamodel
|
|
114
|
+
mockClient?: DeepMockApi
|
|
83
115
|
caseInsensitive?: boolean
|
|
84
116
|
enableIndexes?: boolean
|
|
85
|
-
}
|
|
86
|
-
):
|
|
117
|
+
}
|
|
118
|
+
): P & { $getInternalState: () => Required<PrismaMockData<P>> }
|
|
87
119
|
```
|
|
88
120
|
|
|
89
|
-
###
|
|
90
|
-
|
|
91
|
-
#### `data` (optional)
|
|
121
|
+
### Client Export (`prisma-mock/client`)
|
|
92
122
|
|
|
93
|
-
|
|
123
|
+
```ts
|
|
124
|
+
createPrismaMock<PClient extends PrismaClient, P extends typeof Prisma = typeof Prisma>(
|
|
125
|
+
prisma: P,
|
|
126
|
+
options?: {
|
|
127
|
+
data?: PrismaMockData<PClient>
|
|
128
|
+
datamodel?: P["dmmf"]["datamodel"]
|
|
129
|
+
mockClient?: DeepMockApi
|
|
130
|
+
caseInsensitive?: boolean
|
|
131
|
+
enableIndexes?: boolean
|
|
132
|
+
}
|
|
133
|
+
): PClient & { $getInternalState: () => Required<PrismaMockData<PClient>> }
|
|
134
|
+
```
|
|
94
135
|
|
|
95
|
-
|
|
136
|
+
### Parameters
|
|
96
137
|
|
|
97
|
-
|
|
138
|
+
#### Default Export
|
|
98
139
|
|
|
99
|
-
|
|
140
|
+
- **`options`** (optional): Configuration options (see below)
|
|
100
141
|
|
|
101
|
-
|
|
142
|
+
#### Client Export
|
|
102
143
|
|
|
103
|
-
|
|
144
|
+
- **`prisma`** (required): The Prisma namespace (e.g., `Prisma` from `@prisma/client`). This is used to access the datamodel and type information.
|
|
145
|
+
- **`options`** (optional): Configuration options (see below)
|
|
104
146
|
|
|
105
|
-
|
|
147
|
+
#### Options
|
|
106
148
|
|
|
149
|
+
- **`data`** (optional): Initial mock data for the Prisma models. An object containing keys for tables and values as arrays of objects.
|
|
150
|
+
- **`datamodel`** (optional): The Prisma datamodel, typically `Prisma.dmmf.datamodel`. Defaults to the Prisma client's datamodel.
|
|
151
|
+
- **`mockClient`** (optional): A `jest-mock-extended` or `vitest-mock-extended` instance. If not provided, a plain object is used instead.
|
|
107
152
|
- **`caseInsensitive`** (boolean, default: `false`): If true, all string comparisons are case insensitive
|
|
108
153
|
- **`enableIndexes`** (boolean, default: `false`) Experimental: If true, enables indexing for better query performance on primary keys, unique fields, and foreign keys
|
|
109
154
|
|
|
@@ -113,6 +158,48 @@ Returns a mock Prisma client with all standard model methods plus:
|
|
|
113
158
|
|
|
114
159
|
- `$getInternalState()`: Method to access the internal data state for testing/debugging
|
|
115
160
|
|
|
161
|
+
## DMMF Generator
|
|
162
|
+
|
|
163
|
+
The library includes a Prisma generator that can be used to generate the datamodel separately. This is useful when you need to use the datamodel without having the full Prisma client available, or when you want to use a specific version of the datamodel.
|
|
164
|
+
|
|
165
|
+
### When to Use the DMMF Generator
|
|
166
|
+
|
|
167
|
+
You typically need the DMMF generator when:
|
|
168
|
+
|
|
169
|
+
- You're running tests with jsdom
|
|
170
|
+
- When you have a custom export directory for the Prisma client
|
|
171
|
+
- You're building a tool that needs the datamodel structure without the full Prisma client
|
|
172
|
+
- You want to use a specific version of the datamodel that differs from the current Prisma client
|
|
173
|
+
|
|
174
|
+
### Setup
|
|
175
|
+
|
|
176
|
+
Add the generator to your `schema.prisma` file:
|
|
177
|
+
|
|
178
|
+
```prisma
|
|
179
|
+
generator client {
|
|
180
|
+
provider = "prisma-client-js"
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
generator prisma-mock {
|
|
184
|
+
provider = "prisma-mock"
|
|
185
|
+
output = "./generated/dmmf"
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
After running `prisma generate`, the datamodel will be exported to the specified output path. You can then import and use it:
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
import createPrismaMock from "prisma-mock/client"
|
|
193
|
+
import { Prisma } from "@prisma/client"
|
|
194
|
+
import * as dmmf from "./generated/dmmf"
|
|
195
|
+
|
|
196
|
+
const client = createPrismaMock(Prisma, {
|
|
197
|
+
datamodel: dmmf,
|
|
198
|
+
})
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**Note**: In most cases, you don't need the generator. The library will automatically use `Prisma.dmmf.datamodel` from your Prisma client if you don't provide a custom datamodel.
|
|
202
|
+
|
|
116
203
|
## Supported Features
|
|
117
204
|
|
|
118
205
|
### Model Queries ✅
|
|
@@ -271,7 +358,9 @@ The following features are planned but not yet implemented:
|
|
|
271
358
|
Enable indexing for better query performance:
|
|
272
359
|
|
|
273
360
|
```js
|
|
274
|
-
|
|
361
|
+
import createPrismaMock from "prisma-mock"
|
|
362
|
+
|
|
363
|
+
const client = createPrismaMock({
|
|
275
364
|
enableIndexes: true,
|
|
276
365
|
})
|
|
277
366
|
```
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { Prisma, PrismaClient } from "@prisma/client/default";
|
|
2
|
-
import { MockPrismaOptions } from "./types";
|
|
3
|
-
|
|
2
|
+
import { MockPrismaOptions as MockPrismaOptionsBase } from "./types";
|
|
3
|
+
type MockPrismaOptions<P extends PrismaClient = PrismaClient> = Omit<MockPrismaOptionsBase<P, typeof Prisma>, "datamodel"> & {
|
|
4
|
+
datamodel?: Prisma.DMMF.Datamodel;
|
|
5
|
+
};
|
|
6
|
+
export default function createPrismaClient<P extends PrismaClient = PrismaClient>(options?: MockPrismaOptions<P>): P & {
|
|
4
7
|
$getInternalState: () => Required<Partial<{ [key in import("./types").IsTable<Uncapitalize<import("./types").IsString<keyof P>>>]: import("./types").PrismaList<P, key>; }>>;
|
|
5
8
|
};
|
|
9
|
+
export {};
|
package/lib/index.js
CHANGED
|
@@ -6,6 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const default_1 = require("@prisma/client/default");
|
|
7
7
|
const client_1 = __importDefault(require("./client"));
|
|
8
8
|
function createPrismaClient(options) {
|
|
9
|
-
return (0, client_1.default)(default_1.Prisma,
|
|
9
|
+
return (0, client_1.default)(default_1.Prisma, {
|
|
10
|
+
datamodel: default_1.Prisma.dmmf?.datamodel,
|
|
11
|
+
...options,
|
|
12
|
+
});
|
|
10
13
|
}
|
|
11
14
|
exports.default = createPrismaClient;
|