@pkgverse/prismock 2.0.0-beta.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/LICENSE +21 -0
- package/README.md +277 -0
- package/dist/index.cjs +5337 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +5289 -0
- package/dist/lib/client.d.ts +19 -0
- package/dist/lib/delegate.d.ts +42 -0
- package/dist/lib/extensions/index.d.ts +8 -0
- package/dist/lib/extensions/model.d.ts +5 -0
- package/dist/lib/extensions/query.d.ts +4 -0
- package/dist/lib/extensions/result.d.ts +3 -0
- package/dist/lib/helpers.d.ts +11 -0
- package/dist/lib/operations/aggregate.d.ts +11 -0
- package/dist/lib/operations/create.d.ts +34 -0
- package/dist/lib/operations/delete.d.ts +13 -0
- package/dist/lib/operations/find/find.d.ts +155 -0
- package/dist/lib/operations/find/index.d.ts +1 -0
- package/dist/lib/operations/find/match.d.ts +4 -0
- package/dist/lib/operations/groupBy/groupBy.d.ts +10 -0
- package/dist/lib/operations/groupBy/index.d.ts +1 -0
- package/dist/lib/operations/index.d.ts +6 -0
- package/dist/lib/operations/update.d.ts +14 -0
- package/dist/lib/prismock.d.ts +290 -0
- package/dist/lib/types/Aggregate.d.ts +9 -0
- package/dist/lib/types/Create.d.ts +16 -0
- package/dist/lib/types/Find.d.ts +35 -0
- package/dist/lib/types/GroupBy.d.ts +42 -0
- package/dist/lib/types/Upsert.d.ts +9 -0
- package/dist/lib/types/index.d.ts +5 -0
- package/package.json +112 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Teddy MORIN
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
# prismock
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/prismock)
|
|
4
|
+
[](https://app.circleci.com/pipelines/github/morintd/prismock)
|
|
5
|
+
[](https://www.npmjs.com/package/prismock)
|
|
6
|
+
|
|
7
|
+
This is a mock for `PrismaClient`. It actually reads your `schema.prisma` and generate models based on it.
|
|
8
|
+
|
|
9
|
+
It perfectly simulates Prisma's API and store everything in-memory for fast, isolated, and retry-able unit tests.
|
|
10
|
+
|
|
11
|
+
It's heavily tested, by comparing the mocked query results with real results from prisma. Tested environments include `MySQL`, `PostgreSQL` and `MongoDB`.
|
|
12
|
+
|
|
13
|
+
> This library can also be used as an in-memory implementation of Prisma, for reasons such as prototyping, but that's not its primary goal.
|
|
14
|
+
|
|
15
|
+
# Installation
|
|
16
|
+
|
|
17
|
+
After setting up [Prisma](https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project):
|
|
18
|
+
|
|
19
|
+
yarn
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
$ yarn add -D prismock
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
npm
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
$ npm add --save-dev prismock
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
# Usage
|
|
32
|
+
|
|
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
|
+
```ts
|
|
42
|
+
import { PrismockClient } from 'prismock';
|
|
43
|
+
|
|
44
|
+
export * from '@prisma/client';
|
|
45
|
+
export { PrismockClient as PrismaClient };
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
That's it, prisma will be mocked in all your tests (tested with Jest & ViTest)
|
|
49
|
+
|
|
50
|
+
## PrismaClient
|
|
51
|
+
|
|
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)):
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
jest.mock('@prisma/client', () => {
|
|
56
|
+
return {
|
|
57
|
+
...jest.requireActual('@prisma/client'),
|
|
58
|
+
PrismaClient: jest.requireActual('prismock').PrismockClient,
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Use prismock manually
|
|
64
|
+
|
|
65
|
+
You can instantiate a `PrismockClient` directly and use it in your test, or pass it to a test version of your app.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { PrismockClient } from 'prismock';
|
|
69
|
+
|
|
70
|
+
import { PrismaService } from './prisma.service';
|
|
71
|
+
|
|
72
|
+
const prismock = new PrismockClient();
|
|
73
|
+
const app = createApp(prismock);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Then, you will be able to write your tests as if your app was using an in-memory Prisma client.
|
|
77
|
+
|
|
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
|
+
## Use with decimal.js
|
|
83
|
+
|
|
84
|
+
See [use with decimal.js](https://github.com/morintd/prismock/blob/master/docs/use-with-decimal-js.md).
|
|
85
|
+
|
|
86
|
+
## Internal data
|
|
87
|
+
|
|
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
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const prismock = new PrismockClient();
|
|
94
|
+
prismock.getData(); // { user: [] }
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const prismock = new PrismockClient();
|
|
99
|
+
prismock.reset(); // State of prismock back to its original
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
# Supported features
|
|
103
|
+
|
|
104
|
+
## Model queries
|
|
105
|
+
|
|
106
|
+
| Feature | State |
|
|
107
|
+
| ---------- | --------------------------- |
|
|
108
|
+
| findUnique | ✔ |
|
|
109
|
+
| findFirst | ✔ |
|
|
110
|
+
| findMany | ✔ |
|
|
111
|
+
| create | ✔ |
|
|
112
|
+
| createMany | ✔ |
|
|
113
|
+
| delete | ✔ |
|
|
114
|
+
| deleteMany | ✔ |
|
|
115
|
+
| update | ✔ |
|
|
116
|
+
| updateMany | ✔ |
|
|
117
|
+
| upsert | ✔ |
|
|
118
|
+
| count | ✔ |
|
|
119
|
+
| aggregate | ✔ |
|
|
120
|
+
| groupBy | 💬 [note](#groupby-support) |
|
|
121
|
+
|
|
122
|
+
## Model query options
|
|
123
|
+
|
|
124
|
+
| Feature | State |
|
|
125
|
+
| ----------------- | ----- |
|
|
126
|
+
| distinct | ✔ |
|
|
127
|
+
| include | ✔ |
|
|
128
|
+
| where | ✔ |
|
|
129
|
+
| select | ✔ |
|
|
130
|
+
| orderBy (Partial) | ✔ |
|
|
131
|
+
| select + count | ⛔ |
|
|
132
|
+
|
|
133
|
+
## Nested queries
|
|
134
|
+
|
|
135
|
+
| Feature | State |
|
|
136
|
+
| --------------- | ----- |
|
|
137
|
+
| create | ✔ |
|
|
138
|
+
| createMany | ✔ |
|
|
139
|
+
| update | ✔ |
|
|
140
|
+
| updateMany | ✔ |
|
|
141
|
+
| connect | ✔ |
|
|
142
|
+
| connectOrCreate | ✔ |
|
|
143
|
+
| upsert | ✔ |
|
|
144
|
+
| set | ⛔ |
|
|
145
|
+
| disconnect | ⛔ |
|
|
146
|
+
| delete | ⛔ |
|
|
147
|
+
|
|
148
|
+
## Filter conditions and operators
|
|
149
|
+
|
|
150
|
+
| Feature | State |
|
|
151
|
+
| --------- | ----- |
|
|
152
|
+
| equals | ✔ |
|
|
153
|
+
| gt | ✔ |
|
|
154
|
+
| gte | ✔ |
|
|
155
|
+
| lt | ✔ |
|
|
156
|
+
| lte | ✔ |
|
|
157
|
+
| not | ✔ |
|
|
158
|
+
| in | ✔ |
|
|
159
|
+
| notIn | ✔ |
|
|
160
|
+
| contains | ✔ |
|
|
161
|
+
| startWith | ✔ |
|
|
162
|
+
| endsWith | ✔ |
|
|
163
|
+
| AND | ✔ |
|
|
164
|
+
| OR | ✔ |
|
|
165
|
+
| NOT | ✔ |
|
|
166
|
+
| mode | ✔ |
|
|
167
|
+
| search | ⛔ |
|
|
168
|
+
|
|
169
|
+
## Relation filters
|
|
170
|
+
|
|
171
|
+
| Feature | State |
|
|
172
|
+
| ------- | ----- |
|
|
173
|
+
| some | ✔ |
|
|
174
|
+
| every | ✔ |
|
|
175
|
+
| none | ✔ |
|
|
176
|
+
| is | ✔ |
|
|
177
|
+
|
|
178
|
+
## Scalar list methods
|
|
179
|
+
|
|
180
|
+
| Feature | State |
|
|
181
|
+
| ------- | ----- |
|
|
182
|
+
| set | ⛔ |
|
|
183
|
+
| push | ✔ |
|
|
184
|
+
|
|
185
|
+
## Scalar list filters
|
|
186
|
+
|
|
187
|
+
| Feature | State |
|
|
188
|
+
| -------- | ----- |
|
|
189
|
+
| has | ⛔ |
|
|
190
|
+
| hasEvery | ⛔ |
|
|
191
|
+
| hasSome | ⛔ |
|
|
192
|
+
| isEmpty | ⛔ |
|
|
193
|
+
| equals | ⛔ |
|
|
194
|
+
|
|
195
|
+
## Atomic number operations
|
|
196
|
+
|
|
197
|
+
| Feature | State |
|
|
198
|
+
| --------- | ----- |
|
|
199
|
+
| increment | ✔ |
|
|
200
|
+
| decrement | ✔ |
|
|
201
|
+
| multiply | ✔ |
|
|
202
|
+
| divide | ✔ |
|
|
203
|
+
| set | ✔ |
|
|
204
|
+
|
|
205
|
+
## JSON filters
|
|
206
|
+
|
|
207
|
+
| Feature | State |
|
|
208
|
+
| ------------------- | ----- |
|
|
209
|
+
| path | ⛔ |
|
|
210
|
+
| string_contains | ⛔ |
|
|
211
|
+
| string_starts_withn | ⛔ |
|
|
212
|
+
| string_ends_with | ⛔ |
|
|
213
|
+
| array_contains | ⛔ |
|
|
214
|
+
| array_starts_with | ⛔ |
|
|
215
|
+
| array_ends_with | ⛔ |
|
|
216
|
+
|
|
217
|
+
## Attributes
|
|
218
|
+
|
|
219
|
+
| Feature | State |
|
|
220
|
+
| ---------- | ----- |
|
|
221
|
+
| @@id | ✔ |
|
|
222
|
+
| @default | ✔ |
|
|
223
|
+
| @relation | ✔ |
|
|
224
|
+
| @unique | ⛔ |
|
|
225
|
+
| @@unique | ✔ |
|
|
226
|
+
| @updatedAt | ⛔ |
|
|
227
|
+
|
|
228
|
+
## Attribute functions
|
|
229
|
+
|
|
230
|
+
| Feature | State |
|
|
231
|
+
| --------------- | ----- |
|
|
232
|
+
| autoincrement() | ✔ |
|
|
233
|
+
| now() | ✔ |
|
|
234
|
+
| uuid() | ✔ |
|
|
235
|
+
| auto() | ✔ |
|
|
236
|
+
| cuid() | ✔ |
|
|
237
|
+
| dbgenerated | ⛔ |
|
|
238
|
+
|
|
239
|
+
## Referential actions
|
|
240
|
+
|
|
241
|
+
| Feature | State |
|
|
242
|
+
| ------------------------------------------- | ----- |
|
|
243
|
+
| onDelete (SetNull, Cascade) | ✔ |
|
|
244
|
+
| onDelete (Restrict, NoAction, SetDefault)() | ⛔ |
|
|
245
|
+
| onUpdate | ⛔ |
|
|
246
|
+
|
|
247
|
+
## Notes
|
|
248
|
+
|
|
249
|
+
### groupBy Support
|
|
250
|
+
|
|
251
|
+
Basic groupBy queries are supported, including `having` and `orderBy`. `skip`, `take`, and `cursor` are not yet supported.
|
|
252
|
+
|
|
253
|
+
# Roadmap
|
|
254
|
+
|
|
255
|
+
- Complete supported features.
|
|
256
|
+
- Refactoring of update operation.
|
|
257
|
+
- Replace item formatting with function composition
|
|
258
|
+
- Restore test on `_count` for mongodb
|
|
259
|
+
- Add custom client method for MongoDB (`$runCommandRaw`, `findRaw`, `aggregateRaw`)
|
|
260
|
+
|
|
261
|
+
# Motivation
|
|
262
|
+
|
|
263
|
+
While _Prisma_ is amazing, its `unit testing` section is treated as optional. On the other hand, it should be a priority for developers to write tests.
|
|
264
|
+
|
|
265
|
+
As I love _Prisma_, I decided to create this package, in order to keep using it on real-world projects.
|
|
266
|
+
|
|
267
|
+
I'm also a teacher and believe it's mandatory for students to learn about testing. I needed a similar solution for my [backend course](https://www.scalablebackend.com/), so I created my own.
|
|
268
|
+
|
|
269
|
+
# Feature request
|
|
270
|
+
|
|
271
|
+
I'm personally using this library in my day-to-day activities, and add features or fix bugs depending on my needs.
|
|
272
|
+
|
|
273
|
+
If you need unsupported features or discover unwanted behaviors, feel free to open an issue, I'll take care of it.
|
|
274
|
+
|
|
275
|
+
# Credit
|
|
276
|
+
|
|
277
|
+
Inspired by [prisma-mock](https://github.com/demonsters/prisma-mock).
|