Skip to content

Commit

Permalink
fix: persist cli filters as watch mode file filter (#6955)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 9, 2024
1 parent 1bf27f0 commit cc70336
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Vitest {
invalidates: Set<string> = new Set()
changedTests: Set<string> = new Set()
watchedTests: Set<string> = new Set()
filenamePattern?: string
filenamePattern?: string[]
runningPromise?: Promise<void>
closingPromise?: Promise<void>
isCancelling = false
Expand Down Expand Up @@ -418,6 +418,7 @@ export class Vitest {
await this.report('onInit', this)
}

this.filenamePattern = filters && filters?.length > 0 ? filters : undefined
const files = await this.filterTestsBySource(
await this.globTestFiles(filters),
)
Expand Down Expand Up @@ -714,7 +715,7 @@ export class Vitest {
}

if (this.filenamePattern) {
const filteredFiles = await this.globTestFiles([this.filenamePattern])
const filteredFiles = await this.globTestFiles(this.filenamePattern)
files = files.filter(file => filteredFiles.some(f => f[1] === file))
}

Expand Down Expand Up @@ -778,9 +779,9 @@ export class Vitest {
}

async changeFilenamePattern(pattern: string, files: string[] = this.state.getFilepaths()) {
this.filenamePattern = pattern
this.filenamePattern = pattern ? [pattern] : []

const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern'
const trigger = this.filenamePattern.length ? 'change filename pattern' : 'reset filename pattern'

await this.rerunFiles(files, trigger, pattern === '')
}
Expand Down Expand Up @@ -848,7 +849,7 @@ export class Vitest {
let files = Array.from(this.changedTests)

if (this.filenamePattern) {
const filteredFiles = await this.globTestFiles([this.filenamePattern])
const filteredFiles = await this.globTestFiles(this.filenamePattern)
files = files.filter(file => filteredFiles.some(f => f[1] === file))

// A file that does not match the current filename pattern was changed
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export abstract class BaseReporter implements Reporter {
}

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

if (this.ctx.configOverride.testNamePattern) {
Expand Down
10 changes: 10 additions & 0 deletions test/watch/test/stdin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ test('rerun current pattern tests', async () => {
await vitest.waitForStdout('Test name pattern: /sum/')
await vitest.waitForStdout('1 passed')
})

test('cli filter as watch filename pattern', async () => {
const { vitest } = await runVitest(_options, ['math'])

vitest.write('r')

await vitest.waitForStdout('RERUN')
await vitest.waitForStdout('Filename pattern: math')
await vitest.waitForStdout('1 passed')
})

0 comments on commit cc70336

Please sign in to comment.