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 doesn't work after up to vite 4.3 #3282

Closed
6 tasks done
or2e opened this issue May 2, 2023 · 2 comments
Closed
6 tasks done

vi.mock doesn't work after up to vite 4.3 #3282

or2e opened this issue May 2, 2023 · 2 comments

Comments

@or2e
Copy link

or2e commented May 2, 2023

Describe the bug

After updating to vite 4.3 version, vi.mock no longer work.
The test completes with an error:
ReferenceError: Cannot access %name% before initialization

Downgrading to vite 4.2 fixes the issue.

Reproduction

Vite 4.2 + Vitest
Vite 4.3 + Vitest - test fails

System Info

System:
  OS: Linux 5.15 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
  CPU: (16) x64 Intel(R) Core(TM) i9-9900KF CPU @ 3.60GHz
  Memory: 10.10 GB / 15.58 GB
  Container: Yes
  Shell: 5.1.4 - /bin/bash
Binaries:
  Node: 18.14.2 - /usr/local/share/nvm/versions/node/v18.14.2/bin/node
  npm: 9.5.0 - /usr/local/share/nvm/versions/node/v18.14.2/bin/npm
npmPackages:
  @vitejs/plugin-vue: ^4.2.1 => 4.2.1 
  @vitest/coverage-c8: ^0.30.1 => 0.30.1 
  @vitest/ui: ^0.30.1 => 0.30.1 
  vite: ^4.3.4 => 4.3.4 
  vitest: ^0.30.1 => 0.30.1

Used Package Manager

npm

Validations

@stackblitz
Copy link

stackblitz bot commented May 2, 2023

@sheremet-va
Copy link
Member

This is expected behavior. vi.mock is hoisted to top of the file before any declarations and imports, so you can't access anything declared in that scope (I don't know how you could before, but it was never intended). To bypass that you can use vi.hoisted which will be introduced in the next release (probably tomorrow).

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 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

No branches or pull requests

2 participants