Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(dev): allow .server imports when running under Vitest
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 #13216

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
joshuablac and claude committed Sep 28, 2026
commit 88d31a9f44556a1355e851b7b1d249da7e882022
76 changes: 76 additions & 0 deletions integration/vite-dot-server-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync } from "node:child_process";
import * as path from "node:path";
import { expect } from "@playwright/test";
import stripAnsi from "strip-ansi";
Expand Down Expand Up @@ -268,3 +269,78 @@ test.describe("Vite / server-only escape hatch", async () => {
expect(page.errors).toEqual([]);
});
});

test.describe("Vite / server-only module imported by a Vitest test file", () => {
let files: Files = {
"app/utils.server.ts": serverOnlyModule,
"app/routes/_index.tsx": String.raw`
import { serverOnly } from "../utils.server";

export const loader = () => ({ serverOnly });

export default () => <h1>Index</h1>;
`,
"app/actions/signup.ts": String.raw`
import { serverOnly } from "../utils.server";

export const signup = () => ({ serverOnly });
`,
"tests/signup.test.ts": String.raw`
import { expect, test } from "vitest";
import { serverOnly } from "../app/utils.server";
import { signup } from "../app/actions/signup";

test("signup", () => {
expect(serverOnly).toBe("SERVER_ONLY");
expect(signup()).toEqual({ serverOnly: "SERVER_ONLY" });
});
`,
};

let viteConfigWith = (extra: string) => String.raw`
import { reactRouter } from "@react-router/dev/vite";

export default {
plugins: [reactRouter()],
${extra}
};
`;

let vitestBin = path.resolve(
import.meta.dirname,
"..",
"node_modules",
"vitest",
"vitest.mjs",
);

test("vitest can import .server modules", async () => {
let cwd = await createProject({
...files,
"vite.config.ts": viteConfigWith(
`test: { environment: "jsdom", include: ["tests/**/*.test.ts"] },`,
),
});
let result = spawnSync(process.argv[0], [vitestBin, "run"], {
cwd,
env: { ...process.env, FORCE_COLOR: undefined, NO_COLOR: "1" },
});
let output = stripAnsi(result.stdout.toString() + result.stderr.toString());
expect(output).not.toMatch("Server-only module referenced by client");
expect(result.status, output).toBe(0);
});

test("build with mode 'test' still rejects .server modules in client code", async () => {
let cwd = await createProject({
...files,
"app/routes/_index.tsx": String.raw`
import { serverOnly } from "../utils.server";

export default () => <h1>{serverOnly}</h1>;
`,
"vite.config.ts": viteConfigWith(`mode: "test",`),
});
let stderr = stripAnsi(build({ cwd }).stderr.toString());
expect(stderr).toMatch("Server-only module referenced by client");
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow `.server` modules to be imported when running tests with Vitest
10 changes: 9 additions & 1 deletion packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2145,7 +2145,15 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
viteCommand === "serve" &&
(options as { scan?: boolean })?.scan === true;

if (isOptimizeDeps || options?.ssr) return;
// Vitest runs a Vite dev server to execute test files, which may
// legitimately import server-only modules (e.g. testing a loader or
// action). Keyed on VITEST (set by Vitest itself) rather than
// `mode === "test"` so browser dev servers used for e2e runs keep this
// guard. Never relax this for builds.
let isTestRunner =
viteCommand === "serve" && Boolean(process.env.VITEST);

if (isOptimizeDeps || options?.ssr || isTestRunner) return;

let isResolving = options?.custom?.["react-router:dot-server"] ?? false;
if (isResolving) return;
Expand Down