Skip to content

mock and spyOn are not reset after each test #6040

@joshivaibhavs

Description

@joshivaibhavs

What version of Bun is running?

1.0.3+25e69c71e70ac8a0a88f9cf15b4057bd7b2a633a

What platform is your computer?

Linux 5.15.0-83-generic x86_64 x86_64

What steps can reproduce the bug?

  1. Create a file MyModule.ts
  2. Export a class MyClass from the module with following structure:
export class MyClass {
    myMethod() {
        return 'Hello';
    }
}
  1. Create a file MyModule.test.ts
  2. Write a test that tests the myMethod
test('myMethod return value', () => {
    const value = new MyClass().myMethod();
    expect(value).toEqual('Hello');
})
  1. Create another file OtherModule.test.ts
  2. Mock / spyOn the myMethod.
spyOn(MyClass.prototype, 'myMethod').mockImplementation(() => 'Hola');
  1. The myMethod return value test starts failing like this:
error: expect(received).toEqual(expected)

Expected: "Hello"
Received: "Hola"

What is the expected behavior?

The mocks / spyOns should be contained within individual test suites.

What do you see instead?

The mock is being applied across the test suites. All implementations are now mocked. Also, mockRestore doesn't seem to work.

Additional information

No response

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions