prisma-mock 0.2.1 → 0.3.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/README.md +71 -21
- package/lib/defaults/cuid.d.ts +1 -0
- package/lib/defaults/cuid.js +6 -1
- package/lib/defaults/index.js +2 -1
- package/lib/index.d.ts +4 -3
- package/lib/index.js +86 -34
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -13,29 +13,29 @@ import createPrismaMock from "prisma-mock"
|
|
|
13
13
|
|
|
14
14
|
let client
|
|
15
15
|
|
|
16
|
-
beforeEach(
|
|
17
|
-
client =
|
|
18
|
-
}
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
client = createPrismaMock()
|
|
18
|
+
})
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
An example how to mock a global prisma instance
|
|
21
|
+
An example how to mock a global prisma instance, as the default export in a "db" directory (like blitzjs):
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
|
-
import createPrismaMock from "prisma-mock"
|
|
25
|
-
import { mockDeep, mockReset } from "jest-mock-extended"
|
|
24
|
+
import createPrismaMock from "prisma-mock"
|
|
25
|
+
import { mockDeep, mockReset } from "jest-mock-extended"
|
|
26
26
|
|
|
27
27
|
jest.mock("db", () => ({
|
|
28
28
|
__esModule: true,
|
|
29
29
|
...jest.requireActual("db"),
|
|
30
30
|
default: mockDeep(),
|
|
31
|
-
}))
|
|
31
|
+
}))
|
|
32
32
|
|
|
33
|
-
import db, { Prisma } from "db"
|
|
33
|
+
import db, { Prisma } from "db"
|
|
34
34
|
|
|
35
35
|
beforeEach(() => {
|
|
36
|
-
mockReset(db)
|
|
37
|
-
|
|
38
|
-
})
|
|
36
|
+
mockReset(db)
|
|
37
|
+
createPrismaMock({}, Prisma.dmmf.datamodel)
|
|
38
|
+
})
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
# API
|
|
@@ -44,17 +44,20 @@ beforeEach(() => {
|
|
|
44
44
|
createPrismaMock(
|
|
45
45
|
data: PrismaMockData<P> = {},
|
|
46
46
|
datamodel?: Prisma.DMMF.Datamodel,
|
|
47
|
-
client = mockDeep<P>()
|
|
47
|
+
client = mockDeep<P>(),
|
|
48
|
+
options: {
|
|
49
|
+
caseInsensitive?: boolean
|
|
50
|
+
} = {}
|
|
48
51
|
): Promise<P>
|
|
49
52
|
```
|
|
50
53
|
|
|
51
|
-
|
|
54
|
+
#### Arg: `data`
|
|
52
55
|
|
|
53
|
-
|
|
56
|
+
You can optionally start up a pre-filled db, by passing in an object containing keys for tables, and values as arrays of objects (though using `create` is preferred). Example:
|
|
54
57
|
|
|
55
58
|
```js
|
|
56
59
|
createPrismaMock({
|
|
57
|
-
|
|
60
|
+
user: [
|
|
58
61
|
{
|
|
59
62
|
id: 1,
|
|
60
63
|
name: "John Doe",
|
|
@@ -67,26 +70,33 @@ createPrismaMock({
|
|
|
67
70
|
name: "Company",
|
|
68
71
|
},
|
|
69
72
|
],
|
|
70
|
-
})
|
|
73
|
+
})
|
|
71
74
|
```
|
|
72
75
|
|
|
73
|
-
|
|
76
|
+
#### Arg: `datamodel`
|
|
74
77
|
|
|
75
78
|
The datamodel of the prisma client, value of `Prisma.dmmf.datamodel`.
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
#### Arg: `client`
|
|
81
|
+
|
|
82
|
+
A `jest-mock-extended` instance. If not provided, a new instance is created.
|
|
83
|
+
|
|
84
|
+
#### Arg: `caseInsensitive`
|
|
85
|
+
|
|
86
|
+
If true, all string comparisons are case insensitive.
|
|
87
|
+
|
|
78
88
|
|
|
79
|
-
`jest-mock-extended` instance used. If not provided, a new instance is created.
|
|
80
89
|
|
|
81
90
|
# Supported features
|
|
82
91
|
|
|
83
|
-
|
|
92
|
+
Most common cases are covered, but not everything. Here is a rough list of the supported features:
|
|
84
93
|
|
|
85
94
|
## Model queries
|
|
86
95
|
|
|
87
96
|
- findUnique,
|
|
88
97
|
- findMany,
|
|
89
98
|
- findFirst,
|
|
99
|
+
- findFirstOrThrow,
|
|
90
100
|
- create,
|
|
91
101
|
- createMany
|
|
92
102
|
- delete,
|
|
@@ -137,8 +147,8 @@ Alot of the functionality is implemented, but parts are missing. Here is a list
|
|
|
137
147
|
- AND
|
|
138
148
|
- OR
|
|
139
149
|
- NOT
|
|
150
|
+
- mode
|
|
140
151
|
- TODO: search
|
|
141
|
-
- TODO: mode
|
|
142
152
|
|
|
143
153
|
## Relation filters
|
|
144
154
|
|
|
@@ -190,3 +200,43 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
|
|
|
190
200
|
- onDelete (SetNull, Cascade)
|
|
191
201
|
- TODO: onDelete: Restrict, NoAction, SetDefault
|
|
192
202
|
- TODO: onUpdate
|
|
203
|
+
|
|
204
|
+
## Prisma Client methods
|
|
205
|
+
|
|
206
|
+
- $transaction
|
|
207
|
+
- TODO: $transaction (interactive)
|
|
208
|
+
- TODO: $transaction (isolation)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
# Contribution
|
|
212
|
+
|
|
213
|
+
## Requirements
|
|
214
|
+
|
|
215
|
+
Create a `.env-cmdrc` file in the root of your project with the following content:
|
|
216
|
+
|
|
217
|
+
```json
|
|
218
|
+
{
|
|
219
|
+
"postgres": {
|
|
220
|
+
"PROVIDER": "postgresql",
|
|
221
|
+
"DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/postgres?schema=public"
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Writing Tests
|
|
227
|
+
Create your tests in the `__tests__` directory. You can use snapshot testing with either `expect(res).toMatchSnapshot()` or `expect(res).toMatchInlineSnapshot()`. This captures the result of your tests in a snapshot, which you can use to compare agains prisma-mock results.
|
|
228
|
+
|
|
229
|
+
Note: If you choose to use snapshot testing, make shore to first run your tests against the real database to create a snapshot of the expected result.
|
|
230
|
+
|
|
231
|
+
## Running Tests
|
|
232
|
+
To run tests against a postgres database, run the following command:
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
yarn run test:postgres
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
To run tests against prisma-mock (in-memory database), run:
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
yarn test
|
|
242
|
+
```
|
package/lib/defaults/cuid.d.ts
CHANGED
package/lib/defaults/cuid.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResetCuid = void 0;
|
|
3
4
|
let ciud_cache = 0;
|
|
4
5
|
function pad(s, size) {
|
|
5
6
|
while (s.length < (size || 2)) {
|
|
@@ -10,6 +11,10 @@ function pad(s, size) {
|
|
|
10
11
|
// Format from: https://cuid.marcoonroad.dev/
|
|
11
12
|
const Cuid = () => {
|
|
12
13
|
ciud_cache++;
|
|
13
|
-
return `c00p6qup2${pad(String(ciud_cache), 4)}
|
|
14
|
+
return `c00p6qup2${pad(String(ciud_cache), 4)}ckkzslahp5pn`;
|
|
14
15
|
};
|
|
16
|
+
function ResetCuid() {
|
|
17
|
+
ciud_cache = 0;
|
|
18
|
+
}
|
|
19
|
+
exports.ResetCuid = ResetCuid;
|
|
15
20
|
exports.default = Cuid;
|
package/lib/defaults/index.js
CHANGED
|
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.ResetDefaults = void 0;
|
|
30
30
|
const autoincrement_1 = __importStar(require("./autoincrement"));
|
|
31
|
-
const cuid_1 =
|
|
31
|
+
const cuid_1 = __importStar(require("./cuid"));
|
|
32
32
|
const now_1 = __importDefault(require("./now"));
|
|
33
33
|
// const registry = new Map<string, (string, Prisma.DMMF.Field, PrismaMockData) => any>();
|
|
34
34
|
const registry = new Map();
|
|
@@ -43,5 +43,6 @@ function HandleDefault(prop, field, data) {
|
|
|
43
43
|
exports.default = HandleDefault;
|
|
44
44
|
function ResetDefaults() {
|
|
45
45
|
(0, autoincrement_1.reset)();
|
|
46
|
+
(0, cuid_1.ResetCuid)();
|
|
46
47
|
}
|
|
47
48
|
exports.ResetDefaults = ResetDefaults;
|
package/lib/index.d.ts
CHANGED
|
@@ -11,7 +11,8 @@ type PrismaList<P extends {
|
|
|
11
11
|
export type PrismaMockData<P> = Partial<{
|
|
12
12
|
[key in IsTable<Uncapitalize<IsString<keyof P>>>]: PrismaList<P, key>;
|
|
13
13
|
}>;
|
|
14
|
-
|
|
15
|
-
caseInsensitive
|
|
16
|
-
}
|
|
14
|
+
export type MockPrismaOptions = {
|
|
15
|
+
caseInsensitive?: boolean;
|
|
16
|
+
};
|
|
17
|
+
declare const createPrismaMock: <P>(data?: Partial<{ [key in IsTable<Uncapitalize<IsString<keyof P>>>]: PrismaList<P, key>; }>, datamodel?: Prisma.DMMF.Datamodel, client?: { [K in keyof P]: P[K] extends (...args: infer A) => infer B ? import("jest-mock-extended").CalledWithMock<B, A> & (P[K] extends infer T extends (...args: infer A) => infer B ? { [K_1 in keyof T]: P[K][K_1] extends (...args: infer A_1) => infer B_1 ? import("jest-mock-extended").CalledWithMock<B_1, A_1> & (P[K][K_1] extends infer T_1 extends (...args: infer A_1) => infer B_1 ? { [K_2 in keyof T_1]: P[K][K_1][K_2] extends (...args: infer A_2) => infer B_2 ? import("jest-mock-extended").CalledWithMock<B_2, A_2> & (P[K][K_1][K_2] extends infer T_2 extends (...args: infer A_2) => infer B_2 ? { [K_3 in keyof T_2]: P[K][K_1][K_2][K_3] extends (...args: infer A_3) => infer B_3 ? import("jest-mock-extended").CalledWithMock<B_3, A_3> & (P[K][K_1][K_2][K_3] extends infer T_3 extends (...args: infer A_3) => infer B_3 ? { [K_4 in keyof T_3]: P[K][K_1][K_2][K_3][K_4] extends (...args: infer A_4) => infer B_4 ? import("jest-mock-extended").CalledWithMock<B_4, A_4> & (P[K][K_1][K_2][K_3][K_4] extends infer T_4 extends (...args: infer A_4) => infer B_4 ? { [K_5 in keyof T_4]: P[K][K_1][K_2][K_3][K_4][K_5] extends (...args: infer A_5) => infer B_5 ? import("jest-mock-extended").CalledWithMock<B_5, A_5> & (P[K][K_1][K_2][K_3][K_4][K_5] extends infer T_5 extends (...args: infer A_5) => infer B_5 ? { [K_6 in keyof T_5]: P[K][K_1][K_2][K_3][K_4][K_5][K_6] extends (...args: infer A_6) => infer B_6 ? import("jest-mock-extended").CalledWithMock<B_6, A_6> & (P[K][K_1][K_2][K_3][K_4][K_5][K_6] extends infer T_6 extends (...args: infer A_6) => infer B_6 ? { [K_7 in keyof T_6]: P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends (...args: infer A_7) => infer B_7 ? import("jest-mock-extended").CalledWithMock<B_7, A_7> & (P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] extends infer T_7 extends (...args: infer A_7) => infer B_7 ? { [K_8 in keyof T_7]: P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends (...args: infer A_8) => infer B_8 ? import("jest-mock-extended").CalledWithMock<B_8, A_8> & (P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] extends infer T_8 extends (...args: infer A_8) => infer B_8 ? { [K_9 in keyof T_8]: P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends (...args: infer A_9) => infer B_9 ? import("jest-mock-extended").CalledWithMock<B_9, A_9> & (P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] extends infer T_9 extends (...args: infer A_9) => infer B_9 ? { [K_10 in keyof T_9]: P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10] extends (...args: infer A_10) => infer B_10 ? any : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9][K_10]>; } : never) & P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8][K_9]>; } : never) & P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7][K_8]>; } : never) & P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5][K_6][K_7]>; } : never) & P[K][K_1][K_2][K_3][K_4][K_5][K_6] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5][K_6]>; } : never) & P[K][K_1][K_2][K_3][K_4][K_5] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4][K_5]>; } : never) & P[K][K_1][K_2][K_3][K_4] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3][K_4]>; } : never) & P[K][K_1][K_2][K_3] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2][K_3]>; } : never) & P[K][K_1][K_2] : import("jest-mock-extended").DeepMockProxy<P[K][K_1][K_2]>; } : never) & P[K][K_1] : import("jest-mock-extended").DeepMockProxy<P[K][K_1]>; } : never) & P[K] : import("jest-mock-extended").DeepMockProxy<P[K]>; } & P, options?: MockPrismaOptions) => P;
|
|
17
18
|
export default createPrismaMock;
|
package/lib/index.js
CHANGED
|
@@ -32,10 +32,38 @@ const deepEqual_1 = require("./utils/deepEqual");
|
|
|
32
32
|
function IsFieldDefault(f) {
|
|
33
33
|
return f.name !== undefined;
|
|
34
34
|
}
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
const throwUnkownError = (message, cause) => {
|
|
36
|
+
const code = "P2025";
|
|
37
|
+
const clientVersion = "1.2.3";
|
|
38
|
+
// PrismaClientKnownRequestError prototype changed in version 4.7.0
|
|
39
|
+
// from: constructor(message: string, code: string, clientVersion: string, meta?: any)
|
|
40
|
+
// to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams)
|
|
41
|
+
let error;
|
|
42
|
+
if (runtime_1.PrismaClientKnownRequestError.length === 2) {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
error = new runtime_1.PrismaClientKnownRequestError(message, {
|
|
45
|
+
code,
|
|
46
|
+
clientVersion,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
error = new runtime_1.PrismaClientKnownRequestError(message, code,
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
clientVersion);
|
|
38
54
|
}
|
|
55
|
+
error.meta = {
|
|
56
|
+
cause
|
|
57
|
+
};
|
|
58
|
+
throw error;
|
|
59
|
+
};
|
|
60
|
+
const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = {
|
|
61
|
+
caseInsensitive: false,
|
|
62
|
+
}) => {
|
|
63
|
+
// let data = options.data || {}
|
|
64
|
+
// const datamodel = options.datamodel || Prisma.dmmf.datamodel
|
|
65
|
+
const caseInsensitive = options.caseInsensitive || false;
|
|
66
|
+
// let client = {} as P
|
|
39
67
|
(0, defaults_1.ResetDefaults)();
|
|
40
68
|
const getCamelCase = (name) => {
|
|
41
69
|
return name.substr(0, 1).toLowerCase() + name.substr(1);
|
|
@@ -86,12 +114,16 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
86
114
|
});
|
|
87
115
|
return joinfield;
|
|
88
116
|
};
|
|
89
|
-
// @ts-ignore
|
|
90
117
|
client["$transaction"].mockImplementation(async (actions) => {
|
|
118
|
+
const res = [];
|
|
91
119
|
for (const action of actions) {
|
|
92
|
-
await action;
|
|
120
|
+
res.push(await action);
|
|
93
121
|
}
|
|
122
|
+
return res;
|
|
94
123
|
});
|
|
124
|
+
// client["$connect"] = async () => { }
|
|
125
|
+
// client["$disconnect"] = async () => { }
|
|
126
|
+
// client["$use"] = async () => { }
|
|
95
127
|
const Delegate = (prop, model) => {
|
|
96
128
|
const sortFunc = (orderBy) => (a, b) => {
|
|
97
129
|
if (Array.isArray(orderBy)) {
|
|
@@ -149,6 +181,11 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
149
181
|
};
|
|
150
182
|
const nestedUpdate = (args, isCreating, item) => {
|
|
151
183
|
let d = args.data;
|
|
184
|
+
Object.entries(d).forEach(([key, value]) => {
|
|
185
|
+
if (typeof value === "undefined") {
|
|
186
|
+
delete d[key];
|
|
187
|
+
}
|
|
188
|
+
});
|
|
152
189
|
// Get field schema for default values
|
|
153
190
|
const model = datamodel.models.find((model) => {
|
|
154
191
|
return getCamelCase(model.name) === prop;
|
|
@@ -172,23 +209,7 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
172
209
|
return row[keyToMatch] === valueToMatch;
|
|
173
210
|
});
|
|
174
211
|
if (!matchingRow) {
|
|
175
|
-
|
|
176
|
-
const code = "P2025";
|
|
177
|
-
const clientVersion = "1.2.3";
|
|
178
|
-
// PrismaClientKnownRequestError prototype changed in version 4.7.0
|
|
179
|
-
// from: constructor(message: string, code: string, clientVersion: string, meta?: any)
|
|
180
|
-
// to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams)
|
|
181
|
-
if (runtime_1.PrismaClientKnownRequestError.length === 2) {
|
|
182
|
-
// @ts-ignore
|
|
183
|
-
throw new runtime_1.PrismaClientKnownRequestError(message, {
|
|
184
|
-
code,
|
|
185
|
-
clientVersion,
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
// @ts-ignore
|
|
189
|
-
throw new runtime_1.PrismaClientKnownRequestError(message, code,
|
|
190
|
-
// @ts-ignore
|
|
191
|
-
clientVersion);
|
|
212
|
+
throwUnkownError("An operation failed because it depends on one or more records that were required but not found. {cause}");
|
|
192
213
|
}
|
|
193
214
|
connectionValue = matchingRow[keyToGet];
|
|
194
215
|
}
|
|
@@ -362,9 +383,10 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
362
383
|
};
|
|
363
384
|
}
|
|
364
385
|
if (c.divide) {
|
|
386
|
+
const newValue = item[field.name] / c.divide;
|
|
365
387
|
d = {
|
|
366
388
|
...d,
|
|
367
|
-
[field.name]:
|
|
389
|
+
[field.name]: field.type === "Int" ? Math.floor(newValue) : newValue,
|
|
368
390
|
};
|
|
369
391
|
}
|
|
370
392
|
if (c.set) {
|
|
@@ -468,7 +490,7 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
468
490
|
});
|
|
469
491
|
if (filter.every) {
|
|
470
492
|
if (res.length === 0)
|
|
471
|
-
return
|
|
493
|
+
return true;
|
|
472
494
|
// const all = data[childName].filter(
|
|
473
495
|
// matchFnc(getFieldRelationshipWhere(item, info)),
|
|
474
496
|
// )
|
|
@@ -503,7 +525,7 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
503
525
|
}
|
|
504
526
|
let match = true;
|
|
505
527
|
const matchFilter = { ...filter };
|
|
506
|
-
if (
|
|
528
|
+
if (caseInsensitive || ("mode" in matchFilter && matchFilter.mode === "insensitive")) {
|
|
507
529
|
val = val.toLowerCase ? val.toLowerCase() : val;
|
|
508
530
|
Object.keys(matchFilter).forEach((key) => {
|
|
509
531
|
const value = matchFilter[key];
|
|
@@ -693,11 +715,14 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
693
715
|
});
|
|
694
716
|
}
|
|
695
717
|
else if (joinfield.relationOnDelete === "Cascade") {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
718
|
+
try {
|
|
719
|
+
delegate.delete({
|
|
720
|
+
where: {
|
|
721
|
+
[joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]],
|
|
722
|
+
},
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
catch (e) { }
|
|
701
726
|
}
|
|
702
727
|
});
|
|
703
728
|
});
|
|
@@ -720,8 +745,11 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
720
745
|
if (!schema?.relationName) {
|
|
721
746
|
return;
|
|
722
747
|
}
|
|
748
|
+
const submodel = datamodel.models.find((model) => {
|
|
749
|
+
return model.name === schema.type;
|
|
750
|
+
});
|
|
723
751
|
// Get delegate for relation
|
|
724
|
-
const delegate = Delegate(getCamelCase(schema.type),
|
|
752
|
+
const delegate = Delegate(getCamelCase(schema.type), submodel);
|
|
725
753
|
// Construct arg for relation query
|
|
726
754
|
let subArgs = obj[key] === true ? {} : obj[key];
|
|
727
755
|
subArgs = {
|
|
@@ -772,17 +800,38 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
772
800
|
findUnique: findOne,
|
|
773
801
|
findMany,
|
|
774
802
|
findFirst: findOne,
|
|
803
|
+
findFirstOrThrow: (args) => {
|
|
804
|
+
const found = findOne(args);
|
|
805
|
+
if (!found) {
|
|
806
|
+
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
807
|
+
// @ts-ignore
|
|
808
|
+
"1.2.3");
|
|
809
|
+
}
|
|
810
|
+
return found;
|
|
811
|
+
},
|
|
775
812
|
create,
|
|
776
813
|
createMany: (args) => {
|
|
777
|
-
args.data
|
|
814
|
+
if (!Array.isArray(args.data)) {
|
|
778
815
|
create({
|
|
779
816
|
...args,
|
|
780
|
-
data,
|
|
817
|
+
data: args.data,
|
|
781
818
|
});
|
|
782
|
-
}
|
|
819
|
+
}
|
|
820
|
+
else {
|
|
821
|
+
args.data.forEach((data) => {
|
|
822
|
+
create({
|
|
823
|
+
...args,
|
|
824
|
+
data,
|
|
825
|
+
});
|
|
826
|
+
});
|
|
827
|
+
}
|
|
783
828
|
return findMany(args);
|
|
784
829
|
},
|
|
785
830
|
delete: (args) => {
|
|
831
|
+
const item = findOne(args);
|
|
832
|
+
if (!item) {
|
|
833
|
+
throwUnkownError("An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.", "Record to delete does not exist.");
|
|
834
|
+
}
|
|
786
835
|
const deleted = deleteMany(args);
|
|
787
836
|
if (deleted.length) {
|
|
788
837
|
return deleted[0];
|
|
@@ -836,11 +885,14 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
836
885
|
Object.keys(objs).forEach((fncName) => {
|
|
837
886
|
if (fncName.indexOf("_") === 0)
|
|
838
887
|
return;
|
|
888
|
+
if (!client[c])
|
|
889
|
+
client[c] = {};
|
|
839
890
|
client[c][fncName].mockImplementation(async (...params) => {
|
|
840
891
|
return objs[fncName](...params);
|
|
841
892
|
});
|
|
842
893
|
});
|
|
843
894
|
});
|
|
895
|
+
// @ts-ignore
|
|
844
896
|
return client;
|
|
845
897
|
};
|
|
846
898
|
exports.default = createPrismaMock;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prisma-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Mock prisma for unit testing database",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": "https://github.com/demonsters/prisma-mock",
|
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@prisma/client": "4.7.1",
|
|
14
14
|
"@types/jest": "^27.0.2",
|
|
15
|
+
"cross-spawn": "^7.0.3",
|
|
16
|
+
"env-cmd": "^10.1.0",
|
|
15
17
|
"jest": "^27.3.1",
|
|
16
18
|
"jest-mock-extended": "^2.0.4",
|
|
17
19
|
"prisma": "4.7.1",
|
|
@@ -22,7 +24,8 @@
|
|
|
22
24
|
"preversion": "tsc",
|
|
23
25
|
"build": "tsc",
|
|
24
26
|
"test": "jest",
|
|
25
|
-
"watch": "tsc --watch"
|
|
27
|
+
"watch": "tsc --watch",
|
|
28
|
+
"test:postgres": "env-cmd -e postgres jest --maxWorkers=1"
|
|
26
29
|
},
|
|
27
30
|
"peerDependencies": {
|
|
28
31
|
"@prisma/client": "^3.5.0 || ^4.7.0"
|