Guides
Testing
Unit and integration testing with Bun.
Testing
Since Bklar is built on Bun, you should use the native bun:test runner. It is incredibly fast and API-compatible with Jest.
Integration Testing
Bklar provides a helper method app.request(path, options) specifically designed for testing. It simulates a request without needing to start a TCP server.
Example
Create a file tests/app.test.ts:
import { describe, expect, it, beforeEach } from "bun:test";
import { Bklar } from "bklar";
describe("My API", () => {
let app: ReturnType<typeof Bklar>;
beforeEach(() => {
app = Bklar({ logger: false }); // Disable logger for clean output
app.get("/hello", (ctx) => ctx.json({ hello: "world" }));
});
it("should return hello world", async () => {
// 1. Simulate Request
const res = await app.request("/hello");
// 2. Assert Status
expect(res.status).toBe(200);
// 3. Assert Body
const body = await res.json();
expect(body).toEqual({ hello: "world" });
});
});Run it:
bun testMocking Services
You can use spyOn or standard dependency injection to mock database calls.
import { spyOn } from "bun:test";
import db from "./db";
it("should handle db errors", async () => {
const spy = spyOn(db, "findUser").mockRejectedValue(new Error("DB Down"));
const res = await app.request("/users/1");
expect(res.status).toBe(500);
spy.mockRestore();
});