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
ReferenceError: Cannot access '__vite_ssr_import_0__' before initialization when using mocks #1084
Comments
Vitest is hoisting So there are several workarounds to this:
import { render, screen, expect, describe, it, vi, userEvent, waitFor } from '../../test-utils'
vi.doMock('redaxios')
import LoginForm from './LoginForm' // LoginForm also uses `axios`
import axios from 'redaxios' |
Thanks for the answer! I've fixed the issue by importing directly I'd like to try to work on a PR to fix this issue. Should we try to hoist the related import declaration, instead limiting the hoist to vitest? |
No, I don't think it's safe to hoist other "potential" imports. I think the current solution works as expected. Maybe better error message would be preferable |
I still have the issue. Vitest v0.29.2:
there is highlighted "from" on line #3 I've just created a common utility function with route mocking:
|
This error is related to the usage of |
There is a bug actually, the fix I did should provide a user friendly error but it isn't catching your case. Could you share more code about how you are using vi.mock? The general advice to fix this issue is either:
|
Just wanted to share the behaviour I've observed that is related to the same error:
When the mock factory returns a promise ( It works completely fine when |
@gdorsi
|
@dudintv Looking at the posted files seems that you are not importing |
Correct, I use global "vi" in spec files |
Any updates on this? I'm having the same exact issue. |
If you give me a repro of the issue I can find the time to work on this one. |
@gdorsi These test files are located here: |
Created a dedicated issue with a minimal repro --> #3152 |
I was having this issue in my GitHub action and upgrading to |
Describe the bug
I get the
ReferenceError: Cannot access '__vite_ssr_import_0__' before initialization
when i try to use the APIvi.mock
.Reproduction
This is a failing workflow: https://github.com/gdorsi/vite-mock-error-repro/runs/5804962645?check_suite_focus=true
This is the test file: https://github.com/gdorsi/vite-mock-error-repro/blob/redaxios/frontend/src/components/LoginForm/LoginForm.test.jsx
This is the repro: https://github.com/gdorsi/vite-mock-error-repro
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: