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: add possibility to filter test files by fileName #1915

Merged
merged 3 commits into from Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -301,6 +301,14 @@ export class Vitest {
await this.rerunFiles(files, trigger)
}

async changeFilenamePattern(pattern: string) {
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')
}

async rerunFailed() {
await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed')
}
Expand Down
19 changes: 19 additions & 0 deletions packages/vitest/src/node/stdin.ts
Expand Up @@ -8,6 +8,7 @@ const keys = [
['a', 'rerun all tests'],
['f', 'rerun only failed tests'],
['u', 'update snapshot'],
['p', 'filter by a filename'],
['t', 'filter by a test name regex pattern'],
['q', 'quit'],
]
Expand All @@ -22,6 +23,8 @@ ${keys.map(i => c.dim(' press ') + c.reset(c.bold(i[0])) + c.dim(` to ${i[1]}`)
}

export function registerConsoleShortcuts(ctx: Vitest) {
let latestFilename = ''

async function _keypressHandler(str: string, key: any) {
// ctrl-c or esc
if (str === '\x03' || str === '\x1B' || (key && key.ctrl && key.name === 'c'))
Expand All @@ -48,6 +51,9 @@ export function registerConsoleShortcuts(ctx: Vitest) {
// change testNamePattern
if (name === 't')
return inputNamePattern()
// change fileNamePattern
if (name === 'p')
return inputFilePattern()
// quit
if (name === 'q')
return ctx.exit(true)
Expand All @@ -69,6 +75,19 @@ export function registerConsoleShortcuts(ctx: Vitest) {
on()
}

async function inputFilePattern() {
off()
const { filter = '' }: { filter: string } = await prompt([{
name: 'filter',
type: 'text',
message: 'Input filename pattern',
initial: latestFilename,
}])
latestFilename = filter
await ctx.changeFilenamePattern(filter)
on()
}

let rl: readline.Interface | undefined
function on() {
off()
Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/runtime/collect.ts
Expand Up @@ -22,7 +22,6 @@ function hash(str: string): string {

export async function collectTests(paths: string[], config: ResolvedConfig) {
const files: File[] = []

const browserHashMap = getWorkerState().browserHashMap!

async function importFromBrowser(filepath: string) {
Expand Down