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

feat: allow changing name pattern during watch mode #819

Merged
merged 4 commits into from Feb 21, 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
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