This repository contains a minimal reproduction of an issue where Vitest spies do not work with Prisma methods when using Vitest 4 and Prisma 6.
In Vitest 4 and Prisma 6, attempting to use vi.spyOn (or vitest.spyOn) on Prisma model methods (e.g., prisma.user.findMany) fails. This is because Prisma 6 uses Proxies for its models and methods, which vi.spyOn in Vitest 4 seems unable to resolve correctly, resulting in an error stating that it "can only spy on a function" but received undefined.
- Node.js (v20 or higher recommended)
- npm
-
Install dependencies:
npm install
-
Generate Prisma Client:
npx prisma generate
Run the Vitest test suite:
npm testThe test should pass, and vi.spyOn should successfully attach to the Prisma method.
The test fails with the following error:
Error: vi.spyOn() can only spy on a function. Received undefined.
Notably, this worked correctly in Vitest v3. You can verify this by downgrading Vitest and running the tests again:
npm i vitest@3
npm t