Skip to main content
Bun’s test runner includes Jest-compatible mocking.

Mock Function

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);
});

Spy on Methods

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);
});
See Mocks for complete documentation.

Build docs developers (and LLMs) love