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

fix(mock-api): provide an explainatory error message when the mocks API hoisting doesn't work #1098

Merged
merged 1 commit into from Apr 6, 2022

Conversation

gdorsi
Copy link
Contributor

@gdorsi gdorsi commented Apr 4, 2022

closes #1084

@netlify
Copy link

netlify bot commented Apr 4, 2022

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit b3f8dae
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/624b734902eb560008ea30a7
😎 Deploy Preview https://deploy-preview-1098--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@sheremet-va
Copy link
Member

I think it's better to move this to factory evaluation, and check for "ReferenceError". The code is in mocker.ts

@sheremet-va
Copy link
Member

sheremet-va commented Apr 5, 2022

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 mocker.ts

@gdorsi
Copy link
Contributor Author

gdorsi commented Apr 5, 2022

Thanks for the review!

Uhm, I'm not sure I fully understand what you mean, this error happens because after the hoising vi.mock is called before the vi variable declaration.

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.

@sheremet-va
Copy link
Member

sheremet-va commented Apr 5, 2022

this error happens because after the hoising vi.mock is called before the vi variable declaration.

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. vi covers only a fracture of possible hoisting errors, so I'd prefer a more general solution.

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.

Evaluation happens here:

private async callFunctionMock(dep: string, mock: () => any) {

@gdorsi
Copy link
Contributor Author

gdorsi commented Apr 5, 2022

In this case callFunctionMock is never reached.

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

I think better to format an error that actually happen

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.

@sheremet-va
Copy link
Member

If it will never be reached, then it shouldn't throw, that's the point.

@gdorsi
Copy link
Contributor Author

gdorsi commented Apr 6, 2022

I'm still missing the point, sorry 😞

I think that it throws because the vi.mock(module) is executed before the initialization of the vi variable.

I wanted to make the error look better than Cannot access '__vite_ssr_import_0__' before initialization because it doesn't even mention real the source of the error.

I'm very new to this codebase, so I'm sure I'm missing something big.

@sheremet-va
Copy link
Member

I'm still missing the point, sorry 😞

I think that it throws because the vi.mock(module) is executed before the initialization of the vi variable.

I wanted to make the error look better than Cannot access '__vite_ssr_import_0__' before initialization because it doesn't even mention real the source of the error.

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 factory, but it will throw on initializing vi.mock. Sorry, I got confused!

@antfu antfu merged commit 0b6d980 into vitest-dev:main Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ReferenceError: Cannot access '__vite_ssr_import_0__' before initialization when using mocks
3 participants