From 26189cfdf35efc054cd832521e5ae08f545d5d54 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 2 Jun 2021 01:16:31 +0200 Subject: [PATCH] tests: add basic integration tests (#13) --- .github/workflows/ci.yml | 4 +-- src/server/mod.ts | 2 +- tests/deps.ts | 5 +++ tests/fixture/README.md | 21 +++++++++++++ tests/fixture/api_deps.ts | 1 + tests/fixture/deps.ts | 1 + tests/fixture/main.ts | 4 +++ tests/fixture/pages/[name].tsx | 9 ++++++ tests/fixture/pages/api/name.ts | 5 +++ tests/fixture/pages/index.tsx | 9 ++++++ tests/fixture/routes.gen.ts | 18 +++++++++++ tests/fixture/tsconfig.json | 6 ++++ tests/main_test.ts | 55 +++++++++++++++++++++++++++++++++ 13 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 tests/deps.ts create mode 100644 tests/fixture/README.md create mode 100644 tests/fixture/api_deps.ts create mode 100644 tests/fixture/deps.ts create mode 100644 tests/fixture/main.ts create mode 100644 tests/fixture/pages/[name].tsx create mode 100644 tests/fixture/pages/api/name.ts create mode 100644 tests/fixture/pages/index.tsx create mode 100644 tests/fixture/routes.gen.ts create mode 100644 tests/fixture/tsconfig.json create mode 100644 tests/main_test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2819c47e..43183ad9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,5 +35,5 @@ jobs: - name: Cache dependencies run: deno cache --no-check src/*/deps.ts -# - name: Run tests -# run: deno test -A --unstable + - name: Run tests + run: deno test -A --unstable --config ./tsconfig.json --no-check diff --git a/src/server/mod.ts b/src/server/mod.ts index 4f3ff34f..cc2b6d9d 100644 --- a/src/server/mod.ts +++ b/src/server/mod.ts @@ -13,7 +13,7 @@ export interface Routes { baseUrl: string; } -export { installRoutes, oak }; +export { installRoutes, oak, processRoutes }; export function start(routes: Routes) { const [pages, apiRoutes] = processRoutes(routes); diff --git a/tests/deps.ts b/tests/deps.ts new file mode 100644 index 00000000..288b5447 --- /dev/null +++ b/tests/deps.ts @@ -0,0 +1,5 @@ +export { + assert, + assertEquals, + assertStringIncludes, +} from "https://deno.land/std@0.97.0/testing/asserts.ts"; diff --git a/tests/fixture/README.md b/tests/fixture/README.md new file mode 100644 index 00000000..5ace786b --- /dev/null +++ b/tests/fixture/README.md @@ -0,0 +1,21 @@ +# fresh project + +### Usage + +Install deployctl: + +``` +deno install --allow-read --allow-write --allow-env --allow-net --allow-run --no-check -r -f https://deno.land/x/deploy/deployctl.ts +``` + +Start the project: + +``` +deployctl run --no-check --watch main.ts +``` + +After adding, removing, or moving a page in the `pages` directory, run: + +``` +fresh routes +``` diff --git a/tests/fixture/api_deps.ts b/tests/fixture/api_deps.ts new file mode 100644 index 00000000..0318a0be --- /dev/null +++ b/tests/fixture/api_deps.ts @@ -0,0 +1 @@ +export * from "../../server.ts"; diff --git a/tests/fixture/deps.ts b/tests/fixture/deps.ts new file mode 100644 index 00000000..3ccecce5 --- /dev/null +++ b/tests/fixture/deps.ts @@ -0,0 +1 @@ +export * from "../../runtime.ts"; diff --git a/tests/fixture/main.ts b/tests/fixture/main.ts new file mode 100644 index 00000000..f332138d --- /dev/null +++ b/tests/fixture/main.ts @@ -0,0 +1,4 @@ +import { start } from "../../server.ts"; +import routes from "./routes.gen.ts"; + +start(routes); diff --git a/tests/fixture/pages/[name].tsx b/tests/fixture/pages/[name].tsx new file mode 100644 index 00000000..db752c84 --- /dev/null +++ b/tests/fixture/pages/[name].tsx @@ -0,0 +1,9 @@ +import { h } from "../deps.ts"; + +interface Props { + params: Record; +} + +export default function Greet(props: Props) { + return
Hello {props.params.name}
; +} diff --git a/tests/fixture/pages/api/name.ts b/tests/fixture/pages/api/name.ts new file mode 100644 index 00000000..7f70b590 --- /dev/null +++ b/tests/fixture/pages/api/name.ts @@ -0,0 +1,5 @@ +import { oak } from "../../api_deps.ts"; + +export default (ctx: oak.Context) => { + ctx.response.body = { name: "fresh" }; +}; diff --git a/tests/fixture/pages/index.tsx b/tests/fixture/pages/index.tsx new file mode 100644 index 00000000..a9fc6125 --- /dev/null +++ b/tests/fixture/pages/index.tsx @@ -0,0 +1,9 @@ +import { h, IS_BROWSER, useState } from "../deps.ts"; + +export default function Home() { + return ( +
+

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

+
+ ); +} diff --git a/tests/fixture/routes.gen.ts b/tests/fixture/routes.gen.ts new file mode 100644 index 00000000..be1f5d39 --- /dev/null +++ b/tests/fixture/routes.gen.ts @@ -0,0 +1,18 @@ +// DO NOT EDIT. This file is generated by `fresh`. +// This file SHOULD be checked into source version control. +// To update this file, run `fresh routes`. + +import * as $0 from "./pages/api/name.ts"; +import * as $1 from "./pages/[name].tsx"; +import * as $2 from "./pages/index.tsx"; + +const routes = { + pages: { + "./pages/api/name.ts": $0, + "./pages/[name].tsx": $1, + "./pages/index.tsx": $2, + }, + baseUrl: import.meta.url, +}; + +export default routes; diff --git a/tests/fixture/tsconfig.json b/tests/fixture/tsconfig.json new file mode 100644 index 00000000..798d070e --- /dev/null +++ b/tests/fixture/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "jsxFactory": "h", + "jsxFragmentFactory": "Fragment" + } +} diff --git a/tests/main_test.ts b/tests/main_test.ts new file mode 100644 index 00000000..fc2df8b7 --- /dev/null +++ b/tests/main_test.ts @@ -0,0 +1,55 @@ +import { installRoutes, oak, processRoutes } from "../server.ts"; +import { assert, assertEquals, assertStringIncludes } from "./deps.ts"; +import routes from "./fixture/routes.gen.ts"; + +const router = new oak.Router(); +installRoutes(router, ...processRoutes(routes)); +const app = new oak.Application(); +app.use(router.routes()); +app.use(router.allowedMethods()); + +Deno.test("/ page prerender", async () => { + const resp = await app.handle(new Request("https://fresh.deno.dev/")); + assert(resp); + assertEquals(resp.status, 200); + assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); + const body = await resp.text(); + assertStringIncludes(body, "index.js"); + assertStringIncludes(body, "

Viewing JIT render.

"); + assertStringIncludes( + body, + ``, + ); +}); + +Deno.test("/[name] page prerender", async () => { + const resp = await app.handle(new Request("https://fresh.deno.dev/foo")); + assert(resp); + assertEquals(resp.status, 200); + assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); + const body = await resp.text(); + assertStringIncludes(body, "[name].js"); + assertStringIncludes(body, "
Hello foo
"); + assertStringIncludes( + body, + ``, + ); +}); + +Deno.test("/api/name", async () => { + const resp = await app.handle(new Request("https://fresh.deno.dev/api/name")); + assert(resp); + assertEquals(resp.status, 200); + assertEquals( + resp.headers.get("content-type"), + "application/json; charset=utf-8", + ); + const body = await resp.json(); + assertEquals(body, { name: "fresh" }); +}); + +Deno.test("/api/xyz not found", async () => { + const resp = await app.handle(new Request("https://fresh.deno.dev/api/xyz")); + assert(resp); + assertEquals(resp.status, 404); +});