Skip to content

Commit

Permalink
fix: add possibility to filter test files by fileName (#1915)
Browse files Browse the repository at this point in the history
* feat(vite-node): add possibility to filter test files by fileName

* feat(vite-node): change filtering mechanism to use 'includes' method

* refactor: use globTestFiles to filter by filename

Co-authored-by: golebiowskib <bartosz.golebiowski@@ttpsc.pl>
Co-authored-by: Vladimir Sheremet <sleuths.slews0s@icloud.com>
  • Loading branch information
3 people committed Sep 1, 2022
1 parent 71f1ca7 commit 9add274
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -300,6 +300,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

0 comments on commit 9add274

Please sign in to comment.