@pkgverse/prismock 2.0.1-beta.6 → 2.0.1-beta.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  # prismock
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/@pkgverse/prismock)](https://www.npmjs.com/package/@pkgverse/prismock)
4
- [![npm](https://img.shields.io/npm/dm/@pkgverse/prismock)](https://www.npmjs.com/package/prismock)
4
+ [![npm](https://img.shields.io/npm/dm/@pkgverse/prismock)](https://www.npmjs.com/package/@pkgverse/prismock)
5
5
 
6
6
  ## NOTE
7
7
  Originally forked from https://github.com/morintd/prismock. This library is awesome, and I felt it could use some modernization to work with newer versions of prisma and add support for
@@ -43,12 +43,11 @@ $ bun add -E -D @pkgverse/prismock
43
43
  # Usage
44
44
 
45
45
  ```ts
46
- import { Prisma, PrismaClient } from "${your_prisma_client_directory}"
46
+ import { PrismaClient } from "${your_prisma_client_directory}"
47
47
  import { getClient } from 'prismock';
48
48
 
49
49
  // Pass in the Prisma namespace, and manually define the type of your prisma client
50
50
  let mockedClient = await getClient({
51
- prismaModule: Prisma,
52
51
  prismaClient: PrismaClient,
53
52
  schemaPath: "prisma/schema.prisma",
54
53
  })
@@ -64,12 +63,10 @@ That's it, prisma will be mocked in all your tests (tested with ViTest)
64
63
  If you're using prisma with postgres, you can optionally choose to have the mocked prisma client use PgLite for more 'true-to-life' tests.
65
64
 
66
65
  ```ts
67
- import { Prisma, PrismaClient } from "${your_prisma_client_directory}"
66
+ import { PrismaClient } from "${your_prisma_client_directory}"
68
67
  import { getClient } from 'prismock';
69
68
 
70
- // Pass in the Prisma namespace, and manually define the type of your prisma client
71
69
  let mockedClient = await getClient({
72
- prismaModule: Prisma,
73
70
  prismaClient: PrismaClient,
74
71
  schemaPath: "prisma/schema.prisma",
75
72
  usePgLite: true,
@@ -100,7 +97,6 @@ vi.mock('@prisma/client', async () => {
100
97
  return {
101
98
  ...actual,
102
99
  PrismaClient: await actualPrismock.getClientClass({
103
- prismaModule: actual.Prisma,
104
100
  prismaClient: actual.PrismaClient,
105
101
  schemaPath: "prisma/schema.prisma",
106
102
  }),
@@ -113,11 +109,10 @@ vi.mock('@prisma/client', async () => {
113
109
  You can instantiate a `PrismockClient` directly and use it in your test, or pass it to a test version of your app.
114
110
 
115
111
  ```ts
116
- import { Prisma, PrismaClient } from '${your_prisma_client_directory}';
112
+ import { PrismaClient } from '${your_prisma_client_directory}';
117
113
  import { getClient } from 'prismock';
118
114
 
119
115
  const client = await getClient({
120
- prismaModule: Prisma,
121
116
  PrismaClient,
122
117
  schemaPath: "prisma/schema.prisma",
123
118
  });
package/dist/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- export { generatePrismock, generatePrismockSync } from './lib/prismock';
2
- export { createPrismock, Prismock, type PrismaModule, PrismockClientType, getClient, getClientClass, getDefaultClient, getDefaultClientClass } from './lib/client';
1
+ export { createPrismock, Prismock, PrismockClientType, getClient, getClientClass, getDefaultClient, getDefaultClientClass } from './lib/client';
package/dist/index.mjs CHANGED
@@ -627,6 +627,10 @@ var require_src = __commonJS((exports, module) => {
627
627
  exports.isCuid = isCuid;
628
628
  });
629
629
 
630
+ // src/lib/client.ts
631
+ import * as path2 from "path";
632
+ import * as fs from "fs";
633
+
630
634
  // src/lib/operations/aggregate.ts
631
635
  function aggregate(args, items) {
632
636
  const aggregated = {};
@@ -4869,9 +4873,63 @@ function generateDelegate(model, data, name, properties, delegates, onChange) {
4869
4873
  return delegate;
4870
4874
  }
4871
4875
 
4872
- // src/lib/client.ts
4873
- import * as path2 from "path";
4874
- import * as fs from "fs";
4876
+ // src/lib/dmmf.ts
4877
+ import * as path from "path";
4878
+ var PrismaInternals = await import("@prisma/internals");
4879
+ var { getDMMF, getSchemaWithPath, getConfig } = PrismaInternals.default;
4880
+ async function generateDMMF(schemaPath) {
4881
+ const pathToModule = schemaPath ?? __require.resolve(path.resolve(process.cwd(), "prisma/schema.prisma"));
4882
+ const datamodel = await getSchemaWithPath(pathToModule);
4883
+ return await getDMMF({ datamodel: datamodel.schemas });
4884
+ }
4885
+
4886
+ // src/lib/prismock.ts
4887
+ function generateDelegates(options) {
4888
+ const models = options.models ?? [];
4889
+ const data = {};
4890
+ const properties = {};
4891
+ const delegates = {};
4892
+ async function getData() {
4893
+ return data;
4894
+ }
4895
+ async function setData(d) {
4896
+ console.log('Deprecation notice: setData will be removed in a future version and should not be used anymore. Please use a mix of "reset" and create/createMany to achieve the same result');
4897
+ Object.assign(data, d);
4898
+ Object.assign(properties, Object.entries(d).reduce((accumulator, [currentKey]) => {
4899
+ const model = models.find((m) => camelize(m.name) === currentKey);
4900
+ return {
4901
+ ...accumulator,
4902
+ [currentKey]: {
4903
+ increment: model.fields.reduce((propertiesAccumulator, currentField) => {
4904
+ if (isAutoIncrement(currentField)) {
4905
+ return { ...propertiesAccumulator, [currentField.name]: d[currentKey].length };
4906
+ }
4907
+ return propertiesAccumulator;
4908
+ }, {})
4909
+ }
4910
+ };
4911
+ }, {}));
4912
+ }
4913
+ models.forEach((model) => {
4914
+ const name = camelize(model.name);
4915
+ data[name] = [];
4916
+ properties[name] = {
4917
+ increment: {}
4918
+ };
4919
+ Object.assign(delegates, {
4920
+ [name]: generateDelegate(model, data, name, properties, delegates, (items) => {
4921
+ Object.assign(data, { [name]: items });
4922
+ })
4923
+ });
4924
+ }, {});
4925
+ const clientDelegates = Object.entries(delegates).reduce((accumulator, [delegateKey, delegateValue]) => {
4926
+ return {
4927
+ ...accumulator,
4928
+ [delegateKey]: omit(delegateValue, ["model", "properties", "getItems"])
4929
+ };
4930
+ }, {});
4931
+ return { delegates: clientDelegates, getData, setData };
4932
+ }
4875
4933
 
4876
4934
  // src/lib/extensions/model.ts
4877
4935
  function applyModelExtensions(client, extensions) {
@@ -5159,36 +5217,33 @@ function applyExtensions(client, extensions) {
5159
5217
  return modelExtended;
5160
5218
  }
5161
5219
 
5162
- // src/lib/dmmf.ts
5163
- import * as path from "path";
5164
- var PrismaInternals = await import("@prisma/internals");
5165
- var { getDMMF, getSchemaWithPath, getConfig } = PrismaInternals.default;
5166
- async function generateDMMF(schemaPath) {
5167
- const pathToModule = schemaPath ?? __require.resolve(path.resolve(process.cwd(), "prisma/schema.prisma"));
5168
- const datamodel = await getSchemaWithPath(pathToModule);
5169
- return await getDMMF({ datamodel: datamodel.schemas });
5170
- }
5171
-
5172
5220
  // src/lib/client.ts
5173
5221
  class Prismock {
5174
- __prismaModule;
5175
- constructor(prismaModule) {
5176
- this.__prismaModule = prismaModule;
5177
- this.generate();
5178
- }
5179
- static async create(prismaModule) {
5180
- return new Prismock(prismaModule);
5181
- }
5182
- static async createDefault() {
5183
- const { Prisma: Prisma2, PrismaClient } = await import("@prisma/client");
5184
- return new Prismock(Prisma2);
5185
- }
5186
- reset() {
5187
- this.generate();
5188
- }
5189
- generate() {
5222
+ schemaPath;
5223
+ genPromise;
5224
+ constructor(schemaPath) {
5225
+ this.schemaPath = schemaPath;
5226
+ this.genPromise = this.generate();
5227
+ }
5228
+ static async create(schemaPath) {
5229
+ const p = new Prismock(schemaPath);
5230
+ await p.genPromise;
5231
+ return p;
5232
+ }
5233
+ static async createDefault(schemaPath) {
5234
+ const p = new Prismock(schemaPath);
5235
+ await p.genPromise;
5236
+ return p;
5237
+ }
5238
+ async reset() {
5239
+ await this.generate();
5240
+ }
5241
+ async generate() {
5242
+ const datamodel = await generateDMMF(this.schemaPath);
5190
5243
  const { delegates, setData, getData } = generateDelegates({
5191
- models: this.__prismaModule.dmmf.datamodel.models
5244
+ models: [
5245
+ ...datamodel.datamodel.models
5246
+ ]
5192
5247
  });
5193
5248
  Object.entries({ ...delegates, setData, getData }).forEach(([key, value]) => {
5194
5249
  if (key in this)
@@ -5198,7 +5253,8 @@ class Prismock {
5198
5253
  });
5199
5254
  }
5200
5255
  async $connect() {
5201
- return Promise.resolve();
5256
+ await this.genPromise;
5257
+ return this;
5202
5258
  }
5203
5259
  $disconnect() {
5204
5260
  return Promise.resolve();
@@ -5229,31 +5285,6 @@ class Prismock {
5229
5285
  return args(this);
5230
5286
  }
5231
5287
  }
5232
- function generateClient(delegates, getData, setData) {
5233
- console.log("Deprecation notice: generatePrismock and generatePrismockSync should be replaced with PrismockClient. See https://github.com/morintd/prismock/blob/master/docs/generate-prismock-deprecated.md");
5234
- const client = {
5235
- $connect: () => Promise.resolve(),
5236
- $disconnect: () => Promise.resolve(),
5237
- $on: () => {},
5238
- $use: () => {},
5239
- $executeRaw: () => Promise.resolve(0),
5240
- $executeRawUnsafe: () => Promise.resolve(0),
5241
- $queryRaw: () => Promise.resolve([]),
5242
- $queryRawUnsafe: () => Promise.resolve([]),
5243
- getData,
5244
- setData,
5245
- ...delegates
5246
- };
5247
- return {
5248
- ...client,
5249
- $transaction: async (args) => {
5250
- if (Array.isArray(args)) {
5251
- return Promise.all(args);
5252
- }
5253
- return args(client);
5254
- }
5255
- };
5256
- }
5257
5288
  function getPgLitePrismockData(options) {
5258
5289
  const schemaPathDir = path2.dirname(options.schemaPath);
5259
5290
  const migrationsPath = path2.join(schemaPathDir, "migrations");
@@ -5333,7 +5364,7 @@ async function getClient(options) {
5333
5364
  await prismockData.reset();
5334
5365
  return Object.assign(prisma2, prismockData);
5335
5366
  }
5336
- return await Prismock.create(options.prismaModule);
5367
+ return await Prismock.create(options.schemaPath);
5337
5368
  }
5338
5369
  async function getClientClass(options) {
5339
5370
  const datamodel = await generateDMMF(options.schemaPath);
@@ -5385,93 +5416,33 @@ async function getClientClass(options) {
5385
5416
 
5386
5417
  class PrismaClientMocked extends Prismock {
5387
5418
  constructor() {
5388
- super(options.prismaModule);
5419
+ super(options.schemaPath);
5389
5420
  }
5390
5421
  }
5391
5422
  return PrismaClientMocked;
5392
5423
  }
5393
5424
  async function getDefaultClient() {
5394
- const { Prisma: Prisma2, PrismaClient } = await import("@prisma/client");
5425
+ const { PrismaClient } = await import("@prisma/client");
5395
5426
  return await getClient({
5396
- prismaModule: Prisma2,
5397
5427
  prismaClient: PrismaClient,
5398
5428
  schemaPath: "./prisma/schema.prisma",
5399
5429
  usePgLite: process.env.PRISMOCK_USE_PG_LITE ? true : undefined
5400
5430
  });
5401
5431
  }
5402
5432
  async function getDefaultClientClass() {
5403
- const { Prisma: Prisma2, PrismaClient } = await import("@prisma/client");
5433
+ const { PrismaClient } = await import("@prisma/client");
5404
5434
  return await getClientClass({
5405
- prismaModule: Prisma2,
5406
5435
  PrismaClient,
5407
5436
  schemaPath: "./prisma/schema.prisma",
5408
5437
  usePgLite: process.env.PRISMOCK_USE_PG_LITE ? true : undefined
5409
5438
  });
5410
5439
  }
5411
5440
  var createPrismock = getDefaultClient;
5412
-
5413
- // src/lib/prismock.ts
5414
- async function generatePrismock(options = {}) {
5415
- const schema = await generateDMMF(options.schemaPath);
5416
- return generatePrismockSync({ models: schema.datamodel.models });
5417
- }
5418
- function generatePrismockSync(options) {
5419
- const { delegates, getData, setData } = generateDelegates(options);
5420
- return generateClient(delegates, getData, setData);
5421
- }
5422
- function generateDelegates(options) {
5423
- const models = options.models ?? [];
5424
- const data = {};
5425
- const properties = {};
5426
- const delegates = {};
5427
- async function getData() {
5428
- return data;
5429
- }
5430
- async function setData(d) {
5431
- console.log('Deprecation notice: setData will be removed in a future version and should not be used anymore. Please use a mix of "reset" and create/createMany to achieve the same result');
5432
- Object.assign(data, d);
5433
- Object.assign(properties, Object.entries(d).reduce((accumulator, [currentKey]) => {
5434
- const model = models.find((m) => camelize(m.name) === currentKey);
5435
- return {
5436
- ...accumulator,
5437
- [currentKey]: {
5438
- increment: model.fields.reduce((propertiesAccumulator, currentField) => {
5439
- if (isAutoIncrement(currentField)) {
5440
- return { ...propertiesAccumulator, [currentField.name]: d[currentKey].length };
5441
- }
5442
- return propertiesAccumulator;
5443
- }, {})
5444
- }
5445
- };
5446
- }, {}));
5447
- }
5448
- models.forEach((model) => {
5449
- const name = camelize(model.name);
5450
- data[name] = [];
5451
- properties[name] = {
5452
- increment: {}
5453
- };
5454
- Object.assign(delegates, {
5455
- [name]: generateDelegate(model, data, name, properties, delegates, (items) => {
5456
- Object.assign(data, { [name]: items });
5457
- })
5458
- });
5459
- }, {});
5460
- const clientDelegates = Object.entries(delegates).reduce((accumulator, [delegateKey, delegateValue]) => {
5461
- return {
5462
- ...accumulator,
5463
- [delegateKey]: omit(delegateValue, ["model", "properties", "getItems"])
5464
- };
5465
- }, {});
5466
- return { delegates: clientDelegates, getData, setData };
5467
- }
5468
5441
  export {
5469
5442
  getDefaultClientClass,
5470
5443
  getDefaultClient,
5471
5444
  getClientClass,
5472
5445
  getClient,
5473
- generatePrismockSync,
5474
- generatePrismock,
5475
5446
  createPrismock,
5476
5447
  Prismock
5477
5448
  };
@@ -1,7 +1,6 @@
1
1
  import type { PrismaClient } from "@prisma/client";
2
2
  import type { DMMF } from "@prisma/generator-helper";
3
3
  import type * as runtime from "@prisma/client/runtime/library";
4
- import type { Delegate } from "./delegate";
5
4
  import { Data } from "./prismock";
6
5
  import { type ExtensionsDefinition } from "./extensions";
7
6
  import type { PGlite } from "@electric-sql/pglite";
@@ -19,14 +18,15 @@ export type PrismockOptions = {
19
18
  schemaPath: string;
20
19
  };
21
20
  };
22
- export declare class Prismock<PC = PrismaClient> {
23
- __prismaModule: PrismaModule<PC>;
24
- protected constructor(prismaModule: PrismaModule<PC>);
25
- static create<PC = PrismaClient>(prismaModule: PrismaModule<PC>): Promise<PrismockClientType<PC>>;
26
- static createDefault(): Promise<PrismaClient<import(".prisma").Prisma.PrismaClientOptions, never, runtime.DefaultArgs> & PrismockData>;
27
- reset(): void;
21
+ export declare class Prismock {
22
+ schemaPath: string;
23
+ private genPromise;
24
+ protected constructor(schemaPath: string);
25
+ static create<PC = PrismaClient>(schemaPath: string): Promise<PrismockClientType<PC>>;
26
+ static createDefault(schemaPath: string): Promise<PrismaClient<import(".prisma").Prisma.PrismaClientOptions, never, runtime.DefaultArgs> & PrismockData>;
27
+ reset(): Promise<void>;
28
28
  private generate;
29
- $connect(): Promise<void>;
29
+ $connect(): Promise<this>;
30
30
  $disconnect(): Promise<void>;
31
31
  $on(): void;
32
32
  $use(): this;
@@ -37,10 +37,6 @@ export declare class Prismock<PC = PrismaClient> {
37
37
  $extends(extensionDefs: ExtensionsDefinition): PrismaClient<import(".prisma").Prisma.PrismaClientOptions, never, runtime.DefaultArgs>;
38
38
  $transaction(args: any): Promise<any>;
39
39
  }
40
- export declare function generateClient<T = PrismaClient>(delegates: Record<string, Delegate>, getData: GetData, setData: SetData): PrismockClientType<T>;
41
- export type PrismaModule<PC = PrismaClient> = {
42
- dmmf: runtime.BaseDMMF;
43
- };
44
40
  export declare function getPgLitePrismockData(options: {
45
41
  schemaPath: string;
46
42
  pglite: InstanceType<typeof PGlite>;
@@ -53,7 +49,6 @@ export declare function getPgLitePrismockData(options: {
53
49
  setData: (data: Data) => Promise<void>;
54
50
  };
55
51
  type GetClientOptions<PrismaClientClassType extends new (...args: any[]) => any> = {
56
- prismaModule: PrismaModule<InstanceType<PrismaClientClassType>>;
57
52
  prismaClient: PrismaClientClassType;
58
53
  schemaPath: string;
59
54
  usePgLite?: boolean | null | undefined;
@@ -62,7 +57,6 @@ export declare function getClient<PrismaClientType extends new (options: {
62
57
  adapter?: runtime.SqlDriverAdapterFactory | null;
63
58
  }, ...args: any[]) => any>(options: GetClientOptions<PrismaClientType>): Promise<PrismockClientType<InstanceType<PrismaClientType>>>;
64
59
  type GetClientClassOptions<PrismaClientClassType extends new (...args: any[]) => any> = {
65
- prismaModule: PrismaModule<InstanceType<PrismaClientClassType>>;
66
60
  PrismaClient: PrismaClientClassType;
67
61
  schemaPath: string;
68
62
  usePgLite?: boolean | null | undefined;
@@ -1,21 +1,15 @@
1
- import { PrismaClient } from '@prisma/client';
2
- import { DMMF, type ActiveConnectorType } from '@prisma/generator-helper';
1
+ import { type ActiveConnectorType } from '@prisma/generator-helper';
3
2
  import type { Generator } from '@prisma/internals';
4
3
  import { Delegate, DelegateProperties, Item } from './delegate';
5
- import { PrismockClientType } from './client';
6
- type Options = {
7
- schemaPath?: string;
8
- };
4
+ import type { Model } from "@prisma/dmmf";
9
5
  type OptionsSync = {
10
- models: DMMF.Model[];
6
+ models: Model[];
11
7
  };
12
8
  export type Data = Record<string, Item[]>;
13
9
  export type Properties = Record<string, DelegateProperties>;
14
10
  export type Delegates = Record<string, Delegate>;
15
11
  export declare function fetchProvider(schemaPath?: string): Promise<ActiveConnectorType>;
16
12
  export declare function getProvider(generator: Generator): ActiveConnectorType | undefined;
17
- export declare function generatePrismock<T = PrismaClient>(options?: Options): Promise<PrismockClientType<T>>;
18
- export declare function generatePrismockSync<T = PrismockClientType>(options: OptionsSync): PrismockClientType<T>;
19
13
  export declare function generateDelegates(options: OptionsSync): {
20
14
  delegates: Delegates;
21
15
  getData: () => Promise<Data>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pkgverse/prismock",
3
- "version": "2.0.1-beta.6",
3
+ "version": "2.0.1-beta.8",
4
4
  "description": "A mock for PrismaClient, dedicated to unit testing.",
5
5
  "repository": {
6
6
  "url": "https://github.com/JQuezada0/prismock"
@@ -44,6 +44,7 @@
44
44
  "@commitlint/cli": "19.8.1",
45
45
  "@commitlint/config-conventional": "19.8.1",
46
46
  "@prisma/client": "6.12.0",
47
+ "@prisma/dmmf": "6.12.0",
47
48
  "@semantic-release/changelog": "6.0.3",
48
49
  "@semantic-release/commit-analyzer": "13.0.1",
49
50
  "@semantic-release/git": "10.0.1",