Skip to content

fix(dev): allow .server imports when running under Vitest - #15563

Merged
brophdawg11 merged 5 commits into
remix-run:mainfrom
joshuablac:fix/dot-server-vitest
Nov 23, 2026
Merged

brophdawg11 merged 5 commits into
remix-run:mainfrom
joshuablac:fix/dot-server-vitest

Conversation

@joshuablac

Copy link
Copy Markdown
Contributor

Fixes #13216

Problem

Vitest runs test files through Vite's dev pipeline as client (non-SSR) modules. So when a test or a setupFiles entry imports a .server module, the React Router plugin's .server guard throws Server-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 .server guard now skips when both of these are true:

viteCommand === "serve" && Boolean(process.env.VITEST)
  • Builds are never exempt. vite build still rejects .server imports in client code, even with --mode test or VITEST set.
  • Keying on VITEST (which Vitest sets itself) keeps normal vite dev behaviour unchanged.

Tests

In integration/vite-dot-server-test.ts, a new describe block adds:

  • vitest can import .server modules: runs Vitest on a test file that imports a .server module. It fails without the fix and passes with it.
  • build with mode 'test' still rejects .server modules in client code: a regression guard showing builds stay protected.

A change file is included (packages/react-router-dev/.changes/patch.dot-server-vitest.md).

Relation to earlier work

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity

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.

Comment thread integration/vite-dot-server-test.ts Outdated
});

test.describe("Vite / server-only module imported by a Vitest test file", () => {
let files: Files = {
Comment on lines +2153 to +2156
let isTestRunner =
viteCommand === "serve" && Boolean(process.env.VITEST);

if (isOptimizeDeps || options?.ssr || isTestRunner) return;
Comment thread integration/vite-dot-server-test.ts Outdated
`,
"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>
@brophdawg11

Copy link
Copy Markdown
Contributor

I found two issues in review.

  • The .server exception can apply to ordinary Vite dev servers that inherit VITEST. Vitest can also enable Browser Mode from the CLI without setting test.browser.enabled in the Vite config the guard reads.
  • Removing the existing Files annotation left port implicitly any and broke the integration typecheck.

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 VITEST set. I verified that CLI-enabled Chromium Browser Mode still rejects the import on Vitest 4 and 5 with the fix.

Authored by an AI agent on behalf of @brophdawg11

@brophdawg11 brophdawg11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@brophdawg11 brophdawg11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@brophdawg11
brophdawg11 merged commit cdfbeb8 into remix-run:main Nov 23, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test setup/utils files importing server module gives: Server-only module referenced by client

3 participants