vitest
vitest copied to clipboard
mock.instances should be populated only when new keyword is used
Describe the bug
mock.instances gets populated even when new keyword is not used.
From documentation on mock.instances
This is an array containing all instances that were instantiated when mock was called with a
new
keyword
Reproduction
import { expect, test, vi } from "vitest";
test("no instances", () => {
const Car = vi.fn();
const car = Car(); // Note no "new" keyword
expect(Car.mock.instances).toHaveLength(1); // Should be "0"
const bindContext = { x: 1 };
const boundMockFn = Car.bind(bindContext);
boundMockFn();
expect(Car.mock.instances).toHaveLength(2); // Received "0"
const callContext = { x: 2 };
Car.call(callContext);
expect(Car.mock.instances).toHaveLength(3); // Received "0"
const applyContext = { x: 3 };
Car.apply(applyContext);
expect(Car.mock.instances).toHaveLength(4); // Received "0"
});
System Info
npmPackages:
vitest: ^0.28.4 => 0.28.4
Used Package Manager
yarn
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.