Skip to content

fix: ignore NODE_PATH when checking for jest-dom - #542

Open
schiller-manuel wants to merge 1 commit into
solidjs:mainfrom
schiller-manuel:fix/jest-dom-node-path
Open

schiller-manuel wants to merge 1 commit into
solidjs:mainfrom
schiller-manuel:fix/jest-dom-node-path

Conversation

@schiller-manuel

Copy link
Copy Markdown

Fixes #541

Problem

Since #364, getJestDomExport checks for jest-dom with createRequire(<root>/package.json).resolve(). require.resolve also searches NODE_PATH, and pnpm's bin shims (pnpm vitest) point it at the hoisted virtual store, node_modules/.pnpm/node_modules. In a pnpm workspace where any package depends on jest-dom, the check finds that copy for a project that doesn't have it. The plugin then injects @testing-library/jest-dom/vitest, and Vitest, which resolves setupFiles without NODE_PATH, fails before any test runs:

Error: Failed to load url <repo>/packages/b/@testing-library/jest-dom/vitest (resolved id: <repo>/packages/b/@testing-library/jest-dom/vitest). Does the file exist?

That is Vitest 3.0.7; Vitest 5 reports Cannot find module '<project>/@testing-library/jest-dom/vitest'.

Fix

A backport of the lookup the next branch already has (d30f051, its forward-port of #364). findPackageDir walks <dir>/node_modules/@testing-library/jest-dom up from the root, as Vite does, and the subpath is then checked against the realpath of that copy. Both functions are copied unchanged from next. The injected specifier is still the bare one.

Verification

  • New test in src/jest-dom.test.ts: a project without jest-dom, with NODE_PATH set to this repo's node_modules. Node reads NODE_PATH at startup, so the test recomputes the global paths with Module._initPaths() and restores them afterwards. It fails on main (['@testing-library/jest-dom/vitest'] is injected) and passes with the fix.
  • pnpm run build, pnpm run test:unit and pnpm run test:jsdom in examples/vite-6 pass (Node 24.8, pnpm 9.15.5).
  • A minimal pnpm workspace following the steps in 2.x: jest-dom setup file is injected when only NODE_PATH reaches jest-dom (pnpm) #541: a depends on jest-dom, b is a Solid app without it, and c is a Solid app with it whose test calls toBeInTheDocument() without a setup file of its own. With 2.11.16, pnpm vitest run in b fails with the error above. With this branch linked in, b passes, and c still gets the setup file and passes.

Not run: the browser-mode suite of examples/vite-6 (the plugin doesn't inject jest-dom in browser mode) and the Cypress example tests. In one of 23 full test:unit runs, right after the first build, src/transform-ids.test.ts failed to load. I didn't capture the error; the 22 runs since passed, and that file doesn't involve jest-dom.

🤖 Generated with Claude Code

getJestDomExport checked for `@testing-library/jest-dom/vitest` with a
`createRequire` rooted at the project (solidjs#364). `require.resolve` also
searches NODE_PATH, which pnpm's bin shims (`pnpm vitest`) point at the
hoisted virtual store (`node_modules/.pnpm/node_modules`). In a workspace
where any package depends on jest-dom, the check found that copy for
projects without it, and Vitest, which resolves `setupFiles` without
NODE_PATH, failed to load the injected setup file.

Backport the lookup from the `next` branch (d30f051): walk
`node_modules` up from the root, then check the subpath against that
copy of the package.

Fixes solidjs#541

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ee21af4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
vite-plugin-solid Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/vite-plugin-solid@542

commit: ee21af4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2.x: jest-dom setup file is injected when only NODE_PATH reaches jest-dom (pnpm)

1 participant