Skip to content

Commit a01e037

Browse files
bartlomiejuclaude
andcommitted
test: add tests for CSS modules in _app.tsx and shared across routes
Adds an AppNav island with a CSS module to _app.tsx and a second page that shares the CssModules island. Tests verify: 1. CSS modules from islands in _app.tsx are injected into the page (both build and dev mode) 2. CSS modules from shared islands work on the second page visited, not just the first (both build and dev mode) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3044d60 commit a01e037

6 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.nav {
2+
background-color: rgb(30, 30, 30);
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @ts-ignore upstream issue https://github.com/denoland/deno/issues/30560
2+
import styles from "./AppNav.module.css";
3+
4+
export function AppNav() {
5+
return (
6+
<nav class={`app-nav ${styles.nav}`}>
7+
<span>Fresh</span>
8+
</nav>
9+
);
10+
}

packages/plugin-vite/demo/routes/_app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PageProps } from "fresh";
2+
import { AppNav } from "../islands/AppNav.tsx";
23

34
export default function App({ Component }: PageProps) {
45
return (
@@ -9,6 +10,7 @@ export default function App({ Component }: PageProps) {
910
<meta name="custom" content="foo" />
1011
</head>
1112
<body>
13+
<AppNav />
1214
<Component />
1315
</body>
1416
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { CssModules } from "../../islands/CssModules.tsx";
2+
3+
export default function Page2() {
4+
return (
5+
<div>
6+
<CssModules />
7+
<p class="page2-marker">page2</p>
8+
</div>
9+
);
10+
}

packages/plugin-vite/tests/build_test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,59 @@ integrationTest("vite build - css modules", async () => {
336336
);
337337
});
338338

339+
// Issue: https://github.com/denoland/fresh/issues/3633
340+
integrationTest(
341+
"vite build - css modules in _app.tsx island are injected",
342+
async () => {
343+
await launchProd(
344+
{ cwd: viteResult.tmp },
345+
async (address) => {
346+
await withBrowser(async (page) => {
347+
await page.goto(`${address}/tests/css_modules`, {
348+
waitUntil: "networkidle2",
349+
});
350+
351+
// The AppNav island is in _app.tsx and uses a CSS module.
352+
// Its styles should be injected even though the island
353+
// is discovered after <head> renders.
354+
const bgColor = await page
355+
.locator(".app-nav")
356+
// deno-lint-ignore no-explicit-any
357+
.evaluate((el) =>
358+
window.getComputedStyle(el as any).backgroundColor
359+
);
360+
expect(bgColor).toEqual("rgb(30, 30, 30)");
361+
});
362+
},
363+
);
364+
},
365+
);
366+
367+
// Issue: https://github.com/denoland/fresh/issues/3633
368+
integrationTest(
369+
"vite build - css modules work on second page with shared island",
370+
async () => {
371+
await launchProd(
372+
{ cwd: viteResult.tmp },
373+
async (address) => {
374+
await withBrowser(async (page) => {
375+
// Access the second page that shares the CssModules island
376+
await page.goto(`${address}/tests/css_modules_page2`, {
377+
waitUntil: "networkidle2",
378+
});
379+
380+
// The shared CssModules island's CSS should work here too
381+
const color = await page
382+
.locator(".red > h1")
383+
// deno-lint-ignore no-explicit-any
384+
.evaluate((el) => window.getComputedStyle(el as any).color);
385+
expect(color).toEqual("rgb(255, 0, 0)");
386+
});
387+
},
388+
);
389+
},
390+
);
391+
339392
integrationTest("vite build - route css import", async () => {
340393
await launchProd(
341394
{ cwd: viteResult.tmp },

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,53 @@ integrationTest("vite dev - css modules", async () => {
235235
});
236236
});
237237

238+
// Issue: https://github.com/denoland/fresh/issues/3633
239+
integrationTest(
240+
"vite dev - css modules in _app.tsx island are injected",
241+
async () => {
242+
await withBrowser(async (page) => {
243+
await page.goto(`${demoServer.address()}/tests/css_modules`, {
244+
waitUntil: "networkidle2",
245+
});
246+
247+
await waitFor(async () => {
248+
const bgColor = await page
249+
.locator(".app-nav")
250+
// deno-lint-ignore no-explicit-any
251+
.evaluate((el) => window.getComputedStyle(el as any).backgroundColor);
252+
expect(bgColor).toEqual("rgb(30, 30, 30)");
253+
return true;
254+
});
255+
});
256+
},
257+
);
258+
259+
// Issue: https://github.com/denoland/fresh/issues/3633
260+
integrationTest(
261+
"vite dev - css modules work on second page with shared island",
262+
async () => {
263+
await withBrowser(async (page) => {
264+
// First visit a different page, then visit page2 that shares
265+
// the CssModules island. In dev mode, the CSS must still work.
266+
await page.goto(`${demoServer.address()}/`, {
267+
waitUntil: "networkidle2",
268+
});
269+
await page.goto(`${demoServer.address()}/tests/css_modules_page2`, {
270+
waitUntil: "networkidle2",
271+
});
272+
273+
await waitFor(async () => {
274+
const color = await page
275+
.locator(".red > h1")
276+
// deno-lint-ignore no-explicit-any
277+
.evaluate((el) => window.getComputedStyle(el as any).color);
278+
expect(color).toEqual("rgb(255, 0, 0)");
279+
return true;
280+
});
281+
});
282+
},
283+
);
284+
238285
integrationTest("vite dev - route css import", async () => {
239286
await withBrowser(async (page) => {
240287
await page.goto(`${demoServer.address()}/tests/css`, {

0 commit comments

Comments
 (0)