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

vi.unmock does not unmock imitated module #2351

Closed
6 tasks done
mcous opened this issue Nov 19, 2022 · 2 comments
Closed
6 tasks done

vi.unmock does not unmock imitated module #2351

mcous opened this issue Nov 19, 2022 · 2 comments

Comments

@mcous
Copy link
Contributor

mcous commented Nov 19, 2022

Describe the bug

When mocking modules using vi.mock (or vi.doMock), if the module is replaced implicitly, a subsequent call to vi.unmock (or vi.doUnmock) will not unmock the module.

import {vi, afterEach, test, expect} from 'vitest'

afterEach(() => {
  vi.doUnmock('./dependency')
})

test('mock by imitating the actual module', async () => {
  vi.doMock('./dependency')
  const dependency = await import('./dependency')
  expect(vi.mocked(dependency.doSomething).mock).toBeDefined()
})

test('verify module is not mocked after call to doUnmock', async () => {
  const dependency = await import('./dependency')
  expect(vi.mocked(dependency.doSomething).mock).not.toBeDefined()
})

I believe this to be a bug, because if you provide an explicit replacement via the second argument to vi.mock, instead of relying on the module imitation logic, the call to unmock will successfully remove the mock.

Reproduction

See mcous/vitest-unmock-bug

System Info

System:
    OS: macOS 12.5.1
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 2.06 GB / 16.00 GB
    Shell: 5.9 - /usr/local/bin/zsh
  Binaries:
    Node: 16.18.1 - ~/.nvs/default/bin/node
    npm: 8.19.2 - ~/.nvs/default/bin/npm
    Watchman: 2022.11.07.00 - /usr/local/bin/watchman
  Browsers:
    Firefox: 107.0
    Safari: 15.6.1
  npmPackages:
    vite: ^3.2.4 => 3.2.4
    vitest: ^0.25.2 => 0.25.2

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

This should have „in“ check, because mocks without factory are stored as „null“:

PR welcome

@sheremet-va
Copy link
Member

Fixed by #2353

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

No branches or pull requests

2 participants