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

mocks cannot be reset #1940

Closed
6 tasks done
OmgImAlexis opened this issue Aug 31, 2022 · 4 comments · Fixed by #1947
Closed
6 tasks done

mocks cannot be reset #1940

OmgImAlexis opened this issue Aug 31, 2022 · 4 comments · Fixed by #1947

Comments

@OmgImAlexis
Copy link

Describe the bug

Before #1726 I was able to reset my module that I had mocked using vi.resetModules in a beforeEach handler.

After updating my tests now fail. Looking at the docs I've tried all of the following with none of them letting me reset the mock to its original mock value.

vi.resetModules();
vi.resetAllMocks();
vi.restoreAllMocks();
vi.clearAllMocks();

Reproduction

https://stackblitz.com/edit/node-vzowau?file=test/basic.test.ts

System Info

System:
    OS: Windows 10 10.0.25188
    CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
    Memory: 2.65 GB / 15.92 GB
  Binaries:
    Node: 18.3.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.11.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.25188.1000.0), Chromium (104.0.1293.54)
    Internet Explorer: 11.0.25188.1000
  npmPackages:
    vitest: ^0.22.1 => 0.22.1

Used Package Manager

npm

Validations

@PatrickChen928
Copy link
Contributor

vi.resetModules() had filtered out mocks intentionally. Maybe could add a param resetMocks to vi.resetModules()?🤔

@OmgImAlexis
Copy link
Author

@ChpShy yep got that, just wasn't sure on what the workaround is as the PR didn't state one. 🤔

@sheremet-va
Copy link
Member

@ChpShy yep got that, just wasn't sure on what the workaround is as the PR didn't state one. 🤔

Use vi.unmock (or vi.doUnmock, which is not hoisted) to unmock modules.

@sheremet-va
Copy link
Member

sheremet-va commented Aug 31, 2022

I now see that unmock doesn't remove cache, but it should, so there is a bug after all.

You can also, instead of directly changing object properties, spy on them, and then call vi.restoreAllMocks:

vi.spyOn(data, 'state', 'get').mockReturnValue('STOPPED')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants