chore: speed up tests (#1791)

This PR speeds up tests by handling custom created `Requests` object
directly instead of binding a socket and making a real HTTP request. For
most tests this is fine, and for those that aren't the existing testing
methods are all preserved.
This commit is contained in:
Marvin Hagemeister
2023-09-18 18:44:22 +02:00
committed by GitHub
parent c76f15479f
commit 3850131063
12 changed files with 247 additions and 158 deletions
+19 -20
View File
@@ -1,18 +1,17 @@
import { assertEquals } from "$std/testing/asserts.ts";
import {
assertTextMany,
fetchHtml,
parseHtml,
waitForText,
withFresh,
withFakeServe,
withPageName,
} from "./test_utils.ts";
Deno.test("applies only _layout file of one group", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/route-groups`);
async (server) => {
const doc = await server.getHtml(`/route-groups`);
assertTextMany(doc, "p", ["Foo layout", "Foo page"]);
},
@@ -20,20 +19,20 @@ Deno.test("applies only _layout file of one group", async () => {
});
Deno.test("applies only _layout files in parent groups", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/route-groups/baz`);
async (server) => {
const doc = await server.getHtml(`/route-groups/baz`);
assertTextMany(doc, "p", ["Bar layout", "Baz layout", "Baz page"]);
},
);
});
Deno.test("applies only _layout files in parent groups #2", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/route-groups/boof`);
async (server) => {
const doc = await server.getHtml(`/route-groups/boof`);
assertTextMany(doc, "p", ["Bar layout", "Boof Page"]);
},
);
@@ -52,10 +51,10 @@ Deno.test("can co-locate islands inside routes folder", async () => {
});
Deno.test("does not treat files in (_islands) as routes", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture/main.ts",
async (address) => {
const res = await fetch(`${address}/route-groups-islands/invalid`);
async (server) => {
const res = await server.get(`/route-groups-islands/invalid`);
assertEquals(res.status, 404);
await res.body?.cancel();
},
@@ -63,10 +62,10 @@ Deno.test("does not treat files in (_islands) as routes", async () => {
});
Deno.test("does not treat files in (_...) as routes", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture/main.ts",
async (address) => {
const res = await fetch(`${address}/route-groups-islands/sub`);
async (server) => {
const res = await server.get(`/route-groups-islands/sub`);
assertEquals(res.status, 404);
await res.body?.cancel();
},
@@ -74,10 +73,10 @@ Deno.test("does not treat files in (_...) as routes", async () => {
});
Deno.test("resolve index route in group /(group)/index.tsx", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_group_index/main.ts",
async (address) => {
const res = await fetch(`${address}`);
async (server) => {
const res = await server.get(`/`);
assertEquals(res.status, 200);
const doc = parseHtml(await res.text());
assertEquals(doc.querySelector("h1")?.textContent, "it works");