Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Vitest 4.1.0 breaks CJS import of postgres ("pg") #10012

Description

@konstantin-lukas

Describe the bug

After upgrading from v4.0.18 to v4.1.0 (specifically I found that this issue was introduced in v4.1.0-beta.5) my integration tests no longer work because the "pg" module throws an error:

TypeError: Class extends value [object Module] is not a constructor or null
 ❯ poolFactory node_modules/pg/lib/index.js:14:3
 ❯ new PG node_modules/pg/lib/index.js:25:15
 ❯ Object.<anonymous> node_modules/pg/lib/index.js:50:18

If you investigate the source files, you will see that the Pool exported from "pg" is a factory function that returns a class that extends the Pool class from "pg-pool". This leads me to believe that the underlying issue has something to do with module resolution. It could be something like cjs getting wrapped to fit esm or something. However, I tried importing different entrypoints from "pg" but to no avail.

You will notice that the reproduction runs with the nuxt/vue test utils. The vitest config uses defineVitestProject from @nuxt/test-utils, so the issue could also be there but since the issue first started appearing after updating vitest, it would also be reasonable to assume that this a regression but it could very well be some sort of incompatibility.

Thank you for your time and work. It's much appreciated :)

Reproduction

  1. Clone this repository: https://github.com/konstantin-lukas/vitest-4.1.0-repro
  2. run npm i to install deps
  3. run npm test to run vitest
  4. You should see the described error

System Info

System:
    OS: Linux 6.6 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
    CPU: (16) x64 AMD Ryzen 7 7700X 8-Core Processor
    Memory: 10.20 GB / 15.20 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 24.14.1 - /home/konst/.nvm/versions/node/v24.14.1/bin/node
    npm: 11.11.0 - /home/konst/.nvm/versions/node/v24.14.1/bin/npm
  npmPackages:
    vitest: ^4.1.2 => 4.1.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions