Describe the bug
The stackblitz below illustrates the issue.
Let's say I have a function main that calls a function foobar. When testing main, I would like to mock foobar and have the mock implementation in a separate file.
In my test file, if I import the file with the mock implementation BEFORE the file where the function main is, everything works fine.
However if I import the file with the mock implementation AFTER the file where the function main is,vitest complains with the following hoisting error:
Notes: this example might be silly for functions but this pattern comes in handy in different context when trying to mock components and organize code in several files.
Reproduction
https://stackblitz.com/edit/vitejs-vite-q8pc8e?file=main.test.js&view=editor
Simply run the command npm exec vitest to see the issue.
System Info
System:
OS: Linux 5.0 undefined
CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Memory: 0 Bytes / 0 Bytes
Shell: 1.0 - /bin/jsh
Binaries:
Node: 18.20.3 - /usr/local/bin/node
Yarn: 1.22.19 - /usr/local/bin/yarn
npm: 10.2.3 - /usr/local/bin/npm
pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
vite: ^5.4.3 => 5.4.4
vitest: ^2.1.0 => 2.1.1Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
