Unclutter your test diet.
Vim plugin to run tests in a separate tmux window.
Out of the box it works with mocha, rspec and cucumber. Other test frameworks can be supported through some configuration.
Use a plugin manager:
Plug 'artemave/vigun'
Vigun comes with no mappings, but it does add the following commands:
Run test(s). Requires an argument that refers to one of the commands from g:vigun_mappings.
For example, with default mappings, for mocha:
:VigunRun 'all'
runs all tests in a current file.
:VigunRun 'nearest'
runs test under cursor.
:VigunRun 'debug-nearest'
starts debug session for test under cursor. By default, for mocha, this will use --inspect-brk
and copy the debug url into OS clipboard. Open new Chrome window/tab and paste it into the address bar.
If invoked from a non-test file, VigunRun
(with any argument) will attempt to run the last command.
Move tmux test window into a pane of the current vim window. And vice versa.
Open quickfix window to quickly navigate between the tests.
Fold everything, except current test and all relevant setup code (e.g. before/beforeEach blocks).
Toggle .only
for a current test/context/describe.
au FileType {ruby,javascript,typescript,cucumber} nnoremap <leader>t :VigunRun 'all'<cr>
au FileType {ruby,javascript,typescript,cucumber} nnoremap <leader>T :VigunRun 'nearest'<cr>
au FileType {ruby,javascript,typescript,cucumber} nnoremap <leader>d :VigunRun 'debug-nearest'<cr>
au FileType {javascript,typescript} nnoremap <Leader>vo :VigunToggleOnly<cr>
au FileType {ruby,javascript,typescript,go} nnoremap <leader>vi :VigunShowSpecIndex<cr>
Out of the box, vigun runs mocha, rspec and cucumber. You can add support for new frameworks or modify the default ones:
let g:vigun_mappings = [
\ {
\ 'pattern': 'Spec.js$',
\ 'all': './node_modules/.bin/mocha #{file}',
\ 'nearest': './node_modules/.bin/mocha --fgrep #{nearest_test} #{file}',
\ 'debug-all': './node_modules/.bin/mocha --inspect-brk --no-timeouts #{file}',
\ 'debug-nearest': './node_modules/.bin/mocha --inspect-brk --no-timeouts --fgrep #{nearest_test} #{file}',
\ },
\ {
\ 'pattern': '_spec.rb$',
\ 'all': 'rspec #{file}',
\ 'nearest': 'rspec #{file}:#{line}',
\ },
\ {
\ 'pattern': '.feature$',
\ 'all': 'cucumber #{file}',
\ 'nearest': 'cucumber #{file}:#{line}',
\ },
\]
Each mapping has a pattern
property that will be tested against the current file name. Note that pattern
is a regular expression, not a glob. Also note that the match order matters - the block with the first matched pattern
is selected to run tests.
All other properties represent various ways to run tests. All occurances of #{file}
, #{line}
and #{nearest_test}
in the property value are interpolated based on the current cursor position. You can name the properties whatever you like and then invoke commands via VigunRun 'your-key'
. For example, let's add watch commands:
" Note: requires ripgrep and entr
fun! s:watch(cmd)
return "rg --files | entr -r -d -c sh -c 'echo ".escape('"'.a:cmd.'"', '"')." && ".a:cmd."'"
endf
let g:vigun_mappings = [
\ {
\ 'pattern': '_spec.rb$',
\ 'all': 'rspec #{file}',
\ 'nearest': 'rspec #{file}:#{line}',
\ 'watch-all': s:watch('rspec #{file}'),
\ 'watch-nearest': s:watch('rspec #{file}:#{line}'),
\ },
\]
au FileType {ruby} nnoremap <leader>wt :VigunRun 'watch-all'<cr>
au FileType {ruby} nnoremap <leader>wT :VigunRun 'watch-nearest'<cr>
Mapping property names are arbitrary. However, there is one name based vigun feature that applies to Mocha (or anything else that makes use of .only
). If vigun detects that there is .only
test in the current file, it uses *all
command instead of *nearest
(e.g., VigunRun 'debug-nearest'
will run debug-all
command instead). This is because mocha applies both .only
and --fgrep
and the result is likely to be empty.
A line that starts with one of the following, is considered a start of the test and is used to work out #{nearest_test}
:
let g:vigun_test_keywords = ['[Ii]ts\?', '[Cc]ontext', '[Dd]escribe', 'xit', '[Ff]eature', '[Ss]cenario', 'test']
Overwrie g:vigun_test_keywords
to suit your needs.
Name of the tmux window where tests commands are sent. Defaults to test
.
Be default, VigunToggleTestWindowToPane
moves test window in a vertical split to the right of the vim pane. Setting g:vigun_tmux_pane_orientation = 'horizontal'
will change this to horizontal split at the bottom.
./run_tests