Skip to content

Spies don't work on prisma client methods in vitest 4 #9439

@schoero

Description

@schoero

Describe the bug

When trying to spy on Prisma client methods, vitest throws the following error:

const prisma = new PrismaClient();
const spy = vitest.spyOn(prisma.user, 'findMany')
Error: vi.spyOn() can only spy on a function. Received undefined.

This setup works as expected in vitest v3, but no longer seems to behave work in vitest v4 versions.
This can be verified by installing vitest@3 in the reproduction repository and running the tests again.

Reproduction

https://github.com/schoero/repro-vitest-prisma-spies

System Info

System:
    OS: macOS 15.7.3
    CPU: (8) arm64 Apple M2
    Memory: 107.09 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.5.0 - /Users/schoero/.volta/tools/image/node/24.5.0/bin/node
    Yarn: 1.22.22 - /Users/schoero/.volta/tools/image/yarn/1.22.22/bin/yarn
    npm: 11.5.1 - /Users/schoero/.volta/tools/image/node/24.5.0/bin/npm
    pnpm: 10.16.0 - /Users/schoero/.volta/bin/pnpm
    bun: 1.3.5 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 143.0.7499.194
    Firefox: 142.0
    Safari: 26.2
  npmPackages:
    vitest: ^4.0.17 => 4.0.17

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions