Skip to content

Mocks aren't automatically reset between tests #5391

@alexey-yarmosh

Description

@alexey-yarmosh

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Darwin 22.4.0 x86_64 i386

What steps can reproduce the bug?

import { test, expect, mock } from "bun:test";
const random = mock(() => Math.random());

test("random", async () => {
  random();
  expect(random).toHaveBeenCalledTimes(1);
});

test("random2", async () => {
  random();
  expect(random).toHaveBeenCalledTimes(1);
});

What is the expected behavior?

random mock resets between tests and they are passing.

What do you see instead?

bun test v1.0.1 (31aec4eb)

index.test.js:
✓ random [0.32ms]
 6 |   expect(random).toHaveBeenCalledTimes(1);
 7 | });
 8 | 
 9 | test("random2", async () => {
10 |   random();
11 |   expect(random).toHaveBeenCalledTimes(1);
      ^
error: expect(received).not.toHaveBeenCalledTimes(expected)

Expected [
  [], []
]

      at /Users/baderfall/Documents/web/testing/sandbox/index.test.js:11:2
      at /Users/baderfall/Documents/web/testing/sandbox/index.test.js:9:16
✗ random2 [0.72ms]

 1 pass
 1 fail
 2 expect() calls
Ran 2 tests across 1 files. [16.00ms]

Additional information

As I understand there is no way to explicitly .reset() the mock too. So writing test seems blocked right now.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingbun:testSomething related to the `bun test` runner

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions