Bun’s test runner includes Jest-compatible mocking.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/zhcndoc/bun/llms.txt
Use this file to discover all available pages before exploring further.
Powered by Mintlify
Auto-generate your docs
Create and use mock functions in tests
Bun’s test runner includes Jest-compatible mocking.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/zhcndoc/bun/llms.txt
Use this file to discover all available pages before exploring further.
import { test, expect, mock } from "bun:test";
test("mock function", () => {
const fn = mock((x: number) => x * 2);
fn(5);
fn(10);
expect(fn).toHaveBeenCalledTimes(2);
expect(fn).toHaveBeenCalledWith(5);
});
import { test, expect, spyOn } from "bun:test";
const obj = {
method: (x: number) => x * 2
};
test("spy on method", () => {
const spy = spyOn(obj, "method");
obj.method(5);
expect(spy).toHaveBeenCalledWith(5);
});