Skip to content

Commit

Permalink
fix(watch): properly remove cache after removing existing test files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
soc221b authored Feb 10, 2025
1 parent 0f6d6a6 commit 01a5972
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/vitest/src/node/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ export class TestProject {
this.testFilesList?.push(testPath)
}

/** @internal */
_removeCachedTestFile(testPath: string): void {
if (this.testFilesList) {
this.testFilesList = this.testFilesList.filter(file => file !== testPath)
}
}

/**
* Returns if the file is a test file. Requires `.globTestFiles()` to be called first.
* @internal
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class VitestWatcher {
this.invalidates.add(id)

if (this.vitest.state.filesMap.has(id)) {
this.vitest.projects.forEach(project => project._removeCachedTestFile(id))
this.vitest.state.filesMap.delete(id)
this.vitest.cache.results.removeFromCache(id)
this.vitest.cache.stats.removeStats(id)
Expand Down
38 changes: 37 additions & 1 deletion test/watch/test/file-watching.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { existsSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, expect, test } from 'vitest'

import * as testUtils from '../../test-utils'
Expand Down Expand Up @@ -120,6 +120,42 @@ test("dynamic test case", () => {
await vitest.waitForStdout('1 passed')
})

test('renaming an existing test file', async () => {
cleanups.push(() => rmSync('fixtures/after.test.ts'))
const beforeFile = 'fixtures/before.test.ts'
const afterFile = 'fixtures/after.test.ts'
const textContent = `
import { expect, test } from "vitest";
test("test case", () => {
console.log("Running existing test")
expect(true).toBeTruthy()
})
`
writeFileSync(beforeFile, textContent, 'utf-8')
const { vitest } = await testUtils.runVitest({ root: 'fixtures', watch: true })
await vitest.waitForStdout('Running existing test')

renameSync(beforeFile, afterFile)
await vitest.waitForStdout('Test removed')
await vitest.waitForStdout('Waiting for file changes...')

vitest.write('p')
await vitest.waitForStdout('Input filename pattern')
vitest.write('before')
await vitest.waitForStdout('Pattern matches no results')
vitest.write('\n')
await vitest.waitForStdout('No test files found')
await vitest.waitForStdout('Waiting for file changes...')
vitest.write('p')
await vitest.waitForStdout('Input filename pattern')
vitest.write('after')
await vitest.waitForStdout('Pattern matches 1 result')
vitest.write('\n')
await vitest.waitForStdout('Filename pattern: after')
await vitest.waitForStdout('1 passed')
})

test('editing source file generates new test report to file system', async () => {
const report = 'fixtures/test-results/junit.xml'
if (existsSync(report)) {
Expand Down

0 comments on commit 01a5972

Please sign in to comment.