Skip to content

Some mocked methods no longer work after upgrading to Prisma 6.16 with rust-free engine and prisma-client #1482

@keyserj

Description

@keyserj

Problem

After upgrading from prisma 5.15.1 -> 6.16.2 and using the rust-free engine + generating prisma code outside of node modules, my tests started failing for multiple mock-related reasons, one specific example being that create would return exactly the model that I passed in, without setting any default values e.g. the primary key.

Previous mocking strategy, using vitest:

import { PrismockClient } from "prismock";
import { PrismockClientType } from "prismock/build/main/lib/client";
import { vi } from "vitest";

vi.mock("@prisma/client", () => ({
  PrismaClient: PrismockClient,
}));

Prisma generator config:

generator client {
  provider            = "prisma-client"
  output              = "./generated/prisma"
  importFileExtension = "ts"
  engineType          = "client"
}

Investigation

I noticed that prismock relies on Prisma.dmmf for determining how to handle each field on creation (and for other mocked methods), but this property is not set with the same values as before the upgrade.

When using prisma-client-js as the client, the values are as follows:

Prisma.dmmf.datamodel.models[0].fields[0], on 5.15.1:

  {
    name: 'id',
    kind: 'scalar',
    isList: false,
    isRequired: true,
    isUnique: false,
    isId: true,
    isReadOnly: false,
    hasDefaultValue: true,
    type: 'Int',
    default: { name: 'autoincrement', args: [] },
    isGenerated: false,
    isUpdatedAt: false
  }

on 6.16.2:

  { name: 'id', kind: 'scalar', type: 'Int' }

I also tried using prisma-client, but this doesn't set Prisma.dmmf at all anymore.

Workaround

This is what is working for me now, getting prismock the dmmf it needs via Prisma's getDMMF(), and prismock's createPrismock:

import { type DMMF } from "@prisma/client/runtime/client";
import { getDMMF } from "@prisma/internals";
import { type PrismockClientType, createPrismock } from "prismock/build/main/lib/client";
import { vi } from "vitest";
import { type PrismaClient } from "@/db/generated/prisma/client";

vi.mock("@/db/generated/prisma/client", async (importOriginal) => {
  const original = await importOriginal<typeof import("@/db/generated/prisma/client")>();
  const { Prisma } = original;

  const dmmf = await getDMMF({ datamodelPath: "./src/db/schema.prisma" });
  const PrismaWithDMMF = { ...Prisma, dmmf };

  return {
    // ensure other parts of the import are still usable e.g. `Prisma`
    ...original,
    PrismaClient: createPrismock(
      PrismaWithDMMF as unknown as typeof Prisma & { dmmf: DMMF.Document },
    ),
  };
});

Notes:

Solution ?

It seems like prismock could rely on getDMMF() instead of Prisma.dmmf. Might need to require a schema path passed in? Not sure if there's a way to get at the configured schema path from Prisma.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions