Skip to content

Commit

Permalink
defineRoute: test for removing server-only code
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Jul 2, 2024
1 parent f3528ba commit 06b054a
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions integration/vite-define-route-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from "node:path";
import { expect } from "@playwright/test";

import type { Files } from "./helpers/vite";
import { test, build, createProject, viteConfig } from "./helpers/vite";
import { test, build, createProject, viteConfig, grep } from "./helpers/vite";
import dedent from "dedent";
import stripAnsi from "strip-ansi";
import getPort from "get-port";

test.describe("defineRoute", () => {
test("fails when used outside of route modules", async () => {
Expand Down Expand Up @@ -259,19 +261,39 @@ test.describe("defineRoute", () => {
plugins: [reactRouter()],
}
`,
"app/server-only.ts": dedent`
export const SERVER_ONLY = "SERVER_ONLY";
`,
"app/routes/_index.tsx": dedent`
import { defineRoute } from "react-router";
import { defineRoute } from "react-router"
import { SERVER_ONLY } from "../server-only"
export default defineRoute({
serverLoader() {
console.log(SERVER_ONLY)
return { planet: "world" }
},
serverAction() {
console.log(SERVER_ONLY)
return null
},
Component({ loaderData }) {
return <h1 data-title>Hello, {loaderData.planet}!</h1>
}
})
`,
});

test("removes server-only code", async () => {
let port = await getPort();
let cwd = await createProject(await files({ port }));

let { status } = build({ cwd });
expect(status).toBe(0);

let client = path.join(cwd, "build/client");
expect(grep(client, /SERVER_ONLY/).length).toBe(0);
});

test("react-router dev", async ({ page, dev }) => {
let { port } = await dev(files);
await page.goto(`http://localhost:${port}/`, {
Expand Down

0 comments on commit 06b054a

Please sign in to comment.