# Spy on methods in `bun test` (/docs/guides/test/spy-on)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 230 · updated: 2026-07-28 -->
Related: [Run your tests with the Bun test runner](/docs/guides/test/run-tests.md), [Run tests in watch mode with Bun](/docs/guides/test/watch-mode.md), [Migrate from Jest to Bun's test runner](/docs/guides/test/migrate-from-jest.md), [Mock functions in `bun test`](/docs/guides/test/mock-functions.md), [Set the system time in Bun's test runner](/docs/guides/test/mock-clock.md), [Use snapshot testing in `bun test`](/docs/guides/test/snapshot.md)

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

```ts
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 you've created the spy, use it in `expect` assertions about method calls.

{/* prettier-ignore */}

```ts
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 [Mocks](/test/mocks).
