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
+50 -54
View File
@@ -3,70 +3,69 @@ import {
assertNotSelector,
assertSelector,
clickWhenListenerReady,
fetchHtml,
waitForText,
withFresh,
withFakeServe,
withPageName,
} from "./test_utils.ts";
Deno.test("apply root _layout and _app", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(address);
async (server) => {
const doc = await server.getHtml("/");
assert(doc.body.textContent?.includes("it works"));
assertSelector(doc, ".app .root-layout .home-page");
const doc2 = await fetchHtml(`${address}/other`);
const doc2 = await server.getHtml("/other");
assertSelector(doc2, ".app .root-layout .other-page");
},
);
});
Deno.test("apply sub layouts", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/foo`);
async (server) => {
const doc = await server.getHtml("/foo");
assertSelector(doc, ".app .root-layout .foo-layout .foo-page");
const doc2 = await fetchHtml(`${address}/foo/bar`);
const doc2 = await server.getHtml("/foo/bar");
assertSelector(doc2, ".app .root-layout .foo-layout .bar-page");
},
);
});
Deno.test("skip layouts if not present", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/skip/sub`);
async (server) => {
const doc = await server.getHtml(`/skip/sub`);
assertSelector(doc, ".app .root-layout .sub-layout .sub-page");
},
);
});
Deno.test("check file types", async (t) => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
async (server) => {
await t.step(".js", async () => {
const doc = await fetchHtml(`${address}/files/js`);
const doc = await server.getHtml(`/files/js`);
assertSelector(doc, ".app .root-layout .js-layout .js-page");
});
await t.step(".jsx", async () => {
const doc = await fetchHtml(`${address}/files/jsx`);
const doc = await server.getHtml(`/files/jsx`);
assertSelector(doc, ".app .root-layout .jsx-layout .jsx-page");
});
await t.step(".ts", async () => {
const doc = await fetchHtml(`${address}/files/ts`);
const doc = await server.getHtml(`/files/ts`);
assertSelector(doc, ".app .root-layout .ts-layout .ts-page");
});
await t.step(".tsx", async () => {
const doc = await fetchHtml(`${address}/files/tsx`);
const doc = await server.getHtml(`/files/tsx`);
assertSelector(doc, ".app .root-layout .tsx-layout .tsx-page");
});
},
@@ -74,20 +73,20 @@ Deno.test("check file types", async (t) => {
});
Deno.test("render async layout", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async`);
async (server) => {
const doc = await server.getHtml(`/async`);
assertSelector(doc, ".app .root-layout .async-layout .async-page");
},
);
});
Deno.test("render nested async layout", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async/sub`);
async (server) => {
const doc = await server.getHtml(`/async/sub`);
assertSelector(
doc,
".app .root-layout .async-layout .async-sub-layout .async-sub-page",
@@ -96,27 +95,24 @@ Deno.test("render nested async layout", async () => {
);
});
Deno.test({
name: "can return Response from async layout",
fn: async () => {
await withFresh(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/async/redirect`);
assertSelector(
doc,
".app .root-layout .async-layout .async-sub-layout .async-sub-page",
);
},
);
},
Deno.test("can return Response from async layout", async () => {
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (server) => {
const doc = await server.getHtml(`/async/redirect`);
assertSelector(
doc,
".app .root-layout .async-layout .async-sub-layout .async-sub-page",
);
},
);
});
Deno.test("disable _app layout", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override/no_app`);
async (server) => {
const doc = await server.getHtml(`/override/no_app`);
assertNotSelector(doc, "body body");
assertSelector(doc, "body > .override-layout >.no-app");
},
@@ -124,10 +120,10 @@ Deno.test("disable _app layout", async () => {
});
Deno.test("disable _app in _layout", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override/layout_no_app`);
async (server) => {
const doc = await server.getHtml(`/override/layout_no_app`);
assertNotSelector(doc, "body body");
assertSelector(doc, "body > .override-layout > .no-app-layout > .page");
},
@@ -135,30 +131,30 @@ Deno.test("disable _app in _layout", async () => {
});
Deno.test("override layouts", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override`);
async (server) => {
const doc = await server.getHtml(`/override`);
assertSelector(doc, "body > .app > .override-layout > .override-page");
},
);
});
Deno.test("route overrides layout", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override/no_layout`);
async (server) => {
const doc = await server.getHtml(`/override/no_layout`);
assertSelector(doc, "body > .app > .no-layouts");
},
);
});
Deno.test("route overrides layout and app", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_layouts/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/override/no_layout_no_app`);
async (server) => {
const doc = await server.getHtml(`/override/no_layout_no_app`);
assertSelector(doc, "body > .no-app-no-layouts");
},
);