vitest icon indicating copy to clipboard operation
vitest copied to clipboard

`--changed` skips tests that import a deleted file

Open mikirejf opened this issue 1 month ago • 1 comments

Describe the bug

vitest run --changed skips a test whose import was deleted. The test now fails to load, but --changed finds no affected tests and exits with code 0. A full vitest run fails on the same tree.

The same thing happens when a setup file imports the deleted file.

AffectedModulesResolver links a test to a file through the modules Vite resolved for each import:

https://github.com/vitest-dev/vitest/blob/f15bdac4f05d2838855d4bd4f47e4076036ea18b/packages/vitest/src/node/affected-modules.ts#L346-L349

After the delete, ./helper.js no longer resolves, so the walk records no link from a.test.js to the deleted path. Git reports helper.js as changed, but no test reaches it. Vitest counts a module that fails to transform as affected, in lines 309 to 321. The unresolved import in a.test.js does not make its transform fail, so that rule does not apply.

Reproduction

mkdir deleted-changed && cd deleted-changed
npm init -y >/dev/null && npm pkg set type=module
npm i -D [email protected]
echo 'export const value = 1' > helper.js
cat > a.test.js <<'EOF'
import { expect, test } from 'vitest'
import { value } from './helper.js'
test('helper', () => { expect(value).toBe(1) })
EOF
printf 'node_modules\n' > .gitignore
git init -q && git add -A && git commit -qm base
git rm -q helper.js

npx vitest run --changed; echo "exit $?"   # No affected test files found, exiting with code 0
npx vitest run; echo "exit $?"             # Cannot find module './helper.js', exit 1

Suggested fix

Treat a changed path that no longer exists on disk as affecting every test that imported it in the base revision. If that history is not available, a simpler rule is to run every test when --changed reports a deleted file.

System info

  • vitest 5.0.4
  • git 2.56.0
  • Node 24, macOS

Used package manager

npm

Validations

  • [x] Follow our Code of Conduct
  • [x] Read the Contributing Guidelines
  • [x] Read the docs
  • [x] Check that there isn't already an issue that reports the same bug
  • [x] The provided reproduction is a minimal reproducible example of the bug

mikirejf avatar Oct 13 '26 09:10 mikirejf

Hi maintainers,

I've prepared a fix for this issue in #11635.

The change preserves dependency tracking for deleted files and includes regression tests covering both direct test imports and setup-file imports.

I understand the current PR policy and that the PR was automatically closed.

Would you be willing to review the proposed approach and consider approving this issue? If the fix aligns with the project's direction, I'd be happy to address any feedback.

Thanks for your time!

Singmin avatar Oct 18 '26 12:10 Singmin