Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
25 changes: 25 additions & 0 deletions docs/guide/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ tests/test1.test.ts
tests/test2.test.ts
```

## Shell Autocompletions

Vitest provides shell autocompletions for commands, options, and option values powered by [`@bomb.sh/tab`](https://github.com/bombshell-dev/tab).

### Setup

For permanent setup in zsh, add this to your `~/.zshrc`:

```bash
# Add to ~/.zshrc for permanent autocompletions
source <(vitest complete zsh)
```

Comment thread
AmirSa12 marked this conversation as resolved.
### Package Manager Integration

`@bomb.sh/tab` integrates with package managers. Autocompletions work when running vitest directly:

```bash
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
npx vitest <Tab>
npm exec vitest <Tab>
pnpm vitest <Tab>
yarn vitest <Tab>
bun vitest <Tab>
```

Comment thread
AmirSa12 marked this conversation as resolved.
## Options

::: tip
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ Repository: git+https://github.com/antfu/install-pkg.git

---------------------------------------

## @bomb.sh/tab
Comment thread
AmirSa12 marked this conversation as resolved.
License: MIT
By: Bombshell Authors
Repository: git+https://github.com/bombshell-dev/tab.git

---------------------------------------

## @jridgewell/resolve-uri
License: MIT
By: Justin Ridgewell
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
}
},
"dependencies": {
"@bomb.sh/tab": "^0.0.5",
"@vitest/expect": "workspace:*",
"@vitest/mocker": "workspace:*",
"@vitest/pretty-format": "workspace:*",
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { normalize } from 'pathe'
import c from 'tinyrainbow'
import { version } from '../../../package.json' with { type: 'json' }
import { benchCliOptionsConfig, cliOptionsConfig, collectCliOptionsConfig } from './cli-config'
import { setupTabCompletions } from './completions'

function addCommand(cli: CAC | Command, name: string, option: CLIOption<any>) {
const commandName = option.alias || name
Expand Down Expand Up @@ -195,6 +196,7 @@ export function createCLI(options: CliParseOptions = {}): CAC {
.command('[...filters]', undefined, options)
.action((filters, options) => start('test', filters, options))

setupTabCompletions(cli)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it async?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, but that's intentional here. I wanted to keep createCLI sync while the completions setup async in the background

return cli
}

Expand Down
119 changes: 119 additions & 0 deletions packages/vitest/src/node/cli/completions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { CAC } from 'cac'
import tab from '@bomb.sh/tab/cac'

function testFilters(complete: (pattern: string, desc: string) => void) {
complete('**/*.test.ts', 'TypeScript test files')
complete('**/*.test.js', 'JavaScript test files')
complete('**/*.spec.ts', 'TypeScript spec files')
complete('**/*.spec.js', 'JavaScript spec files')
}

function benchFilters(complete: (pattern: string, desc: string) => void) {
complete('**/*.bench.ts', 'TypeScript benchmark files')
complete('**/*.bench.js', 'JavaScript benchmark files')
complete('**/*.benchmark.ts', 'TypeScript benchmark files')
complete('**/*.benchmark.js', 'JavaScript benchmark files')
}

function relatedFilters(complete: (pattern: string, desc: string) => void) {
complete('src/**/*.ts', 'TypeScript source files')
complete('src/**/*.js', 'JavaScript source files')
complete('lib/**/*.ts', 'TypeScript library files')
complete('lib/**/*.js', 'JavaScript library files')
}

export async function setupTabCompletions(cli: CAC): Promise<void> {
Comment thread
AmirSa12 marked this conversation as resolved.
await tab(cli, {
subCommands: {
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
run: { args: { filters: testFilters } },

Check failure on line 28 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Extra space before value for key 'run'
watch: { args: { filters: testFilters } },
dev: { args: { filters: testFilters } },

Check failure on line 30 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Extra space before value for key 'dev'
list: { args: { filters: testFilters } },

Check failure on line 31 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Extra space before value for key 'list'
related: { args: { filters: relatedFilters } },
bench: { args: { filters: benchFilters } },
init: {
args: {
project(complete) {
complete('browser', 'Initialize browser testing setup')
},
},
},
},
options: {
config(complete) {
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
complete('vitest.config.ts', 'TypeScript config file')
complete('vitest.config.js', 'JavaScript config file')
complete('vitest.config.mjs', 'ES module config file')
complete('vite.config.ts', 'Vite TypeScript config file')
complete('vite.config.js', 'Vite JavaScript config file')
},
mode(complete) {
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
complete('test', 'Test mode')
complete('benchmark', 'Benchmark mode')
complete('development', 'Development mode')
complete('production', 'Production mode')
},
environment(complete) {
complete('node', 'Node.js environment')
complete('jsdom', 'JSDOM environment')
complete('happy-dom', 'Happy DOM environment')
complete('edge-runtime', 'Edge runtime environment')
},
pool(complete) {
complete('threads', 'Threads pool')
complete('forks', 'Forks pool')
complete('vmThreads', 'VM threads pool')
complete('vmForks', 'VM forks pool')
},
reporter(complete) {
complete('default', 'Default reporter')
complete('verbose', 'Verbose reporter')
complete('dot', 'Dot reporter')
complete('json', 'JSON reporter')
complete('junit', 'JUnit reporter')
complete('html', 'HTML reporter')
complete('tap', 'TAP reporter')
complete('tap-flat', 'TAP flat reporter')
complete('hanging-process', 'Hanging process reporter')
},
'coverage.provider'(complete) {

Check failure on line 79 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Expected longform method syntax for string literal keys
complete('v8', 'V8 coverage provider')
complete('istanbul', 'Istanbul coverage provider')
complete('custom', 'Custom coverage provider')
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
},
'coverage.reporter'(complete) {

Check failure on line 84 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Expected longform method syntax for string literal keys
complete('text', 'Text coverage reporter')
complete('html', 'HTML coverage reporter')
complete('clover', 'Clover coverage reporter')
complete('json', 'JSON coverage reporter')
complete('json-summary', 'JSON summary coverage reporter')
complete('lcov', 'LCOV coverage reporter')
complete('lcovonly', 'LCOV only coverage reporter')
complete('teamcity', 'TeamCity coverage reporter')
complete('cobertura', 'Cobertura coverage reporter')
},
'browser.name'(complete) {

Check failure on line 95 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Expected longform method syntax for string literal keys
complete('chrome', 'Google Chrome')
complete('firefox', 'Mozilla Firefox')
complete('safari', 'Safari')
complete('edge', 'Microsoft Edge')
complete('chromium', 'Chromium')
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
},
'browser.provider'(complete) {

Check failure on line 102 in packages/vitest/src/node/cli/completions.ts

View workflow job for this annotation

GitHub Actions / Lint: node-latest, ubuntu-latest

Expected longform method syntax for string literal keys
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
complete('playwright', 'Playwright provider')
complete('webdriverio', 'WebdriverIO provider')
complete('preview', 'Preview provider')
},
testNamePattern(complete) {
Comment thread
AmirSa12 marked this conversation as resolved.
Outdated
complete('should.*work', 'Tests containing "should" and "work"')
complete('integration.*test', 'Integration tests')
complete('unit.*test', 'Unit tests')
},
silent(complete) {
complete('true', 'Enable silent mode')
complete('false', 'Disable silent mode')
complete('passed-only', 'Show logs from failing tests only')
},
},
})
}
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading