diff --git a/library/sources/Hono.test.ts b/library/sources/Hono.test.ts index 61b0ef23..afd3e05f 100644 --- a/library/sources/Hono.test.ts +++ b/library/sources/Hono.test.ts @@ -12,6 +12,10 @@ import { isLocalhostIP } from "../helpers/isLocalhostIP"; import { createTestAgent } from "../helpers/createTestAgent"; import { addHonoMiddleware } from "../middleware/hono"; import * as fetch from "../helpers/fetch"; +import { + contextStorage as honoContextStorage, + getContext as getHonoContext, +} from "hono/context-storage"; wrap(fetch, "fetch", function mock(original) { return async function mock(this: typeof fetch) { @@ -62,11 +66,20 @@ const agent = createTestAgent({ }); agent.start([new HonoInternal(), new HTTPServer()]); +type Env = { + Variables: { + testProp: string; + }; +}; + function getApp() { const { Hono } = require("hono") as typeof import("hono"); - const app = new Hono(); + const app = new Hono(); + + app.use(honoContextStorage()); app.use(async (c, next) => { + c.set("testProp", "test-value"); if (c.req.path.startsWith("/user/blocked")) { setUser({ id: "567" }); } else if (c.req.path.startsWith("/user")) { @@ -89,6 +102,15 @@ function getApp() { return c.text("OK"); }); + // Access async context outside of handler + const getTestProp = () => { + return getHonoContext().var.testProp; + }; + + app.get("/hono-async-context", (c) => { + return c.text(getTestProp()); + }); + return app; } @@ -335,3 +357,12 @@ t.test("ip blocking works (real socket)", opts, async (t) => { // Cleanup server server.close(); }); + +t.test("The hono async context still works", opts, async (t) => { + const response = await getApp().request("/hono-async-context", { + method: "GET", + }); + + const body = await response.text(); + t.equal(body, "test-value"); +});