Skip to content

Commit

Permalink
Add basic tests (#39)
Browse files Browse the repository at this point in the history
- end-to-end build & dev in CI
- better type-checking

Related to #35
  • Loading branch information
mxdvl authored May 2, 2023
2 parents 2bdf667 + 52cf599 commit e120fb9
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 191 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ jobs:

- name: Lint
run: deno lint

- name: Test
run: deno test -A --no-check

- name: Check
run: deno check src/**.ts
196 changes: 11 additions & 185 deletions deno.lock

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions src/build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import { build } from "./build.ts";

const base = "mononykus/";
const site_dir = "src/_site";

Deno.test({
name: "Able to build the current project",
fn: async () => {
await build({ base, site_dir });
},
});

Deno.test({
name: "Able to develop the current project",
fn: async () => {
const command = new Deno.Command(
Deno.execPath(),
{
args: [
"task",
"dev",
],
stdout: "null",
stderr: "null",
},
);

const process = command.spawn();

await new Promise<void>((resolve) => {
const check_if_port_is_open = async () => {
try {
const { status } = await fetch("http://localhost:4507/mononykus/", {
method: "HEAD",
});
if (status === 200) resolve();
} catch (_) {
setTimeout(check_if_port_is_open, 60);
}
};
return check_if_port_is_open();
});

const response = await fetch("http://localhost:4507/mononykus/");

const html = await response.text();

process.kill();
await process.output();

assert(html.startsWith(
"<!DOCTYPE html>",
));
assert(html.includes(
"<title>Mononykus – Deno + Svelte</title>",
));
},
sanitizeResources: true,
});
4 changes: 2 additions & 2 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const build = async (
const out_dir = slashify(_out_dir);
const site_dir = slashify(_site_dir);

clean(out_dir);
await clean(out_dir);

await rebuild({ base, out_dir, site_dir, minify });

Expand All @@ -152,7 +152,7 @@ export const watch = async (
const out_dir = slashify(_out_dir);
const site_dir = slashify(_site_dir);

clean(out_dir);
await clean(out_dir);

const _rebuild = () => rebuild({ base, out_dir, site_dir, minify });

Expand Down
1 change: 1 addition & 0 deletions src/esbuild_plugins/get_route_html.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error -- the package is untyped
import { format } from "npm:prettier";

interface TemplateOptions {
Expand Down
11 changes: 7 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Handler } from "https://deno.land/[email protected]/http/server.ts";
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts";
import { normalize as normalise } from "https://deno.land/[email protected]/path/posix.ts";

interface ServerOptions {
base?: string;
base: string;
out_dir: string;
}

export const create_handler = (
{ base = "", out_dir }: ServerOptions,
{ base, out_dir }: ServerOptions,
): Handler => ((req) => {
const url = new URL(req.url);

if (url.pathname.startsWith("/" + base)) {
if (url.pathname.startsWith(normalise("/" + base))) {
return serveDir(req, { fsRoot: out_dir, urlRoot: base });
} else {
return Response.redirect(new URL(base + url.pathname, url.origin));
return Response.redirect(
new URL(normalise(base + url.pathname), url.origin),
);
}
});

0 comments on commit e120fb9

Please sign in to comment.