Test an HTTP application in memory
Send a standard Request directly to app.handler. This exercises routing, middleware, argument validation, dependency injection and response conversion. The test can inspect the same status, headers and body a network client would receive.
Test a write followed by a read
Place catalog_test.ts next to the two files from the writable catalog. Run deno test catalog_test.ts.
import assert from "node:assert/strict";
import { createApp } from "./app.ts";
Deno.test("a created book is readable at its Location", async () => {
const app = await createApp();
try {
const url = "http://library/api/books";
const created = await app.handler(new Request(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ title: " The Odyssey " }),
}));
assert.equal(created.status, 201);
const location = created.headers.get("location");
assert.ok(location);
const found = await app.handler(new Request(new URL(location, url)));
assert.equal(found.status, 200);
const book = await found.json();
assert.equal(book.title, "The Odyssey");
assert.deepEqual(book, await created.json());
const invalid = await app.handler(new Request(url, {
method: "POST",
body: JSON.stringify({ title: " " }),
}));
assert.equal(invalid.status, 400);
assert.equal((await invalid.json()).key, "title");
} finally {
await app.close();
}
});The test reads the resource through the URL returned by the server. A broken Location therefore fails the test even when the store inserted the book. A fresh application per test keeps data isolated.
Replace an external dependency
When a provider normally opens a database or calls an upstream API, replace its token during creation. For the catalog example:
import { ChoWeb, provider } from "@chojs/web";
import { Library } from "./app.ts";
import { Catalog } from "./catalog.ts";
const fixture = new Catalog();
const book = fixture.add("The Iliad");
const app = await ChoWeb.create(Library, {
prefix: "api",
overrides: [provider({ provide: Catalog, factory: () => fixture })],
});
try {
const response = await app.handler(new Request("http://library/api/books/" + book.id));
console.log(response.status); // 200
} finally {
await app.close();
}Select the test boundary
Use the ordinary application and linker when testing route precedence or a not-found response. Use the adapter's native test facility when asserting native plugin behavior. Use a listening server for transport concerns such as client disconnects and connection draining. Fastify tests · Shutdown