Skip to content

Commit 541aef5

Browse files
committed
Add context data tests for @fedify/elysia
This commit includes the following changes: - Added two tests with a minimal fake federation: fedify() calls the context data factory when handling a request, and the factory's return value reaches federation.fetch() as its contextData option. - Added the test and test:bun scripts to package.json because `mise run test-each elysia` did not pick up the new tests without them. The tests now run on Node.js and Bun, the runtimes Elysia targets. Assisted-by: Claude Code:claude-fable-5
1 parent 90fdf19 commit 541aef5

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎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)