This is a minimal example repository to demonstrate an issue with vitest mocking. It was created using the vanilla JS vite template and adding vitest.
NOTE Make sure to run these steps on a Windows machine in a command prompt. Command prompt recommended because depending on the version of Powershell, Powershell might normalize the directory name including the drive letter, which results in not being able to reproduce this issue.
-
Clone this repository anywhere on your local machine
-
Navigate to the directory this project was cloned on, making sure to use an absolute path that contains the lowercase
c:/drive letter. For example:cd c:/<repo path>
-
Run
npm install -
Run
npm run test -
Note the failing test (I have the test setup to expect a specific mocked value)
-
Navigate to this same directory, making sure to use an absolute path that contains the uppercase
c:/drive letter. For example:cd C:/<repo path>
-
Run
npm run testagain -
Note the succeeding test (the mocked value is returned in this case, so the test passes)