Repository navigation
fix(dev): allow .server imports when running under Vitest - #15563
Conversation
Vitest runs a Vite dev server, so test files and setup files importing .server modules were rejected with "Server-only module referenced by client". Skip the check in serve mode under Vitest (VITEST env); builds are never exempted. Fixes remix-run#13216 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Critical guard-scope and test type-checking issues remain, with incomplete setup-file and build regression coverage.
Review effort: Lite
Findings: 2
Open (3)
What changed in this PR
Allows Vitest to import .server modules while preserving build-time protection.
Changes:
- Adds a Vitest-specific Vite serve exemption.
- Adds Vitest and build regression tests.
- Adds a patch changeset.
| File | Description |
|---|---|
packages/react-router-dev/vite/plugin.ts |
Adjusts .server import guarding for Vitest. |
packages/react-router-dev/.changes/patch.dot-server-vitest.md |
Documents the patch release. |
integration/vite-dot-server-test.ts |
Adds Vitest and build protection coverage. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| }); | ||
|
|
||
| test.describe("Vite / server-only module imported by a Vitest test file", () => { | ||
| let files: Files = { |
| let isTestRunner = | ||
| viteCommand === "serve" && Boolean(process.env.VITEST); | ||
|
|
||
| if (isOptimizeDeps || options?.ssr || isTestRunner) return; |
| `, | ||
| "vite.config.ts": viteConfigWith(`mode: "test",`), | ||
| }); | ||
| let stderr = stripAnsi(build({ cwd }).stderr.toString()); |
- Don't relax the .server guard when Vitest browser mode is enabled, since test files run as client code there - Drop redundant Files annotations in the test - Set VITEST in the build test so it actually covers the build path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
I found two issues in review.
I’ll push a follow-up that identifies Vitest’s Node test runner from the active Vite server, restores the annotation, and adds regression coverage for setup-file imports and ordinary dev with Authored by an AI agent on behalf of @brophdawg11 |
…g11/codex-pr-15563-vitest
brophdawg11
left a comment
There was a problem hiding this comment.
The Vitest resolver change and its regression coverage look good. I verified that the update from main preserves the reviewed diff.
Authored by an AI agent on behalf of @brophdawg11
…g11/codex-pr-15563-vitest
brophdawg11
left a comment
There was a problem hiding this comment.
This update brings in the Firefox nonce-hydration test fix from main. I verified that the reviewed Vitest patch is unchanged.
Authored by an AI agent on behalf of @brophdawg11


Fixes #13216
Problem
Vitest runs test files through Vite's dev pipeline as client (non-SSR) modules. So when a test or a
setupFilesentry imports a.servermodule, the React Router plugin's.serverguard throwsServer-only module referenced by client. That happens even though no client bundle is being built.Fix
In
packages/react-router-dev/vite/plugin.ts, the.serverguard now skips when both of these are true:vite buildstill rejects.serverimports in client code, even with--mode testorVITESTset.VITEST(which Vitest sets itself) keeps normalvite devbehaviour unchanged.Tests
In
integration/vite-dot-server-test.ts, a new describe block adds:.servermodule. It fails without the fix and passes with it.A change file is included (
packages/react-router-dev/.changes/patch.dot-server-vitest.md).Relation to earlier work
setupFilesinmode === "test". This PR covers every module Vitest loads, both test files and setup files. It keys onVITESTinstead of the mode, so a--mode testbuild can't slip through..serverfiles. Scoping to the Vitest serve process avoids that.🤖 Generated with Claude Code