prisma-mock 0.2.0 → 0.3.0
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 +34 -21
- package/lib/defaults/cuid.d.ts +1 -0
- package/lib/defaults/cuid.js +17 -2
- package/lib/defaults/index.js +2 -1
- package/lib/index.d.ts +4 -3
- package/lib/index.js +82 -33
- package/package.json +6 -5
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,17 @@ beforeEach(() => {
|
|
|
44
44
|
createPrismaMock(
|
|
45
45
|
data: PrismaMockData<P> = {},
|
|
46
46
|
datamodel?: Prisma.DMMF.Datamodel,
|
|
47
|
-
client = mockDeep<P>()
|
|
47
|
+
client = mockDeep<P>(),
|
|
48
48
|
): Promise<P>
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
#### Arg: `data`
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
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
54
|
|
|
55
55
|
```js
|
|
56
56
|
createPrismaMock({
|
|
57
|
-
|
|
57
|
+
user: [
|
|
58
58
|
{
|
|
59
59
|
id: 1,
|
|
60
60
|
name: "John Doe",
|
|
@@ -67,26 +67,33 @@ createPrismaMock({
|
|
|
67
67
|
name: "Company",
|
|
68
68
|
},
|
|
69
69
|
],
|
|
70
|
-
})
|
|
70
|
+
})
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
#### Arg: `datamodel`
|
|
74
74
|
|
|
75
75
|
The datamodel of the prisma client, value of `Prisma.dmmf.datamodel`.
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
#### Arg: `client`
|
|
78
|
+
|
|
79
|
+
A `jest-mock-extended` instance. If not provided, a new instance is created.
|
|
80
|
+
|
|
81
|
+
#### Arg: `caseInsensitive`
|
|
82
|
+
|
|
83
|
+
If true, all string comparisons are case insensitive.
|
|
84
|
+
|
|
78
85
|
|
|
79
|
-
`jest-mock-extended` instance used. If not provided, a new instance is created.
|
|
80
86
|
|
|
81
87
|
# Supported features
|
|
82
88
|
|
|
83
|
-
|
|
89
|
+
Most common cases are covered, but not everything. Here is a rough list of the supported features:
|
|
84
90
|
|
|
85
91
|
## Model queries
|
|
86
92
|
|
|
87
93
|
- findUnique,
|
|
88
94
|
- findMany,
|
|
89
95
|
- findFirst,
|
|
96
|
+
- findFirstOrThrow,
|
|
90
97
|
- create,
|
|
91
98
|
- createMany
|
|
92
99
|
- delete,
|
|
@@ -137,8 +144,8 @@ Alot of the functionality is implemented, but parts are missing. Here is a list
|
|
|
137
144
|
- AND
|
|
138
145
|
- OR
|
|
139
146
|
- NOT
|
|
147
|
+
- mode
|
|
140
148
|
- TODO: search
|
|
141
|
-
- TODO: mode
|
|
142
149
|
|
|
143
150
|
## Relation filters
|
|
144
151
|
|
|
@@ -190,3 +197,9 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
|
|
|
190
197
|
- onDelete (SetNull, Cascade)
|
|
191
198
|
- TODO: onDelete: Restrict, NoAction, SetDefault
|
|
192
199
|
- TODO: onUpdate
|
|
200
|
+
|
|
201
|
+
## Prisma Client methods
|
|
202
|
+
|
|
203
|
+
- $transaction
|
|
204
|
+
- TODO: $transaction (interactive)
|
|
205
|
+
- TODO: $transaction (isolation)
|
package/lib/defaults/cuid.d.ts
CHANGED
package/lib/defaults/cuid.js
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
exports.ResetCuid = void 0;
|
|
4
|
+
let ciud_cache = 0;
|
|
5
|
+
function pad(s, size) {
|
|
6
|
+
while (s.length < (size || 2)) {
|
|
7
|
+
s = "0" + s;
|
|
8
|
+
}
|
|
9
|
+
return s;
|
|
10
|
+
}
|
|
11
|
+
// Format from: https://cuid.marcoonroad.dev/
|
|
12
|
+
const Cuid = () => {
|
|
13
|
+
ciud_cache++;
|
|
14
|
+
return `c00p6qup2${pad(String(ciud_cache), 4)}ckkzslahp5pn`;
|
|
15
|
+
};
|
|
16
|
+
function ResetCuid() {
|
|
17
|
+
ciud_cache = 0;
|
|
18
|
+
}
|
|
19
|
+
exports.ResetCuid = ResetCuid;
|
|
5
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?: {}, 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
|
});
|
|
@@ -772,17 +797,38 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
772
797
|
findUnique: findOne,
|
|
773
798
|
findMany,
|
|
774
799
|
findFirst: findOne,
|
|
800
|
+
findFirstOrThrow: (args) => {
|
|
801
|
+
const found = findOne(args);
|
|
802
|
+
if (!found) {
|
|
803
|
+
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
804
|
+
// @ts-ignore
|
|
805
|
+
"1.2.3");
|
|
806
|
+
}
|
|
807
|
+
return found;
|
|
808
|
+
},
|
|
775
809
|
create,
|
|
776
810
|
createMany: (args) => {
|
|
777
|
-
args.data
|
|
811
|
+
if (!Array.isArray(args.data)) {
|
|
778
812
|
create({
|
|
779
813
|
...args,
|
|
780
|
-
data,
|
|
814
|
+
data: args.data,
|
|
781
815
|
});
|
|
782
|
-
}
|
|
816
|
+
}
|
|
817
|
+
else {
|
|
818
|
+
args.data.forEach((data) => {
|
|
819
|
+
create({
|
|
820
|
+
...args,
|
|
821
|
+
data,
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
}
|
|
783
825
|
return findMany(args);
|
|
784
826
|
},
|
|
785
827
|
delete: (args) => {
|
|
828
|
+
const item = findOne(args);
|
|
829
|
+
if (!item) {
|
|
830
|
+
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.");
|
|
831
|
+
}
|
|
786
832
|
const deleted = deleteMany(args);
|
|
787
833
|
if (deleted.length) {
|
|
788
834
|
return deleted[0];
|
|
@@ -836,11 +882,14 @@ const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_
|
|
|
836
882
|
Object.keys(objs).forEach((fncName) => {
|
|
837
883
|
if (fncName.indexOf("_") === 0)
|
|
838
884
|
return;
|
|
885
|
+
if (!client[c])
|
|
886
|
+
client[c] = {};
|
|
839
887
|
client[c][fncName].mockImplementation(async (...params) => {
|
|
840
888
|
return objs[fncName](...params);
|
|
841
889
|
});
|
|
842
890
|
});
|
|
843
891
|
});
|
|
892
|
+
// @ts-ignore
|
|
844
893
|
return client;
|
|
845
894
|
};
|
|
846
895
|
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.0",
|
|
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,12 +24,11 @@
|
|
|
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"
|
|
29
32
|
},
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"@paralleldrive/cuid2": "^2.0.0"
|
|
32
|
-
}
|
|
33
|
+
"dependencies": {}
|
|
33
34
|
}
|