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.mock ignored if module is imported in setup file #1450

Closed
6 tasks done
MaartenPetiet opened this issue Jun 8, 2022 · 3 comments
Closed
6 tasks done

vi.mock ignored if module is imported in setup file #1450

MaartenPetiet opened this issue Jun 8, 2022 · 3 comments

Comments

@MaartenPetiet
Copy link

Describe the bug

Registration of vi.mock on a module will not work if that module is imported in a file added to the setupFiles array in vite.config.ts.

Use case

We use Vue 3 with global component registration (this is needed because of the way our CMS works). To accomplish this in our tests, we register these components in vitest.setup.ts to the @vue/test-utils config as the example below.

import { config } from '@vue/test-utils';

config.global.components[name] = component;

This causes the mocks set on the imports of these components to be ignored. Although this is our use case, this issue is unrelated to Vue. See the minimal reproduction for more details.

Reproduction

Here is a minimal repo: https://stackblitz.com/edit/vitest-dev-vitest-etmse4?file=vitest.setup.ts

In test/foo.test.ts a mock is used. This fails, unless I remove the only line in vitest.setup.ts.

System Info

System:
    OS: Windows 10 10.0.17763
    CPU: (4) x64 Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz
    Memory: 4.85 GB / 16.00 GB
  Binaries:
    Node: 14.17.6 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.15 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 102.0.5005.63
    Internet Explorer: 11.0.17763.2145
  npmPackages:
    @vitejs/plugin-vue: ^2.3.3 => 2.3.3
    vite: ^2.9.9 => 2.9.9
    vitest: 0.14.1 => 0.14.1

Used Package Manager

npm

Validations

@sheremet-va sheremet-va added the bug label Jun 9, 2022
@sheremet-va
Copy link
Member

I found the problem, but I am not sure it's possible to fix without losing performance.

For now, you can call vi.resetModules() before any imports in a test file.

@MaartenPetiet
Copy link
Author

Thanks, I have added vi.resetModules() at the end of vitest.setup.ts to only call it once and that seems to work too.

@sheremet-va
Copy link
Member

Module was already evaluated and cached with code that you provided in setup, so when it is imported next time, it uses this cached version. I don't think there is a good way for us to clear that cache automatically, so you need to use vi.resetModules.

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 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.

2 participants