prisma-mock 0.3.0 → 0.4.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 +38 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +28 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,6 +45,9 @@ createPrismaMock(
|
|
|
45
45
|
data: PrismaMockData<P> = {},
|
|
46
46
|
datamodel?: Prisma.DMMF.Datamodel,
|
|
47
47
|
client = mockDeep<P>(),
|
|
48
|
+
options: {
|
|
49
|
+
caseInsensitive?: boolean
|
|
50
|
+
} = {}
|
|
48
51
|
): Promise<P>
|
|
49
52
|
```
|
|
50
53
|
|
|
@@ -91,6 +94,7 @@ Most common cases are covered, but not everything. Here is a rough list of the s
|
|
|
91
94
|
## Model queries
|
|
92
95
|
|
|
93
96
|
- findUnique,
|
|
97
|
+
- findUniqueOrThrow,
|
|
94
98
|
- findMany,
|
|
95
99
|
- findFirst,
|
|
96
100
|
- findFirstOrThrow,
|
|
@@ -202,4 +206,37 @@ TODO (path, string_contains, string_starts_with, string_ends_with, array_contain
|
|
|
202
206
|
|
|
203
207
|
- $transaction
|
|
204
208
|
- TODO: $transaction (interactive)
|
|
205
|
-
- TODO: $transaction (isolation)
|
|
209
|
+
- TODO: $transaction (isolation)
|
|
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/index.d.ts
CHANGED
|
@@ -14,5 +14,5 @@ export type PrismaMockData<P> = Partial<{
|
|
|
14
14
|
export type MockPrismaOptions = {
|
|
15
15
|
caseInsensitive?: boolean;
|
|
16
16
|
};
|
|
17
|
-
declare const createPrismaMock: <P>(data?: {
|
|
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;
|
|
18
18
|
export default createPrismaMock;
|
package/lib/index.js
CHANGED
|
@@ -53,7 +53,7 @@ const throwUnkownError = (message, cause) => {
|
|
|
53
53
|
clientVersion);
|
|
54
54
|
}
|
|
55
55
|
error.meta = {
|
|
56
|
-
cause
|
|
56
|
+
cause,
|
|
57
57
|
};
|
|
58
58
|
throw error;
|
|
59
59
|
};
|
|
@@ -121,6 +121,11 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
121
121
|
}
|
|
122
122
|
return res;
|
|
123
123
|
});
|
|
124
|
+
client["$connect"].mockImplementation(async () => { });
|
|
125
|
+
client["$disconnect"].mockImplementation(async () => { });
|
|
126
|
+
client["$use"].mockImplementation(async () => {
|
|
127
|
+
throw new Error("$use is not yet implemented in prisma-mock");
|
|
128
|
+
});
|
|
124
129
|
// client["$connect"] = async () => { }
|
|
125
130
|
// client["$disconnect"] = async () => { }
|
|
126
131
|
// client["$use"] = async () => { }
|
|
@@ -525,7 +530,8 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
525
530
|
}
|
|
526
531
|
let match = true;
|
|
527
532
|
const matchFilter = { ...filter };
|
|
528
|
-
if (caseInsensitive ||
|
|
533
|
+
if (caseInsensitive ||
|
|
534
|
+
("mode" in matchFilter && matchFilter.mode === "insensitive")) {
|
|
529
535
|
val = val.toLowerCase ? val.toLowerCase() : val;
|
|
530
536
|
Object.keys(matchFilter).forEach((key) => {
|
|
531
537
|
const value = matchFilter[key];
|
|
@@ -610,6 +616,15 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
610
616
|
}
|
|
611
617
|
return items[0];
|
|
612
618
|
};
|
|
619
|
+
const findOrThrow = (args) => {
|
|
620
|
+
const found = findOne(args);
|
|
621
|
+
if (!found) {
|
|
622
|
+
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025",
|
|
623
|
+
// @ts-ignore
|
|
624
|
+
"1.2.3");
|
|
625
|
+
}
|
|
626
|
+
return found;
|
|
627
|
+
};
|
|
613
628
|
const findMany = (args) => {
|
|
614
629
|
let res = data[prop].filter(matchFnc(args?.where)).map(includes(args));
|
|
615
630
|
if (args?.distinct) {
|
|
@@ -745,8 +760,11 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
745
760
|
if (!schema?.relationName) {
|
|
746
761
|
return;
|
|
747
762
|
}
|
|
763
|
+
const submodel = datamodel.models.find((model) => {
|
|
764
|
+
return model.name === schema.type;
|
|
765
|
+
});
|
|
748
766
|
// Get delegate for relation
|
|
749
|
-
const delegate = Delegate(getCamelCase(schema.type),
|
|
767
|
+
const delegate = Delegate(getCamelCase(schema.type), submodel);
|
|
750
768
|
// Construct arg for relation query
|
|
751
769
|
let subArgs = obj[key] === true ? {} : obj[key];
|
|
752
770
|
subArgs = {
|
|
@@ -792,20 +810,18 @@ const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel,
|
|
|
792
810
|
data = removeMultiFieldIds(model, data);
|
|
793
811
|
return findOne({ ...args, where: updatedItem });
|
|
794
812
|
};
|
|
813
|
+
const notImplemented = (name) => () => {
|
|
814
|
+
throw new Error(`${name} is not yet implemented in prisma-mock`);
|
|
815
|
+
};
|
|
795
816
|
return {
|
|
817
|
+
aggregate: notImplemented("aggregate"),
|
|
818
|
+
groupBy: notImplemented("groupBy"),
|
|
796
819
|
findOne,
|
|
797
820
|
findUnique: findOne,
|
|
821
|
+
findUniqueOrThrow: findOrThrow,
|
|
798
822
|
findMany,
|
|
799
823
|
findFirst: findOne,
|
|
800
|
-
findFirstOrThrow:
|
|
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
|
-
},
|
|
824
|
+
findFirstOrThrow: findOrThrow,
|
|
809
825
|
create,
|
|
810
826
|
createMany: (args) => {
|
|
811
827
|
if (!Array.isArray(args.data)) {
|