Ops & Advanced
Testing
Cara melakukan testing pada aplikasi BlizzardTS. π§ͺ
π§ͺ Testing
BlizzardTS didesain agar mudah di-test. Karena kita menggunakan Bun, kita bisa memanfaatkan test runner bawaan Bun (bun:test) yang super cepat dan kompatibel dengan Jest.
Setup Testing
Tidak perlu install apa-apa! bun:test sudah built-in.
Buat file test, misalnya tests/app.test.ts.
Integration Testing
Cara terbaik mengetes aplikasi web adalah dengan mengirim request sungguhan ke handler kamu.
import { describe, expect, it } from "bun:test";
import { Blizzard } from "blizzardts";
describe("Blizzard App", () => {
it("should return 200 OK", async () => {
const app = Blizzard();
app.get("/", (c) => c.text("Hello Test"));
// Simulasi Request
const req = new Request("http://localhost/");
const res = await app.fetch(req);
expect(res.status).toBe(200);
expect(await res.text()).toBe("Hello Test");
});
it("should handle JSON body", async () => {
const app = Blizzard();
app.post("/data", async (c) => {
const body = await c.req.json();
return c.json({ received: body });
});
const req = new Request("http://localhost/data", {
method: "POST",
body: JSON.stringify({ foo: "bar" }),
headers: { "Content-Type": "application/json" }
});
const res = await app.fetch(req);
const data = await res.json();
expect(res.status).toBe(200);
expect(data).toEqual({ received: { foo: "bar" } });
});
});Menjalankan Test
Cukup jalankan perintah:
bun testTip: Gunakan bun test --watch untuk menjalankan test secara otomatis setiap kali file berubah.n test --watch`.
Mocking Services
Jika kamu butuh mock database atau service lain, bun:test juga punya fitur mock.
import { mock } from "bun:test";
const db = {
findUser: mock(() => ({ id: 1, name: "Test User" }))
};
// ... test code ...