Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: watch mode's filename pattern to persist on unrelated file changes #2754

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Vitest {

invalidates: Set<string> = new Set()
changedTests: Set<string> = new Set()
filenamePattern?: string
runningPromise?: Promise<void>
closingPromise?: Promise<void>

Expand Down Expand Up @@ -359,6 +360,11 @@ export class Vitest {
}

async rerunFiles(files: string[] = this.state.getFilepaths(), trigger?: string) {
if (this.filenamePattern) {
const filteredFiles = await this.globTestFiles([this.filenamePattern])
files = files.filter(file => filteredFiles.includes(file))
}

if (this.coverageProvider && this.config.coverage.cleanOnRerun)
await this.coverageProvider.clean()

Expand All @@ -368,20 +374,25 @@ export class Vitest {
await this.reportCoverage(!trigger)

if (!this.config.browser)
await this.report('onWatcherStart')
await this.report('onWatcherStart', this.state.getFiles(files))
}

async changeNamePattern(pattern: string, files: string[] = this.state.getFilepaths(), trigger?: string) {
// Empty test name pattern should reset filename pattern as well
if (pattern === '')
this.filenamePattern = undefined

this.config.testNamePattern = pattern ? new RegExp(pattern) : undefined
await this.rerunFiles(files, trigger)
}

async changeFilenamePattern(pattern: string) {
this.filenamePattern = pattern

const files = this.state.getFilepaths()
if (!pattern)
return await this.rerunFiles(files, 'reset filename pattern')
const filteredFiles = await this.globTestFiles([pattern])
await this.rerunFiles(filteredFiles, 'change filename pattern')
const trigger = this.filenamePattern ? 'change filename pattern' : 'reset filename pattern'

await this.rerunFiles(files, trigger)
}

async rerunFailed() {
Expand Down Expand Up @@ -433,7 +444,17 @@ export class Vitest {
this.isFirstRun = false

this.snapshot.clear()
const files = Array.from(this.changedTests)
let files = Array.from(this.changedTests)

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

// A file that does not match the current filename pattern was changed
if (files.length === 0)
return
}

this.changedTests.clear()

if (this.coverageProvider && this.config.coverage.cleanOnRerun)
Expand All @@ -446,7 +467,7 @@ export class Vitest {
await this.reportCoverage(false)

if (!this.config.browser)
await this.report('onWatcherStart')
await this.report('onWatcherStart', this.state.getFiles(files))
}, WATCHER_DEBOUNCE)
}

Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,18 @@ export abstract class BaseReporter implements Reporter {

const BADGE = c.inverse(c.bold(c.blue(' RERUN ')))
const TRIGGER = trigger ? c.dim(` ${this.relative(trigger)}`) : ''
const FILENAME_PATTERN = this.ctx.filenamePattern ? `${BADGE_PADDING} ${c.dim('Filename pattern: ')}${c.blue(this.ctx.filenamePattern)}\n` : ''
const TESTNAME_PATTERN = this.ctx.config.testNamePattern ? `${BADGE_PADDING} ${c.dim('Test name pattern: ')}${c.blue(String(this.ctx.config.testNamePattern))}\n` : ''

if (files.length > 1) {
// we need to figure out how to handle rerun all from stdin
this.ctx.logger.clearFullScreen(`\n${BADGE}${TRIGGER}\n`)
this.ctx.logger.clearFullScreen(`\n${BADGE}${TRIGGER}\n${FILENAME_PATTERN}${TESTNAME_PATTERN}`)
this._lastRunCount = 0
}
else if (files.length === 1) {
const rerun = this._filesInWatchMode.get(files[0]) ?? 1
this._lastRunCount = rerun
this.ctx.logger.clearFullScreen(`\n${BADGE}${TRIGGER} ${c.blue(`x${rerun}`)}\n`)
this.ctx.logger.clearFullScreen(`\n${BADGE}${TRIGGER} ${c.blue(`x${rerun}`)}\n${FILENAME_PATTERN}${TESTNAME_PATTERN}`)
}

this._timeStart = new Date()
Expand Down