Skip to content

fix: resolve @testing-library/jest-dom from the project root - #364

Open
brenelz wants to merge 1 commit into
mainfrom
fix/jest-dom-resolve-from-root-231
Open

brenelz wants to merge 1 commit into
mainfrom
fix/jest-dom-resolve-from-root-231

Conversation

@brenelz

@brenelz brenelz commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #231

Problem

getJestDomExport used the plugin's own require (created from import.meta.url), so the check was "can vite-plugin-solid reach @testing-library/jest-dom/vitest?". With pnpm's isolated node_modules that is true whenever another package in the tree (Storybook in the issue) depends on jest-dom transitively, because pnpm links the optional peer next to the plugin. Vitest, however, resolves bare setupFiles specifiers from the project root, where @testing-library/jest-dom is not installed, so the injected setup file fails with Failed to load url .../@testing-library/jest-dom/vitest.

Fix

Probe with a require created from the project root (userConfig.root, falling back to process.cwd()), so the setup file is only injected when the project itself can resolve jest-dom. That matches the intent of an optional peer dependency: you opt in by installing it.

The bare specifier is still what gets injected. I tried injecting the resolved absolute path as extra hardening, but require.resolve picks jest-dom's CommonJS entry (dist/vitest.js), which Vitest rejects with "Vitest cannot be imported in a CommonJS module using require()". Vitest resolving the specifier itself lands on the ESM entry. There is a comment explaining this next to the code.

Verification

  • Called the built plugin's config hook with mode: 'test' from a Node script. With root set to this repo (jest-dom installed) it injects @testing-library/jest-dom/vitest; with root set to an empty project it injects nothing, even though the plugin itself can still resolve jest-dom. That second case is the issue's scenario.
  • Ran a temporary non-browser Vitest test in examples/vite-7 against src/index.ts asserting expect(null).toBeInTheDocument is a function. It passes, so the injected setup file loads.

🤖 Generated with Claude Code

The Vitest setup-file check used the plugin's own `require`, so it asked
whether vite-plugin-solid could reach `@testing-library/jest-dom/vitest`.
Under pnpm's isolated layout that succeeds when another package (e.g.
Storybook) depends on jest-dom transitively, but Vitest resolves
`setupFiles` from the project root, where the package is not installed,
and fails with "Failed to load url .../@testing-library/jest-dom/vitest".

Probe from the project root instead so the setup file is only injected
when the project itself can resolve it. The bare specifier is still what
gets injected: the resolved path would be jest-dom's CommonJS entry,
which Vitest refuses to load.

Fixes #231

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6cc30fe

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 Sep 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 6cc30fe

@brenelz brenelz added the 1.x label Sep 18, 2026
ryansolid added a commit that referenced this pull request Sep 18, 2026
fix: forward-port #364 (jest-dom root resolution) + #323 (respect vitest projects environments)
@github-actions github-actions Bot mentioned this pull request Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check for @testing-library/jest-dom doesn't guarantee it's safe to use as setupFiles in Vitest

1 participant