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

When using vi.doMock from vitest, I found that imports cannot be called ahead of time in JavaScript files, #3763

Closed
6 tasks done
lrsoy opened this issue Jul 12, 2023 · 6 comments

Comments

@lrsoy
Copy link

lrsoy commented Jul 12, 2023

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

99e00d7e636cd1d7d109cae7e850768

System Info

FAIL  increment.spec.js > 官方文档案例vi.doMock > importing the next module imports mocked one
AssertionError: expected 100 to be 101 // Object.is equality

- Expected
+ Received

- 101
+ 100

 ❯ increment.spec.js:24:32


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed (1)
   Start at  18:38:39
   Duration  398ms (transform 49ms, setup 0ms, collect 36ms, tests 12ms, environment 0ms, prepare 147ms)


 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

This is not a reproduction, it is a screenshot

@github-actions
Copy link

Hello @lrsoy. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@sheremet-va
Copy link
Member

But even in the screenshot, you can see that vi.doMock works correctly. If it didn't, you would get 2 and not 100. The only bug here is the mocked implementation - it should use ++ at the start and not after mockedIncrement.

@lrsoy
Copy link
Author

lrsoy commented Jul 12, 2023

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__/

@lrsoy lrsoy closed this as completed Jul 13, 2023
@lrsoy lrsoy reopened this Jul 13, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
@sheremet-va sheremet-va reopened this Jul 17, 2023
@sheremet-va
Copy link
Member

sheremet-va commented Jul 28, 2023

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. vi.doMock clears the cache for specified modules, but doesn't clear the cache for modules that import it, you need to do it yourself by calling vi.resetModules:

beforeEach(() => {
  vi.resetModules()
})

@lrsoy
Copy link
Author

lrsoy commented Aug 2, 2023

thanks, this is very helpful for me

@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants