Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of behavior with restoreMocks and vi.mock #3410

Closed
6 tasks done
cschwebk opened this issue May 19, 2023 · 2 comments · Fixed by #3438
Closed
6 tasks done

Change of behavior with restoreMocks and vi.mock #3410

cschwebk opened this issue May 19, 2023 · 2 comments · Fixed by #3438
Assignees

Comments

@cschwebk
Copy link

Describe the bug

With the release of vitest@0.31.1, there was a change of behavior with the combination of vi.mock and the restoreMocks configuration option that I do not think was intended. The following will (hopefully) help to explain what is shown in the reproduction (all of the snippets below are taken from the reproduction).

In my codebase, we have the restoreMocks option enabled and use a pattern like this (contrived) example:

import { describe, expect, it, vi } from "vitest";
import { double } from "./double"; // the module containing logic I want to test
import { multiply } from "./multiply"; // a separate module that my module-under-test uses, and that i want to mock in this test

vi.mock("./multiply");

describe("double - mocked mock", () => {
  it("should double 4", () => { 
    const mockMultiply = vi.fn();
    vi.mocked(multiply).mockImplementation(mockMultiply).mockReturnValue(12);

    expect(double(4)).toEqual(8);
  });
});

With restoreMocks: true in the configuration, versions prior to 0.31.1 worked as expected, and this test would fail. After installing 0.31.1, the mockImplementation on multiply is seemingly no longer being used, and the test passes. If I change restoreMocks to false, the test starts failing with 0.31.1 as well.

The reason I think this is unintended behavior is because providing a factory to the vi.mock call results in (expected) test failures under both 0.31.1 and 0.31.0, with both restoreMocks: true and restoreMocks: false

import { describe, expect, it, vi } from "vitest";
import { double } from "./double";
import { multiply } from "./multiply";

vi.mock("./multiply", () => ({
  multiply: vi.fn(),
}));

describe("double - factory mock", () => {
  it("should double 4", () => {
    vi.mocked(multiply).mockReturnValue(12);

    expect(double(4)).toEqual(8);
  });
});

Using my pattern with the directly imported module also fails tests as expected under both versions and with both settings of restoreMocks.

import { describe, expect, it, vi } from "vitest";
import { double } from "./double";

vi.mock("./double");

describe("double - direct mock", () => {
  it("should double 4", () => {
    const mockDouble = vi.fn();
    vi.mocked(double).mockImplementation(mockDouble).mockReturnValue(12);

    expect(double(4)).toEqual(8);
  });
});

My expectation is that my original pattern should continue to work in 0.31.1 as it did in prior versions with restoreMocks: true.

Reproduction

https://stackblitz.com/edit/node-pvnkfw

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 253.94 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.19.1 - ~/.nvm/versions/node/v16.19.1/bin/node
    npm: 8.19.3 - ~/.nvm/versions/node/v16.19.1/bin/npm
  Browsers:
    Chrome: 113.0.5672.126
    Firefox: 112.0.2
    Safari: 16.4
  npmPackages:
    vite: latest => 4.3.8
    vitest: 0.31.1 => 0.31.1

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented May 19, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@throrin19
Copy link

Same problem here with the version 0.31.1. When will the patch be released?

@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants