# Bun Spy on methods in `bun test`

> Spy on methods in `bun test`

- Skill: `jarle/bun-spy-on-methods-in-bun-test` (Agent Skill)
- Install (CLI): `npx skillmds@latest add jarle/bun-spy-on-methods-in-bun-test`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jarle/bun-spy-on-methods-in-bun-test/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: jarle (https://skillmd.com/u/jarle)
- Updated: 2026-09-21
- Page: https://skillmd.com/skills/jarle/bun-spy-on-methods-in-bun-test

---


# Spy on methods in `bun test`

Use the `spyOn` utility to track method calls with Bun's test runner.

```ts  theme={"theme":{"light":"github-light","dark":"dracula"}}
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.

```ts  theme={"theme":{"light":"github-light","dark":"dracula"}}
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](/test/mocks) for complete documentation on mocking with the Bun test runner.

