Skip to content

Commit cc70336

Browse files
authoredDec 9, 2024··
fix: persist cli filters as watch mode file filter (#6955)
1 parent 1bf27f0 commit cc70336

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
 

‎packages/vitest/src/node/core.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class Vitest {
6868
invalidates: Set<string> = new Set()
6969
changedTests: Set<string> = new Set()
7070
watchedTests: Set<string> = new Set()
71-
filenamePattern?: string
71+
filenamePattern?: string[]
7272
runningPromise?: Promise<void>
7373
closingPromise?: Promise<void>
7474
isCancelling = false
@@ -418,6 +418,7 @@ export class Vitest {
418418
await this.report('onInit', this)
419419
}
420420

421+
this.filenamePattern = filters && filters?.length > 0 ? filters : undefined
421422
const files = await this.filterTestsBySource(
422423
await this.globTestFiles(filters),
423424
)
@@ -714,7 +715,7 @@ export class Vitest {
714715
}
715716

716717
if (this.filenamePattern) {
717-
const filteredFiles = await this.globTestFiles([this.filenamePattern])
718+
const filteredFiles = await this.globTestFiles(this.filenamePattern)
718719
files = files.filter(file => filteredFiles.some(f => f[1] === file))
719720
}
720721

@@ -778,9 +779,9 @@ export class Vitest {
778779
}
779780

780781
async changeFilenamePattern(pattern: string, files: string[] = this.state.getFilepaths()) {
781-
this.filenamePattern = pattern
782+
this.filenamePattern = pattern ? [pattern] : []
782783

783-
const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern'
784+
const trigger = this.filenamePattern.length ? 'change filename pattern' : 'reset filename pattern'
784785

785786
await this.rerunFiles(files, trigger, pattern === '')
786787
}
@@ -848,7 +849,7 @@ export class Vitest {
848849
let files = Array.from(this.changedTests)
849850

850851
if (this.filenamePattern) {
851-
const filteredFiles = await this.globTestFiles([this.filenamePattern])
852+
const filteredFiles = await this.globTestFiles(this.filenamePattern)
852853
files = files.filter(file => filteredFiles.some(f => f[1] === file))
853854

854855
// A file that does not match the current filename pattern was changed

‎packages/vitest/src/node/reporters/base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ export abstract class BaseReporter implements Reporter {
221221
}
222222

223223
if (this.ctx.filenamePattern) {
224-
this.log(BADGE_PADDING + c.dim(' Filename pattern: ') + c.blue(this.ctx.filenamePattern))
224+
this.log(BADGE_PADDING + c.dim(' Filename pattern: ') + c.blue(this.ctx.filenamePattern.join(', ')))
225225
}
226226

227227
if (this.ctx.configOverride.testNamePattern) {

‎test/watch/test/stdin.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ test('rerun current pattern tests', async () => {
103103
await vitest.waitForStdout('Test name pattern: /sum/')
104104
await vitest.waitForStdout('1 passed')
105105
})
106+
107+
test('cli filter as watch filename pattern', async () => {
108+
const { vitest } = await runVitest(_options, ['math'])
109+
110+
vitest.write('r')
111+
112+
await vitest.waitForStdout('RERUN')
113+
await vitest.waitForStdout('Filename pattern: math')
114+
await vitest.waitForStdout('1 passed')
115+
})

0 commit comments

Comments
 (0)
Please sign in to comment.