From e01ef2d8a0ae1f1737b6ef7a4b685ed7457cb994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Gr=C3=A4=C3=9Fl?= Date: Mon, 11 Sep 2023 16:05:55 +0200 Subject: [PATCH] feat(dev): Ignore common test files in routes and islands (#1039) Co-authored-by: Marvin Hagemeister Co-authored-by: Asher Gomez --- src/dev/dev_command.ts | 2 +- src/dev/mod.ts | 14 +++-- src/server/types.ts | 9 ++++ tests/deps.ts | 5 ++ tests/dev_test.ts | 51 +++++++++++++++++++ tests/fixture/islands/Counter.test.ts | 0 tests/fixture/islands/test_test.ts | 0 tests/fixture/routes/_404_test.tsx | 0 tests/fixture/routes/not_found.test.ts | 3 ++ tests/fixture_router_ignore_files/deno.json | 15 ++++++ tests/fixture_router_ignore_files/dev.ts | 6 +++ .../fixture_router_ignore_files/fresh.gen.ts | 15 ++++++ tests/fixture_router_ignore_files/main.ts | 11 ++++ tests/fixture_router_ignore_files/options.ts | 7 +++ .../routes/index.cy.ts | 0 .../routes/index.tsx | 9 ++++ 16 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 tests/dev_test.ts create mode 100644 tests/fixture/islands/Counter.test.ts create mode 100644 tests/fixture/islands/test_test.ts create mode 100644 tests/fixture/routes/_404_test.tsx create mode 100644 tests/fixture/routes/not_found.test.ts create mode 100644 tests/fixture_router_ignore_files/deno.json create mode 100755 tests/fixture_router_ignore_files/dev.ts create mode 100644 tests/fixture_router_ignore_files/fresh.gen.ts create mode 100644 tests/fixture_router_ignore_files/main.ts create mode 100644 tests/fixture_router_ignore_files/options.ts create mode 100644 tests/fixture_router_ignore_files/routes/index.cy.ts create mode 100644 tests/fixture_router_ignore_files/routes/index.tsx diff --git a/src/dev/dev_command.ts b/src/dev/dev_command.ts index 8e7c780a..222a2eb3 100644 --- a/src/dev/dev_command.ts +++ b/src/dev/dev_command.ts @@ -30,7 +30,7 @@ export async function dev( } else { currentManifest = { islands: [], routes: [] }; } - const newManifest = await collect(dir); + const newManifest = await collect(dir, options); Deno.env.set("FRSH_DEV_PREVIOUS_MANIFEST", JSON.stringify(newManifest)); const manifestChanged = diff --git a/src/dev/mod.ts b/src/dev/mod.ts index 834802f1..012707e7 100644 --- a/src/dev/mod.ts +++ b/src/dev/mod.ts @@ -1,7 +1,8 @@ import { gte, join, posix, relative, walk, WalkEntry } from "./deps.ts"; import { error } from "./error.ts"; - +import { FreshOptions } from "../server/mod.ts"; const MIN_DENO_VERSION = "1.31.0"; +const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/; export function ensureMinDenoVersion() { // Check that the minimum supported Deno version is being used. @@ -23,6 +24,7 @@ export function ensureMinDenoVersion() { async function collectDir( dir: string, callback: (entry: WalkEntry, dir: string) => void, + options: FreshOptions, ): Promise { // Check if provided path is a directory try { @@ -37,6 +39,7 @@ async function collectDir( includeDirs: false, includeFiles: true, exts: ["tsx", "jsx", "ts", "js"], + skip: [options?.router?.ignoreFilePattern || TEST_FILE_PATTERN], }); for await (const entry of routesFolder) { @@ -50,7 +53,10 @@ export interface Manifest { } const GROUP_REG = /[/\\\\]\((_[^/\\\\]+)\)[/\\\\]/; -export async function collect(directory: string): Promise { +export async function collect( + directory: string, + options: FreshOptions = {}, +): Promise { const filePaths = new Set(); const routes: string[] = []; @@ -78,11 +84,11 @@ export async function collect(directory: string): Promise { } filePaths.add(normalized); routes.push(rel); - }), + }, options), collectDir(join(directory, "./islands"), (entry, dir) => { const rel = join("islands", relative(dir, entry.path)); islands.push(rel); - }), + }, options), ]); routes.sort(); diff --git a/src/server/types.ts b/src/server/types.ts index 3051cac5..204f6007 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -19,6 +19,15 @@ export interface RouterOptions { * @default {false} */ trailingSlash?: boolean; + /** + * Configures the pattern of files to ignore in islands and routes. + * + * By default Fresh will ignore test files, + * for example files with a `.test.ts` or a `_test.ts` suffix. + * + * @default {/(?:[^/]*_|[^/]*\.|)test\.(?:ts|tsx|mts|js|mjs|jsx|)\/*$/} + */ + ignoreFilePattern?: RegExp; } export type RenderFunction = ( diff --git a/tests/deps.ts b/tests/deps.ts index 3d31e8cf..8f9e68f1 100644 --- a/tests/deps.ts +++ b/tests/deps.ts @@ -32,3 +32,8 @@ export { export { defineConfig, type Preset } from "https://esm.sh/@twind/core@1.1.3"; export { default as presetTailwind } from "https://esm.sh/@twind/preset-tailwind@1.1.4"; export * as fs from "https://deno.land/std@0.195.0/fs/mod.ts"; +export { + dirname, + fromFileUrl, + join, +} from "https://deno.land/std@0.193.0/path/mod.ts"; diff --git a/tests/dev_test.ts b/tests/dev_test.ts new file mode 100644 index 00000000..e04f085f --- /dev/null +++ b/tests/dev_test.ts @@ -0,0 +1,51 @@ +import { collect } from "../src/dev/mod.ts"; +import { assert, dirname, fromFileUrl, join } from "./deps.ts"; + +Deno.test({ + name: "routes collect", + fn: async () => { + const { routes } = await collect( + join(dirname(fromFileUrl(import.meta.url)), "fixture"), + {}, + ); + + assert( + !routes.includes("routes/not_found.test.ts") && + !routes.includes("routes\\not_found.test.ts"), + ); + assert( + !routes.includes("routes/_404_test.tsx") && + !routes.includes("routes\\_404_test.tsx"), + ); + assert( + !routes.includes("routes/islands/test_test.tsx") && + !routes.includes("routes\\islands\\test_test.tsx"), + ); + }, +}); + +Deno.test({ + name: "routes collect with custom pattern", + fn: async () => { + const { routes } = await collect( + join( + dirname(fromFileUrl(import.meta.url)), + "fixture_router_ignore_files", + ), + { + router: { + ignoreFilePattern: /[\.|_]cy\.[t|j]s(x)?$/, + }, + }, + ); + + assert( + !routes.includes("routes/index.cy.ts") && + !routes.includes("routes\\index.cy.ts"), + ); + assert( + routes.includes("routes/index.tsx") || + routes.includes("routes\\index.tsx"), + ); + }, +}); diff --git a/tests/fixture/islands/Counter.test.ts b/tests/fixture/islands/Counter.test.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixture/islands/test_test.ts b/tests/fixture/islands/test_test.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixture/routes/_404_test.tsx b/tests/fixture/routes/_404_test.tsx new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixture/routes/not_found.test.ts b/tests/fixture/routes/not_found.test.ts new file mode 100644 index 00000000..6b409d8c --- /dev/null +++ b/tests/fixture/routes/not_found.test.ts @@ -0,0 +1,3 @@ +Deno.test("not really", () => { + return; +}); diff --git a/tests/fixture_router_ignore_files/deno.json b/tests/fixture_router_ignore_files/deno.json new file mode 100644 index 00000000..2b73c679 --- /dev/null +++ b/tests/fixture_router_ignore_files/deno.json @@ -0,0 +1,15 @@ +{ + "lock": false, + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.15.1", + "preact/": "https://esm.sh/preact@10.15.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.5", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.3.1" + }, + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_router_ignore_files/dev.ts b/tests/fixture_router_ignore_files/dev.ts new file mode 100755 index 00000000..3908da70 --- /dev/null +++ b/tests/fixture_router_ignore_files/dev.ts @@ -0,0 +1,6 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; +import options from "./options.ts"; + +await dev(import.meta.url, "./main.ts", options); diff --git a/tests/fixture_router_ignore_files/fresh.gen.ts b/tests/fixture_router_ignore_files/fresh.gen.ts new file mode 100644 index 00000000..b321c0e0 --- /dev/null +++ b/tests/fixture_router_ignore_files/fresh.gen.ts @@ -0,0 +1,15 @@ +// DO NOT EDIT. This file is generated by Fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import * as $0 from "./routes/index.tsx"; + +const manifest = { + routes: { + "./routes/index.tsx": $0, + }, + islands: {}, + baseUrl: import.meta.url, +}; + +export default manifest; diff --git a/tests/fixture_router_ignore_files/main.ts b/tests/fixture_router_ignore_files/main.ts new file mode 100644 index 00000000..1409a21d --- /dev/null +++ b/tests/fixture_router_ignore_files/main.ts @@ -0,0 +1,11 @@ +/// +/// +/// +/// +/// + +import { start } from "$fresh/server.ts"; +import routes from "./fresh.gen.ts"; +import options from "./options.ts"; + +await start(routes, options); diff --git a/tests/fixture_router_ignore_files/options.ts b/tests/fixture_router_ignore_files/options.ts new file mode 100644 index 00000000..3bba27a2 --- /dev/null +++ b/tests/fixture_router_ignore_files/options.ts @@ -0,0 +1,7 @@ +import { FreshOptions } from "$fresh/server.ts"; + +export default { + router: { + ignoreFilePattern: /[\.|_]cy\.[t|j]s(x)?$/, + }, +} as FreshOptions; diff --git a/tests/fixture_router_ignore_files/routes/index.cy.ts b/tests/fixture_router_ignore_files/routes/index.cy.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/fixture_router_ignore_files/routes/index.tsx b/tests/fixture_router_ignore_files/routes/index.tsx new file mode 100644 index 00000000..7d94ac5a --- /dev/null +++ b/tests/fixture_router_ignore_files/routes/index.tsx @@ -0,0 +1,9 @@ +import { IS_BROWSER } from "$fresh/runtime.ts"; + +export default function Home() { + return ( +
+

{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}

+
+ ); +}