bun-fetch-mock
v1.0.2
Published
A powerful fetch mocking library for Bun tests with TypeScript support
Readme
bun-fetch-mock
Dead simple fetch mocking for Bun tests.
Install
bun add -d bun-fetch-mockUsage
Initialize useFetchMock() inside describe(...), not inside test(...).
import { describe, test, expect } from "bun:test";
import { useFetchMock } from "bun-fetch-mock";
describe("api", () => {
const fetchMock = useFetchMock({ baseUrl: "https://api.example.com" });
test("mocks fetch", async () => {
fetchMock
.get("/users/1", { data: { id: 1, name: "Ada" }, once: true })
.get("/users/1", { status: 404, data: { error: "Not found" } });
const okRes = await fetch("https://api.example.com/users/1");
expect(await okRes.json()).toEqual({ id: 1, name: "Ada" });
const notFoundRes = await fetch("https://api.example.com/users/1");
expect(notFoundRes.status).toBe(404);
// Fails the test if any configured mock was not called.
fetchMock.assertAllMocksUsed();
});
});Dev
bun run test
bun run test:coverage
bun run type-checkLicense
MIT
