-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
When using vi.doMock from vitest, I found that imports cannot be called ahead of time in JavaScript files, #3763
Comments
This is not a reproduction, it is a screenshot |
Hello @lrsoy. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
But even in the screenshot, you can see that |
Sorry for my mistake, I put the specific code on stackblitz, you can see how the specific implementation is, I wrote two versions, one ts and one js, you can distinguish them by distinguishing the folder name , but it is true that ts can run through, but js can't run through, I really don't understand why this is link address: https://stackblitz.com/edit/vitest-dev-vitest-7kveii?file=test%2Fincrement-js%2Fincrement.spec.js&initialPath=__vitest__/ |
Vitest automatically removes unused imports in TypeScript files, so your example works correctly in ts files. Modules are cached, so next import returns already evaluated result. beforeEach(() => {
vi.resetModules()
}) |
thanks, this is very helpful for me |
Describe the bug
When using vi.doMock in vitest, I found that the import cannot be called ahead of time in the JavaScript file, causing vi.doMock to fail. Normally, I can use vi.doMock to mock a function or module, but calling it before the import statement doesn't seem to have an effect. I'd like to know how to fix this so that I can use vi.doMock correctly in JavaScript files.,
But I don't have any problems in ts, it can run completely and get the correct result
In the screenshot below, I took the sample code of vi.doMock from the vitest document, and he couldn’t run it in the end. Although ts is the most commonly used, this problem will occur when I use js when I’m learning.
Forgive me for not being able to post the code and only show it by screenshots, so I hope that you can try it yourself when you see this problem, this problem does exist
my development environment
pnpm 8.4.0
vitest 0.33.0
node 16.x
Reproduction
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: