skills/bun-guides-test-spy-on/SKILL.md
Spy on methods in `bun test`
npx skillsauth add jarle/bun-skills Bun Spy on methods in `bun test`Install this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
bun testUse the spyOn utility to track method calls with Bun's test runner.
import { test, expect, spyOn } from "bun:test";
const leo = {
name: "Leonardo",
sayHi(thing: string) {
console.log(`Sup I'm ${this.name} and I like ${thing}`);
},
};
const spy = spyOn(leo, "sayHi");
Once the spy is created, it can be used to write expect assertions relating to method calls.
import { test, expect, spyOn } from "bun:test";
const leo = {
name: "Leonardo",
sayHi(thing: string) {
console.log(`Sup I'm ${this.name} and I like ${thing}`);
},
};
const spy = spyOn(leo, "sayHi");
test("turtles", () => { // [!code ++]
expect(spy).toHaveBeenCalledTimes(0); // [!code ++]
leo.sayHi("pizza"); // [!code ++]
expect(spy).toHaveBeenCalledTimes(1); // [!code ++]
expect(spy.mock.calls).toEqual([["pizza"]]); // [!code ++]
}); // [!code ++]
See Docs > Test Runner > Mocks for complete documentation on mocking with the Bun test runner.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling