diff --git a/packages/fresh/src/constants.ts b/packages/fresh/src/constants.ts index e8b4991a..421232dd 100644 --- a/packages/fresh/src/constants.ts +++ b/packages/fresh/src/constants.ts @@ -12,3 +12,5 @@ export const WEEK = DAY * 7; export const ASSET_CACHE_BUST_KEY = "__frsh_c"; export const UPDATE_INTERVAL = DAY; + +export const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/; diff --git a/packages/fresh/src/dev/builder.ts b/packages/fresh/src/dev/builder.ts index d833176d..95b9fac2 100644 --- a/packages/fresh/src/dev/builder.ts +++ b/packages/fresh/src/dev/builder.ts @@ -25,7 +25,7 @@ import { parseDirPath } from "../config.ts"; import { pathToExportName, UniqueNamer } from "../utils.ts"; import { checkDenoCompilerOptions } from "./check.ts"; import { crawlFsItem } from "./fs_crawl.ts"; -import { UPDATE_INTERVAL } from "../constants.ts"; +import { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "../constants.ts"; export interface BuildOptions { /** @@ -104,8 +104,6 @@ export type ResolvedBuildConfig = Required> & { sourceMap?: FreshBundleOptions["sourceMap"]; }; -const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/; - // deno-lint-ignore no-explicit-any export class Builder { #transformer: FileTransformer; diff --git a/packages/fresh/src/dev/fs_crawl_test.ts b/packages/fresh/src/dev/fs_crawl_test.ts index c848bca3..fe55f049 100644 --- a/packages/fresh/src/dev/fs_crawl_test.ts +++ b/packages/fresh/src/dev/fs_crawl_test.ts @@ -18,3 +18,28 @@ Deno.test("walkDir - ", async () => { expect(files).toEqual(["foo/bar/baz.txt", "foo/bar.txt"]); }); + +Deno.test("walkDir - respects skip patterns", async () => { + const fs = createFakeFs({ + "routes/index.tsx": "foo", + "routes/index_test.tsx": "test", + "routes/about.tsx": "foo", + "routes/about.test.ts": "test", + "routes/api/users.ts": "foo", + "routes/api/users_test.ts": "test", + }); + + const files: string[] = []; + const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/; + + await walkDir(fs, "routes", (entry) => { + files.push(entry.path); + }, [TEST_FILE_PATTERN]); + + // Should only include non-test files + expect(files).toEqual([ + "routes/index.tsx", + "routes/about.tsx", + "routes/api/users.ts", + ]); +}); diff --git a/packages/fresh/src/fs_routes_test.tsx b/packages/fresh/src/fs_routes_test.tsx index 1332d0da..314ce058 100644 --- a/packages/fresh/src/fs_routes_test.tsx +++ b/packages/fresh/src/fs_routes_test.tsx @@ -1564,3 +1564,52 @@ Deno.test("fsRoutes - merge group methods", async () => { res = await server.post("/bar"); expect(await res.text()).toEqual("POST ok"); }); + +Deno.test("fsRoutes - ignores test files in routes folder", async () => { + const fs = createFakeFs({ + "routes/index.tsx": { + default: () => <>index, + }, + "routes/index_test.tsx": { + default: () => <>index_test, + }, + "routes/foo_test.ts": { + handler: () => new Response("foo_test"), + }, + "routes/bar.test.ts": { + handler: () => new Response("bar_test"), + }, + "routes/baz.test.tsx": { + default: () => <>baz_test, + }, + "routes/qux_test.js": { + handler: () => new Response("qux_test"), + }, + "routes/valid.tsx": { + default: () => <>valid, + }, + }); + + const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/; + const routeDir = path.join(fs.cwd(), "routes"); + const rawFiles = await crawlRouteDir( + fs, + routeDir, + [TEST_FILE_PATTERN], + () => {}, + ); + + // Only index.tsx and valid.tsx should be found, test files should be filtered out + expect(rawFiles.length).toEqual(2); + expect(rawFiles.some((f) => f.filePath.includes("index.tsx"))).toBe(true); + expect(rawFiles.some((f) => f.filePath.includes("valid.tsx"))).toBe(true); + + // Ensure none of the test files are included + expect(rawFiles.some((f) => f.filePath.includes("index_test.tsx"))).toBe( + false, + ); + expect(rawFiles.some((f) => f.filePath.includes("foo_test.ts"))).toBe(false); + expect(rawFiles.some((f) => f.filePath.includes("bar.test.ts"))).toBe(false); + expect(rawFiles.some((f) => f.filePath.includes("baz.test.tsx"))).toBe(false); + expect(rawFiles.some((f) => f.filePath.includes("qux_test.js"))).toBe(false); +}); diff --git a/packages/fresh/src/internals_dev.ts b/packages/fresh/src/internals_dev.ts index 20989a7f..e2d78b3d 100644 --- a/packages/fresh/src/internals_dev.ts +++ b/packages/fresh/src/internals_dev.ts @@ -12,4 +12,4 @@ export { export { specToName } from "./dev/builder.ts"; export { pathToSpec, UniqueNamer } from "./utils.ts"; export { updateCheck } from "./dev/update_check.ts"; -export { UPDATE_INTERVAL } from "./constants.ts"; +export { TEST_FILE_PATTERN, UPDATE_INTERVAL } from "./constants.ts"; diff --git a/packages/fresh/src/test_utils.ts b/packages/fresh/src/test_utils.ts index 70237b15..53441d62 100644 --- a/packages/fresh/src/test_utils.ts +++ b/packages/fresh/src/test_utils.ts @@ -102,8 +102,14 @@ export function serveMiddleware( export function createFakeFs(files: Record): FsAdapter { return { cwd: () => ".", - async *walk(_root) { + async *walk(_root, options) { + const skip = options?.skip ?? []; for (const file of Object.keys(files)) { + // Check if file matches any skip pattern + if (skip.some((pattern) => pattern.test(file))) { + continue; + } + const entry: WalkEntry = { isDirectory: false, isFile: true, diff --git a/packages/plugin-vite/src/mod.ts b/packages/plugin-vite/src/mod.ts index e9741539..cdbdc332 100644 --- a/packages/plugin-vite/src/mod.ts +++ b/packages/plugin-vite/src/mod.ts @@ -16,6 +16,7 @@ import { patches } from "./plugins/patches.ts"; import process from "node:process"; import { specToName, + TEST_FILE_PATTERN, UniqueNamer, UPDATE_INTERVAL, updateCheck, @@ -33,7 +34,7 @@ export function fresh(config?: FreshViteConfig): Plugin[] { clientEntry: config?.clientEntry ?? "client.ts", islandsDir: config?.islandsDir ?? "islands", routeDir: config?.routeDir ?? "routes", - ignore: config?.ignore ?? [], + ignore: config?.ignore ?? [TEST_FILE_PATTERN], islandSpecifiers: new Map(), namer: new UniqueNamer(), checkImports: config?.checkImports ?? [], diff --git a/packages/plugin-vite/tests/build_test.ts b/packages/plugin-vite/tests/build_test.ts index b538037c..f6fd35eb 100644 --- a/packages/plugin-vite/tests/build_test.ts +++ b/packages/plugin-vite/tests/build_test.ts @@ -566,3 +566,38 @@ Deno.test({ sanitizeOps: false, sanitizeResources: false, }); + +Deno.test({ + name: "vite build - excludes test files from routes", + fn: async () => { + const fixture = path.join(FIXTURE_DIR, "test_files_exclusion"); + await using res = await buildVite(fixture); + + // Verify that test files in routes/ are not bundled + const serverAssetsDir = path.join(res.tmp, "_fresh", "server", "assets"); + + // List all compiled route files + const files: string[] = []; + for await (const entry of Deno.readDir(serverAssetsDir)) { + if (entry.isFile && entry.name.startsWith("_fresh-route")) { + files.push(entry.name); + } + } + + // Should have index route but NOT test files + expect(files.some((f) => f.includes("_fresh-route___index"))).toBe(true); + expect(files.some((f) => f.includes("index_test"))).toBe(false); + expect(files.some((f) => f.includes("foo.test"))).toBe(false); + + // Verify no test pattern files at all + // Note: files with "_test" or ".test" in their name (not just a "tests" folder) + const testPatterns = ["_test.", "_test-", ".test."]; + for (const file of files) { + for (const pattern of testPatterns) { + expect(file.includes(pattern)).toBe(false); + } + } + }, + sanitizeOps: false, + sanitizeResources: false, +}); diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/deno.json b/packages/plugin-vite/tests/fixtures/test_files_exclusion/deno.json new file mode 100644 index 00000000..23242bc0 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/deno.json @@ -0,0 +1,8 @@ +{ + "imports": { + "fresh": "jsr:@fresh/core@^2.0.0", + "@fresh/plugin-vite": "jsr:@fresh/plugin-vite@^1.0.0", + "preact": "npm:preact@^10.27.2", + "preact/": "npm:/preact@^10.27.2/" + } +} diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/main.ts b/packages/plugin-vite/tests/fixtures/test_files_exclusion/main.ts new file mode 100644 index 00000000..287ca00a --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/main.ts @@ -0,0 +1,3 @@ +import { App } from "fresh"; + +export const app = new App().fsRoutes(); diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/foo.test.ts b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/foo.test.ts new file mode 100644 index 00000000..0e205193 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/foo.test.ts @@ -0,0 +1,4 @@ +// This test file should NOT be bundled into production +export const handler = () => { + return new Response("This should never be in production"); +}; diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index.tsx b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index.tsx new file mode 100644 index 00000000..cb8365e5 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index.tsx @@ -0,0 +1,3 @@ +export default function Home() { + return
ok
; +} diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index_test.tsx b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index_test.tsx new file mode 100644 index 00000000..870d8699 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/routes/index_test.tsx @@ -0,0 +1,4 @@ +// This test file should NOT be bundled into production +export default function TestRoute() { + return
This should never be in production
; +} diff --git a/packages/plugin-vite/tests/fixtures/test_files_exclusion/vite.config.ts b/packages/plugin-vite/tests/fixtures/test_files_exclusion/vite.config.ts new file mode 100644 index 00000000..727d49a2 --- /dev/null +++ b/packages/plugin-vite/tests/fixtures/test_files_exclusion/vite.config.ts @@ -0,0 +1,6 @@ +import { defineConfig } from "vite"; +import { fresh } from "@fresh/plugin-vite"; + +export default defineConfig({ + plugins: [fresh()], +});