Skip to content

Commit 01a5972

Browse files
authored
fix(watch): properly remove cache after removing existing test files (#7399)
1 parent 0f6d6a6 commit 01a5972

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

packages/vitest/src/node/project.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ export class TestProject {
390390
this.testFilesList?.push(testPath)
391391
}
392392

393+
/** @internal */
394+
_removeCachedTestFile(testPath: string): void {
395+
if (this.testFilesList) {
396+
this.testFilesList = this.testFilesList.filter(file => file !== testPath)
397+
}
398+
}
399+
393400
/**
394401
* Returns if the file is a test file. Requires `.globTestFiles()` to be called first.
395402
* @internal

packages/vitest/src/node/watcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class VitestWatcher {
7070
this.invalidates.add(id)
7171

7272
if (this.vitest.state.filesMap.has(id)) {
73+
this.vitest.projects.forEach(project => project._removeCachedTestFile(id))
7374
this.vitest.state.filesMap.delete(id)
7475
this.vitest.cache.results.removeFromCache(id)
7576
this.vitest.cache.stats.removeStats(id)

test/watch/test/file-watching.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
1+
import { existsSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
22
import { afterEach, describe, expect, test } from 'vitest'
33

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

123+
test('renaming an existing test file', async () => {
124+
cleanups.push(() => rmSync('fixtures/after.test.ts'))
125+
const beforeFile = 'fixtures/before.test.ts'
126+
const afterFile = 'fixtures/after.test.ts'
127+
const textContent = `
128+
import { expect, test } from "vitest";
129+
130+
test("test case", () => {
131+
console.log("Running existing test")
132+
expect(true).toBeTruthy()
133+
})
134+
`
135+
writeFileSync(beforeFile, textContent, 'utf-8')
136+
const { vitest } = await testUtils.runVitest({ root: 'fixtures', watch: true })
137+
await vitest.waitForStdout('Running existing test')
138+
139+
renameSync(beforeFile, afterFile)
140+
await vitest.waitForStdout('Test removed')
141+
await vitest.waitForStdout('Waiting for file changes...')
142+
143+
vitest.write('p')
144+
await vitest.waitForStdout('Input filename pattern')
145+
vitest.write('before')
146+
await vitest.waitForStdout('Pattern matches no results')
147+
vitest.write('\n')
148+
await vitest.waitForStdout('No test files found')
149+
await vitest.waitForStdout('Waiting for file changes...')
150+
vitest.write('p')
151+
await vitest.waitForStdout('Input filename pattern')
152+
vitest.write('after')
153+
await vitest.waitForStdout('Pattern matches 1 result')
154+
vitest.write('\n')
155+
await vitest.waitForStdout('Filename pattern: after')
156+
await vitest.waitForStdout('1 passed')
157+
})
158+
123159
test('editing source file generates new test report to file system', async () => {
124160
const report = 'fixtures/test-results/junit.xml'
125161
if (existsSync(report)) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy