Ergonomic event mocking for SvelteKit server-side testing with a simple builder API.
bun add -d sveltekit-event-mock
pnpm add -D sveltekit-event-mock
import { mock } from "sveltekit-event-mock";
import { describe, test, expect } from "bun:test";
import { GET, POST } from "./+server";
describe("my handler", () => {
test("handles GET request with auth", async () => {
const event = mock
.get("/api/users")
.withHeader("Authorization", "Bearer token")
.withCookie("session", "user-session-id");
const result = await GET(event);
const data = await result.json();
expect(data.key).toEqual("123");
});
test("handles POST with JSON payload", async () => {
const event = mock.post("/api/items").json({ name: "New Item" });
const result = await GET(event);
const data = await result.json();
expect(data).toEqual({ name: "New Item" });
});
});
mock.get(url); // GET request
mock.post(url); // POST request
mock.put(url); // PUT request
mock.delete(url); // DELETE request
mock.patch(url); // PATCH request
mock.head(url); // HEAD request
mock.options(url); // OPTIONS request
mock
.post("/api/data")
.json({ key: "value" }) // JSON body
.text("plain text") // Plain text
.form({ field: "value", file }); // Form data
mock
.get("/api/data")
.withHeader("Authorization", "Bearer token")
.withHeaders({ "X-Custom": "value" })
.withCookie("session", "session-id")
.withCookies({ theme: "dark", lang: "en" });
mock
.get("/api/users/[userId]/posts/[postId]")
.withParam("userId", "123")
.withParam("postId", "456")
.withRoute("/api/users/[userId]/posts/[postId]");
mock
.get("/api/data")
.withLocals({ user: { id: 123 } } as App.Locals)
.withIsDataRequest(true)
.withIsSubRequest(false)
.withIsRemoteRequest(true)
.clientAddress("192.168.1.100")
.withPlatform(platform);
const mockFetch = async (url) => {
return new Response('{"data":"mocked"}', {
headers: { "Content-Type": "application/json" },
});
};
const event = mock.get("/api/internal").withFetch(mockFetch);
const response = await event.fetch("/api/external");
mock – Main builder object with HTTP method shortcutsMockRequestEventBuilder – Builder classMockCookies – Mock cookies implementationMockSpan – Mock span for tracingRequestEvent, Cookies, RouteParams, Span, TracingMIT