Skip to content

Commit

Permalink
feat: allow changing name pattern during watch mode (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 21, 2022
1 parent 5456b52 commit 0b2bc38
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -208,6 +208,11 @@ export class Vitest {
await this.report('onWatcherStart')
}

async changeNamePattern(pattern: string, files: string[] = this.state.getFilepaths(), trigger?: string) {
this.config.testNamePattern = new RegExp(pattern)
await this.rerunFiles(files, trigger)
}

async returnFailed() {
await this.rerunFiles(this.state.getFailedFilepaths(), 'rerun failed')
}
Expand Down
56 changes: 56 additions & 0 deletions packages/vitest/src/node/stdin.ts
Expand Up @@ -6,6 +6,7 @@ const keys = [
['a', 'rerun all tests'],
['f', 'rerun only failed tests'],
['u', 'update snapshot'],
['t', 'filter by a test name regex pattern'],
['q', 'quit'],
]

Expand All @@ -18,7 +19,51 @@ ${keys.map(i => c.dim(' press ') + c.reset(c.bold(i[0])) + c.dim(` to ${i[1]}`)
)
}

function useChangePattern(ctx: Vitest) {
let namePattern = ''
let changingPattern = false

function backspace() {
readline.moveCursor(process.stdout, -1, process.stdout.rows)
process.stdout.write(' ')
readline.moveCursor(process.stdout, -1, process.stdout.rows)
}

function end() {
ctx.changeNamePattern(namePattern, undefined, 'change pattern')
namePattern = ''
changingPattern = false
}

function start() {
process.stdout.write(`\n${c.bgMagenta(' FILTER ')} ${c.magenta('Filter tests by their name (using regexp):')} `)
changingPattern = true
}

function append(str: string, key: any) {
if (key.name === 'backspace') {
namePattern = namePattern.slice(0, namePattern.length - 1)
backspace()
}
else {
namePattern += str
process.stdout.write(str)
}
}

return {
get isChanging() {
return changingPattern
},
end,
start,
append,
}
}

export function registerConsoleShortcuts(ctx: Vitest) {
const pattern = useChangePattern(ctx)

readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on('keypress', (str: string, key: any) => {
Expand All @@ -32,6 +77,13 @@ export function registerConsoleShortcuts(ctx: Vitest) {

const name = key?.name

if (pattern.isChanging) {
if (name === 'return')
return pattern.end()

return pattern.append(str, key)
}

// help
if (name === 'h')
return printShortcutsHelp()
Expand All @@ -41,6 +93,10 @@ export function registerConsoleShortcuts(ctx: Vitest) {
// rerun all tests
if (name === 'a' || name === 'return')
return ctx.rerunFiles(undefined, 'rerun all')
// change testNamePattern
if (name === 't')
return pattern.start()

// quit
if (name === 'q')
return ctx.exit(true)
Expand Down

0 comments on commit 0b2bc38

Please sign in to comment.