vitest-dev / vitest-dev/vitest
Watch Mode Import Succeeds But Reruns Tests with Stale Code with "Incorrect" Import Path
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 17.1k
- Forks
- 2k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 94
Description
Describe the bug
Hi. In watch mode, if I specify an import in a test assuming a path relative to the project’s root, tests will run correctly the first time, changes to the source module will trigger a rerun, but the imported source module will not reflect any updated code.
Here is a minimal example:
// src/sum.js
export function sum(a, b) {
return a + b
}
// test/sum.test.js
import { expect, test } from 'vitest'
// note the relative path '.' rather than '..'
import { sum } from './src/sum'
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3)
})
Running vitest will succeed — the import resolves correctly and the test will pass. Without quitting vitest, changes to src/sum.js will (correctly) trigger a retest but the actual code change in src/sum.js will not be reflected in the test. So if we change the function to return a + b + b, the tests will continue to pass until we stop and restart vitest.
Changing the import in test/sum.test.js to
import { sum } from '../src/sum'
eliminates the problem.
It seems like the correct behavior would be for the import using a path rooted at the project root to fail, since, relative to test/sum.test.js, ./src/sum.js doesn’t exist.
Reproduction
Example repo: https://github.com/drocco007/vitest_watch_mode_issue
To reproduce the issue:
- clone the repo
- npm install
- npm run test
- edit
src/sum.js, make a change — for example to introduce a bug into the calculation — and save
Expected behavior:
- the tests will rerun
- the tests will reflect the updated code in
src/sum.js
Actual behavior:
- the tests will rerun (correct)
- the tests will reflect the code as it existed when
npm run testwas first executed, not the current state of the file
System Info
$ npx envinfo --system --npmPackages '{vitest,@vitest/*,vite,@vitejs/*}' --binaries --browsers
System:
OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
Memory: 9.25 GB / 15.29 GB
Container: Yes
Shell: 5.1.16 - /bin/bash
Binaries:
Node: 18.17.1 - /usr/bin/node
npm: 10.0.0 - ~/.npm-global/bin/npm
Browsers:
Brave Browser: 108.1.46.144
Chrome: 108.0.5359.124
Chromium: 117.0.5938.132
npmPackages:
vitest: ^1.3.1 => 1.3.1
### Used Package Manager
npm
### Validations
- [X] Follow our [Code of Conduct](https://github.com/vitest-dev/vitest/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitest-dev/vitest/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitest.dev/guide/).
- [X] Check that there isn't [already an issue](https://github.com/vitest-dev/vitest/issues) that reports the same bug to avoid creating a duplicate.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions) or join our [Discord Chat Server](https://chat.vitest.dev).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Clone the linked reproduction, run npm install and npm run test, then inspect src/sum.js and test/sum.test.js while editing the source in watch mode. Confirm that the tests rerun and reflect the changed implementation rather than the initial code; the issue does not name a Vitest source file or failing test to begin with.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript, vite
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100