vitest icon indicating copy to clipboard operation
vitest copied to clipboard

mock.instances should be populated only when new keyword is used

Open trivikr opened this issue 1 year ago • 8 comments

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

trivikr avatar Feb 07 '23 00:02 trivikr