tests: add basic integration tests (#13)
This commit is contained in:
@@ -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
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export {
|
||||
assert,
|
||||
assertEquals,
|
||||
assertStringIncludes,
|
||||
} from "https://deno.land/std@0.97.0/testing/asserts.ts";
|
||||
@@ -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
|
||||
```
|
||||
@@ -0,0 +1 @@
|
||||
export * from "../../server.ts";
|
||||
@@ -0,0 +1 @@
|
||||
export * from "../../runtime.ts";
|
||||
@@ -0,0 +1,4 @@
|
||||
import { start } from "../../server.ts";
|
||||
import routes from "./routes.gen.ts";
|
||||
|
||||
start(routes);
|
||||
@@ -0,0 +1,9 @@
|
||||
import { h } from "../deps.ts";
|
||||
|
||||
interface Props {
|
||||
params: Record<string, string | string[]>;
|
||||
}
|
||||
|
||||
export default function Greet(props: Props) {
|
||||
return <div>Hello {props.params.name}</div>;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { oak } from "../../api_deps.ts";
|
||||
|
||||
export default (ctx: oak.Context) => {
|
||||
ctx.response.body = { name: "fresh" };
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { h, IS_BROWSER, useState } from "../deps.ts";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"jsxFactory": "h",
|
||||
"jsxFragmentFactory": "Fragment"
|
||||
}
|
||||
}
|
||||
@@ -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, "<p>Viewing JIT render.</p>");
|
||||
assertStringIncludes(
|
||||
body,
|
||||
`<script id="__FRSH_PROPS" type="application/json">{"params":{}}</script>`,
|
||||
);
|
||||
});
|
||||
|
||||
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, "<div>Hello foo</div>");
|
||||
assertStringIncludes(
|
||||
body,
|
||||
`<script id="__FRSH_PROPS" type="application/json">{"params":{"name":"foo"}}</script>`,
|
||||
);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
Reference in New Issue
Block a user