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
+16 -16
View File
@@ -2,7 +2,7 @@ import { assertEquals } from "./deps.ts";
import {
assertSelector,
assertTextMany,
fetchHtml,
withFakeServe,
withFresh,
withPageName,
} from "./test_utils.ts";
@@ -12,10 +12,10 @@ Deno.test({
name: "render async server component",
async fn() {
await withFresh(
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}/basic`);
async (server) => {
const doc = await server.getHtml(`/basic`);
assertTextMany(doc, "h1", ["it works"]);
},
);
@@ -26,10 +26,10 @@ Deno.test({
name: "uses returned response",
async fn() {
await withFresh(
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (address) => {
const res = await fetch(`${address}/response`);
async (server) => {
const res = await server.get(`/response`);
const text = await res.text();
assertEquals(text, "it works");
},
@@ -104,10 +104,10 @@ Deno.test({
name: "can call context.renderNotFound()",
async fn() {
await withFresh(
await withFakeServe(
"./tests/fixture_server_components/main.ts",
async (address) => {
const res = await fetch(`${address}/fail`);
async (server) => {
const res = await server.get(`/fail`);
assertEquals(res.status, Status.NotFound);
const html = await res.text();
@@ -144,10 +144,10 @@ Deno.test({
name: "renders async app template",
async fn() {
await withFresh(
await withFakeServe(
"./tests/fixture_async_app/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}`);
async (server) => {
const doc = await server.getHtml(``);
assertSelector(doc, "html > body > .app > .layout > .page");
},
);
@@ -155,10 +155,10 @@ Deno.test({
});
Deno.test("define helpers", async () => {
await withFresh(
await withFakeServe(
"./tests/fixture_define_helpers/main.ts",
async (address) => {
const doc = await fetchHtml(`${address}`);
async (server) => {
const doc = await server.getHtml(``);
assertSelector(doc, "html > body > .app > .layout > .page");
assertTextMany(doc, "p", ["Layout: it works", "Page: it works"]);
},