jest-prisma
jest-prisma copied to clipboard
Prisma Client Error won't be handled properly
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
> [email protected] 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
I think this is because of how jest isolates context, as described here: https://backend.cafe/should-you-use-jest-as-a-testing-library I also see it when doing a simple Array instancesof:
expect(result.data).toBeInstanceOf(Array);
// Expected constructor: Array
// Received constructor: Array
Changing jest-prisma-node
to derive from jest-environment-node-single-context
instead of jest-environment-node
might fix it, but I have no idea how to accomplish that.