jest-dom icon indicating copy to clipboard operation
jest-dom copied to clipboard

Cannot find package 'vitest'

Open ghost opened this issue 9 months ago • 1 comments

  • @testing-library/jest-dom version: 6.6.3
  • node version: v22.13.1
  • jest (or vitest) version: 3.0.5
  • npm (or yarn) version: [email protected]

Relevant code or config:

; .npmrc
hoist=false

What you did:

Tried running tests.

What happened:

Error: Cannot find package 'vitest' imported from /path/to/project/ui/node_modules/.pnpm/@[email protected]/node_modules/@testing-library/jest-dom/dist/vitest.mjs

Suggested solution:

vitest is declared in the devDependencies of @testing-library/jest-dom but it must be declared in the dependencies or peerDependencies because @testing-library/jest-dom/dist/vitest.mjs is importing vitests expect at test run time.

ghost avatar Feb 06 '25 13:02 ghost

I'm getting the same error, someone should patch it up. I ended up downgrading to 6.5

"@testing-library/jest-dom": "6.5"

dnafication avatar Mar 12 '25 06:03 dnafication

In case anyone else comes across this before the ghost dependencies are resolved, I was able to fix this when using vitest with pnpm by using their packageExtensions feature to re-enable the peerDependencies for this package. I believe yarn also has a solution for this though I didn't investigate it.

pnpm-workspace.yaml:

packageExtensions:
  '@testing-library/jest-dom':
    peerDependencies:
      jest: '*'
      vitest: '*'

lachieh avatar Aug 18 '25 21:08 lachieh

For the record, this is a regression caused by the removal of peerDeps from this package and can only be resolved in this repo or with overrides. The src/vitest.js file imports directly from vitest but the package incorrectly omits it from peerDependencies, causing a ghost dependency.

https://github.com/testing-library/jest-dom/blob/e15f7893cda14a493c92511968502331939adef3/src/vitest.js#L3

lachieh avatar Aug 18 '25 21:08 lachieh