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
fix(mock-api): provide an explainatory error message when the mocks API hoisting doesn't work #1098
Conversation
…PI hoisting doesn't work
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
I think it's better to move this to factory evaluation, and check for "ReferenceError". The code is in |
I think it's better to move this to factory evaluation, and check for "ReferenceError", to show error for any variable that it tries to reach and cant. The code is in |
Thanks for the review! Uhm, I'm not sure I fully understand what you mean, this error happens because after the hoising We never reach the factory evaluation (which happens somewhere around here, right?), because the error is raised at the very start of the module execution. |
Yes, that is why I left a review. I don't like raising the error here, I think better to format an error that actually happens, since parsing is unsafe.
Evaluation happens here:
|
In this case I've tried to throw an error at the start of the function: private async callFunctionMock(dep: string, mock: () => any) {
throw new Error('callFunctionMock') I still get: ReferenceError: Cannot access '__vite_ssr_import_0__' before initialization
WDYT if we wrap the hoisted statements in a try/catch? I'm thinking about something like this: packages/vitest/src/node/plugins/mock.ts#L29 - m.prepend(`${m.slice(startIndex, endIndex)}\n`)
+ m.prepend(`try { ${m.slice(startIndex, endIndex)} } catch(err) { isReferenceError(err) ? throw MOCK_ERROR : throw err } \n`) Anyway, if this becomes too complex we could simply close the PR. |
If it will never be reached, then it shouldn't throw, that's the point. |
I'm still missing the point, sorry 😞 I think that it throws because the I wanted to make the error look better than I'm very new to this codebase, so I'm sure I'm missing something big. |
Oh, wait, you are write. I was thinking about usage inside |
closes #1084