Skip to content

When restoreMocks is true functions in auto-mocked files are being restored to their original implementation #3503

@HristoKolev

Description

@HristoKolev

Describe the bug

When you call vi.restoreAllMocks(); or have restoreMocks set to true this restores functions in modules that are mocked like this vi.mock('./multiply');. to their original implementation.

That behavior is not documented here https://vitest.dev/guide/mocking.html#automocking-algorithm and is possibly not intended.

This certainly breaks compatibility with jest where restoring all mocks doesn't touch auto-generated modules.

Reproduction

https://stackblitz.com/edit/node-rnge8j?file=mock.test.ts

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (32) x64 AMD Ryzen 9 7950X 16-Core Processor
    Memory: 15.75 GB / 31.16 GB
  Binaries:
    Node: 18.12.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files\nodejs\yarn.CMD
    npm: 9.2.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1702.0), Chromium (113.0.1774.57)
    Internet Explorer: 11.0.22621.1

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions