Skip to content

Commit 09cd70e

Browse files
authored
Merge pull request #969 from dktsudgg/852-elysia-context-data-test
Add context data tests for `@fedify/elysia`
2 parents 5936534 + 541aef5 commit 09cd70e

5 files changed

Lines changed: 118 additions & 55 deletions

File tree

‎deno.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"astro": "npm:astro@^7.0.0",
7878
"byte-encodings": "npm:byte-encodings@^1.0.11",
7979
"chalk": "npm:chalk@^5.6.2",
80+
"elysia": "npm:elysia@^1.3.8",
8081
"es-toolkit": "npm:es-toolkit@^1.46.1",
8182
"h3": "npm:h3@^1.15.0",
8283
"hono": "jsr:@hono/hono@^4.8.3",

‎deno.lock‎

Lines changed: 61 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/elysia/package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
"build:self": "tsdown",
4747
"build": "pnpm --filter @fedify/elysia... run build:self",
4848
"prepack": "pnpm build",
49-
"prepublish": "pnpm build"
49+
"prepublish": "pnpm build",
50+
"test": "node --experimental-transform-types --test",
51+
"test:bun": "bun test src/"
5052
},
5153
"peerDependencies": {
5254
"elysia": "^1.3.6",

‎packages/elysia/src/index.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Elysia } from "elysia";
2+
import { strict as assert } from "node:assert";
3+
import { describe, test } from "node:test";
4+
import { fedify } from "./index.ts";
5+
6+
interface MockFederation {
7+
fetch(request: Request, options: unknown): Promise<Response>;
8+
}
9+
10+
describe("[elysia] fedify() plugin", () => {
11+
test("fedify() calls the context data factory when handling a request", async () => {
12+
const elysia = new Elysia();
13+
const mockFederation: MockFederation = {
14+
fetch: () => Promise.resolve(new Response("OK")),
15+
};
16+
let count = 0;
17+
const mockContextDataFactory = () => {
18+
// increases count if this method called
19+
count++;
20+
};
21+
22+
elysia.use(fedify(mockFederation as never, mockContextDataFactory));
23+
await elysia.handle(new Request("http://localhost/"));
24+
25+
assert.strictEqual(count, 1, "the context data factory must be called");
26+
});
27+
28+
test("fedify() calls federation.fetch() with the context data", async () => {
29+
const elysia = new Elysia();
30+
const mockFederation: MockFederation = {
31+
fetch: (_req, opts) => {
32+
// return context data
33+
const contextData = (opts as { contextData: string }).contextData;
34+
return Promise.resolve(new Response(contextData));
35+
},
36+
};
37+
const mockContextDataFactory = () => "Hello World";
38+
39+
elysia.use(fedify(mockFederation as never, mockContextDataFactory));
40+
const actual = await elysia.handle(new Request("http://localhost/"))
41+
.then((res) => res.text());
42+
43+
assert.strictEqual(
44+
actual,
45+
"Hello World",
46+
"federation.fetch() must receive the context data returned by the factory",
47+
);
48+
});
49+
});

0 commit comments

Comments
 (0)