Quramy / jest-prisma

Jest environment for integrated testing with Prisma client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prisma Client Error won't be handled properly

aokht opened this issue · comments

commented

Thank you for the great library.

When I tried to add a following test into the examples/example-prj/src/service, I found a suspicious behavior.

import { Prisma, PrismaClient } from "@prisma/client";

describe.each([
  ["Original PrismaClient", new PrismaClient()],
  ["jestPrisma.client", jestPrisma.client],
])("%s", (_, prisma) => {
  test("handles prisma error properly", async () => {
    try {
      await prisma.user.update({
        data: { name: "" },
        where: { id: "record_not_found" },
      });
    } catch (e) {
      expect(e).toBeInstanceOf(Prisma.PrismaClientKnownRequestError);
    }
  });
});
npm run test:ci src/service/prismaError.test.ts

> example-prj@1.4.0 test:ci
> DATABASE_URL="file:./test.db" jest "src/service/prismaError.test.ts"

 FAIL  src/service/prismaError.test.ts
  Original PrismaClient
    ✓ handles prisma error properly (19 ms)
  jestPrisma.client
    ✕ handles prisma error properly (144 ms)

  ● jestPrisma.client › handles prisma error properly

    expect(received).toBeInstanceOf(expected)

    Expected constructor: PrismaClientKnownRequestError
    Received constructor: PrismaClientKnownRequestError

      12 |       });
      13 |     } catch (e) {
    > 14 |       expect(e).toBeInstanceOf(Prisma.PrismaClientKnownRequestError);
         |                 ^
      15 |     }
      16 |   });
      17 | });

      at Object.<anonymous> (src/service/prismaError.test.ts:14:17)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        3.502 s

It seems that the prisma error thrown via jestPrisma.client isn't recognized as an instanceof PrismaClientKnownRequestError properly (even though it shows a same name "PrismaClientKnownRequestError")

And this prevents us from handling prisma errors by the way the official document provides.

Do you have any idea on this?
Thanks!

Same here and it breaks the test cases