gabz75 · GitHub

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:

Screenshot 2024-09-13 at 11 10 10 AM

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.1

Used Package Manager

npm

Validations

Read the original on github.com ↗