prisma-mock
Advanced tools
Comparing version 0.2.1 to 0.3.0
declare const Cuid: () => string; | ||
export declare function ResetCuid(): void; | ||
export default Cuid; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ResetCuid = void 0; | ||
let ciud_cache = 0; | ||
@@ -13,4 +14,8 @@ function pad(s, size) { | ||
ciud_cache++; | ||
return `c00p6qup2${pad(String(ciud_cache), 4)}ckkzslahp5p`; | ||
return `c00p6qup2${pad(String(ciud_cache), 4)}ckkzslahp5pn`; | ||
}; | ||
function ResetCuid() { | ||
ciud_cache = 0; | ||
} | ||
exports.ResetCuid = ResetCuid; | ||
exports.default = Cuid; |
@@ -31,3 +31,3 @@ "use strict"; | ||
const autoincrement_1 = __importStar(require("./autoincrement")); | ||
const cuid_1 = __importDefault(require("./cuid")); | ||
const cuid_1 = __importStar(require("./cuid")); | ||
const now_1 = __importDefault(require("./now")); | ||
@@ -47,3 +47,4 @@ // const registry = new Map<string, (string, Prisma.DMMF.Field, PrismaMockData) => any>(); | ||
(0, autoincrement_1.reset)(); | ||
(0, cuid_1.ResetCuid)(); | ||
} | ||
exports.ResetDefaults = ResetDefaults; |
@@ -14,5 +14,6 @@ import { Prisma } from "@prisma/client"; | ||
}>; | ||
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?: { | ||
caseInsensitive: boolean; | ||
}) => P; | ||
export type MockPrismaOptions = { | ||
caseInsensitive?: boolean; | ||
}; | ||
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; | ||
export default createPrismaMock; |
115
lib/index.js
@@ -35,6 +35,34 @@ "use strict"; | ||
} | ||
const createPrismaMock = (data = {}, datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = { caseInsensitive: false }) => { | ||
if (!datamodel || typeof datamodel === "string") { | ||
datamodel = client_1.Prisma.dmmf.datamodel; | ||
const throwUnkownError = (message, cause) => { | ||
const code = "P2025"; | ||
const clientVersion = "1.2.3"; | ||
// PrismaClientKnownRequestError prototype changed in version 4.7.0 | ||
// from: constructor(message: string, code: string, clientVersion: string, meta?: any) | ||
// to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams) | ||
let error; | ||
if (runtime_1.PrismaClientKnownRequestError.length === 2) { | ||
// @ts-ignore | ||
error = new runtime_1.PrismaClientKnownRequestError(message, { | ||
code, | ||
clientVersion, | ||
}); | ||
} | ||
else { | ||
// @ts-ignore | ||
error = new runtime_1.PrismaClientKnownRequestError(message, code, | ||
// @ts-ignore | ||
clientVersion); | ||
} | ||
error.meta = { | ||
cause | ||
}; | ||
throw error; | ||
}; | ||
const createPrismaMock = (data = {}, datamodel = client_1.Prisma.dmmf.datamodel, client = (0, jest_mock_extended_1.mockDeep)(), options = { | ||
caseInsensitive: false, | ||
}) => { | ||
// let data = options.data || {} | ||
// const datamodel = options.datamodel || Prisma.dmmf.datamodel | ||
const caseInsensitive = options.caseInsensitive || false; | ||
// let client = {} as P | ||
(0, defaults_1.ResetDefaults)(); | ||
@@ -90,8 +118,12 @@ const getCamelCase = (name) => { | ||
}; | ||
// @ts-ignore | ||
client["$transaction"].mockImplementation(async (actions) => { | ||
const res = []; | ||
for (const action of actions) { | ||
await action; | ||
res.push(await action); | ||
} | ||
return res; | ||
}); | ||
// client["$connect"] = async () => { } | ||
// client["$disconnect"] = async () => { } | ||
// client["$use"] = async () => { } | ||
const Delegate = (prop, model) => { | ||
@@ -154,2 +186,7 @@ const sortFunc = (orderBy) => (a, b) => { | ||
let d = args.data; | ||
Object.entries(d).forEach(([key, value]) => { | ||
if (typeof value === "undefined") { | ||
delete d[key]; | ||
} | ||
}); | ||
// Get field schema for default values | ||
@@ -178,19 +215,3 @@ const model = datamodel.models.find((model) => { | ||
if (!matchingRow) { | ||
const message = "An operation failed because it depends on one or more records that were required but not found. {cause}"; | ||
const code = "P2025"; | ||
const clientVersion = "1.2.3"; | ||
// PrismaClientKnownRequestError prototype changed in version 4.7.0 | ||
// from: constructor(message: string, code: string, clientVersion: string, meta?: any) | ||
// to: constructor(message: string, { code, clientVersion, meta, batchRequestIdx }: KnownErrorParams) | ||
if (runtime_1.PrismaClientKnownRequestError.length === 2) { | ||
// @ts-ignore | ||
throw new runtime_1.PrismaClientKnownRequestError(message, { | ||
code, | ||
clientVersion, | ||
}); | ||
} | ||
// @ts-ignore | ||
throw new runtime_1.PrismaClientKnownRequestError(message, code, | ||
// @ts-ignore | ||
clientVersion); | ||
throwUnkownError("An operation failed because it depends on one or more records that were required but not found. {cause}"); | ||
} | ||
@@ -369,5 +390,6 @@ connectionValue = matchingRow[keyToGet]; | ||
if (c.divide) { | ||
const newValue = item[field.name] / c.divide; | ||
d = { | ||
...d, | ||
[field.name]: item[field.name] / c.divide, | ||
[field.name]: field.type === "Int" ? Math.floor(newValue) : newValue, | ||
}; | ||
@@ -476,3 +498,3 @@ } | ||
if (res.length === 0) | ||
return false; | ||
return true; | ||
// const all = data[childName].filter( | ||
@@ -512,3 +534,3 @@ // matchFnc(getFieldRelationshipWhere(item, info)), | ||
const matchFilter = { ...filter }; | ||
if (options.caseInsensitive) { | ||
if (caseInsensitive || ("mode" in matchFilter && matchFilter.mode === "insensitive")) { | ||
val = val.toLowerCase ? val.toLowerCase() : val; | ||
@@ -703,7 +725,10 @@ Object.keys(matchFilter).forEach((key) => { | ||
else if (joinfield.relationOnDelete === "Cascade") { | ||
delegate.delete({ | ||
where: { | ||
[joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]], | ||
}, | ||
}); | ||
try { | ||
delegate.delete({ | ||
where: { | ||
[joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]], | ||
}, | ||
}); | ||
} | ||
catch (e) { } | ||
} | ||
@@ -783,13 +808,34 @@ }); | ||
findFirst: findOne, | ||
findFirstOrThrow: (args) => { | ||
const found = findOne(args); | ||
if (!found) { | ||
throw new runtime_1.PrismaClientKnownRequestError(`No ${prop.slice(0, 1).toUpperCase()}${prop.slice(1)} found`, "P2025", | ||
// @ts-ignore | ||
"1.2.3"); | ||
} | ||
return found; | ||
}, | ||
create, | ||
createMany: (args) => { | ||
args.data.forEach((data) => { | ||
if (!Array.isArray(args.data)) { | ||
create({ | ||
...args, | ||
data, | ||
data: args.data, | ||
}); | ||
}); | ||
} | ||
else { | ||
args.data.forEach((data) => { | ||
create({ | ||
...args, | ||
data, | ||
}); | ||
}); | ||
} | ||
return findMany(args); | ||
}, | ||
delete: (args) => { | ||
const item = findOne(args); | ||
if (!item) { | ||
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."); | ||
} | ||
const deleted = deleteMany(args); | ||
@@ -848,2 +894,4 @@ if (deleted.length) { | ||
return; | ||
if (!client[c]) | ||
client[c] = {}; | ||
client[c][fncName].mockImplementation(async (...params) => { | ||
@@ -854,4 +902,5 @@ return objs[fncName](...params); | ||
}); | ||
// @ts-ignore | ||
return client; | ||
}; | ||
exports.default = createPrismaMock; |
{ | ||
"name": "prisma-mock", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Mock prisma for unit testing database", | ||
@@ -15,2 +15,4 @@ "main": "lib/index.js", | ||
"@types/jest": "^27.0.2", | ||
"cross-spawn": "^7.0.3", | ||
"env-cmd": "^10.1.0", | ||
"jest": "^27.3.1", | ||
@@ -26,3 +28,4 @@ "jest-mock-extended": "^2.0.4", | ||
"test": "jest", | ||
"watch": "tsc --watch" | ||
"watch": "tsc --watch", | ||
"test:postgres": "env-cmd -e postgres jest --maxWorkers=1" | ||
}, | ||
@@ -29,0 +32,0 @@ "peerDependencies": { |
@@ -16,12 +16,12 @@ # Prisma Mock | ||
beforeEach(async () => { | ||
client = await createPrismaMock() | ||
} | ||
beforeEach(() => { | ||
client = createPrismaMock() | ||
}) | ||
``` | ||
An example how to mock a global prisma instance inside and schema a "db" directory (like blitzjs): | ||
An example how to mock a global prisma instance, as the default export in a "db" directory (like blitzjs): | ||
```js | ||
import createPrismaMock from "prisma-mock"; | ||
import { mockDeep, mockReset } from "jest-mock-extended"; | ||
import createPrismaMock from "prisma-mock" | ||
import { mockDeep, mockReset } from "jest-mock-extended" | ||
@@ -32,10 +32,10 @@ jest.mock("db", () => ({ | ||
default: mockDeep(), | ||
})); | ||
})) | ||
import db, { Prisma } from "db"; | ||
import db, { Prisma } from "db" | ||
beforeEach(() => { | ||
mockReset(db); | ||
return createPrismaMock({}, Prisma.dmmf.datamodel, db); | ||
}); | ||
mockReset(db) | ||
createPrismaMock({}, Prisma.dmmf.datamodel) | ||
}) | ||
``` | ||
@@ -49,13 +49,13 @@ | ||
datamodel?: Prisma.DMMF.Datamodel, | ||
client = mockDeep<P>() | ||
client = mockDeep<P>(), | ||
): Promise<P> | ||
``` | ||
## data | ||
#### Arg: `data` | ||
Object with an array per table of default data (using `create` is preferred). Example: | ||
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: | ||
```js | ||
createPrismaMock({ | ||
users: [ | ||
user: [ | ||
{ | ||
@@ -73,16 +73,22 @@ id: 1, | ||
], | ||
}); | ||
}) | ||
``` | ||
## datamodel | ||
#### Arg: `datamodel` | ||
The datamodel of the prisma client, value of `Prisma.dmmf.datamodel`. | ||
## client | ||
#### Arg: `client` | ||
`jest-mock-extended` instance used. If not provided, a new instance is created. | ||
A `jest-mock-extended` instance. If not provided, a new instance is created. | ||
#### Arg: `caseInsensitive` | ||
If true, all string comparisons are case insensitive. | ||
# Supported features | ||
Alot of the functionality is implemented, but parts are missing. Here is a list of the (missing) features: | ||
Most common cases are covered, but not everything. Here is a rough list of the supported features: | ||
@@ -94,2 +100,3 @@ ## Model queries | ||
- findFirst, | ||
- findFirstOrThrow, | ||
- create, | ||
@@ -145,4 +152,4 @@ - createMany | ||
- NOT | ||
- mode | ||
- TODO: search | ||
- TODO: mode | ||
@@ -199,1 +206,7 @@ ## Relation filters | ||
- TODO: onUpdate | ||
## Prisma Client methods | ||
- $transaction | ||
- TODO: $transaction (interactive) | ||
- TODO: $transaction (isolation) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52254
1080
205
9