# Mock functions in `bun test` (/docs/guides/test/mock-functions)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 379 · 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), [Spy on methods in `bun test`](/docs/guides/test/spy-on.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)

Create mocks with the `mock` function from `bun:test`.

```ts test.ts icon="/icons/typescript.svg"
import { test, expect, mock } from "bun:test";

const random = mock(() => Math.random());
```

***

The mock function can accept arguments.

```ts test.ts icon="/icons/typescript.svg"
import { test, expect, mock } from "bun:test";

const random = mock((multiplier: number) => multiplier * Math.random());
```

***

The result of `mock()` is a new function decorated with extra properties.

```ts test.ts icon="/icons/typescript.svg"
import { mock } from "bun:test";

const random = mock((multiplier: number) => multiplier * Math.random());

random(2);
random(10);

random.mock.calls;
// [[ 2 ], [ 10 ]]

random.mock.results;
//  [
//    { type: "return", value: 0.6533907460954099 },
//    { type: "return", value: 0.6452713933037312 }
//  ]
```

***

Use these properties to write `expect` assertions about how the mock was used: how many times it was called, with which arguments, and what it returned.

```ts test.ts icon="/icons/typescript.svg"
import { test, expect, mock } from "bun:test";

const random = mock((multiplier: number) => multiplier * Math.random());

test("random", async () => {
  const a = random(1);
  const b = random(2);
  const c = random(3);

  expect(random).toHaveBeenCalled();
  expect(random).toHaveBeenCalledTimes(3);
  expect(random.mock.calls).toEqual([[1], [2], [3]]);
  expect(random.mock.results[0]).toEqual({ type: "return", value: a });
});
```

***

See [Mocks](/test/mocks).
