July 09, 2024

MiniTest, Tmux & Vim

I was recently working on a side project with Greg Molnar and he convinced me to try MiniTest. After 10+ years of using rspec and nothing else, I was reluctant to try. I'd of course poked at it before, but could never get in the swing of things. One of the main issues was that I had been using Vim, Tmux, and Tslime to magically and wonderfully send specs to a different tmux pane. This didn't work with MiniTest, and I couldn't find a replacement.  

So this time around, I figured if I was going to give it a fair shot, I needed to get it working so I could get into the flow I wanted. With a little help from ChatGPT, I came up with the following. 

" send minitest to tmux
function! RunCurrentMinitest()
    let l:file = expand('%')
    let l:line = line('.')
    if l:file =~ '^test/'
        let g:cmd = "bundle exec rails test " . l:file . ":" . l:line
    endif
    call system("tmux send-keys -t 1 '" . g:cmd . "' C-m")
endfunction

function! RunCurrentMinitestFile()
    let l:file = expand('%')
    if l:file =~ '^test/'
        let g:cmd = "bundle exec rails test " . l:file
    endif
    call system("tmux send-keys -t 1 '" . g:cmd . "' C-m")
endfunction

nnoremap <silent> <Leader>m :call RunCurrentMinitest()<CR>
nnoremap <silent> <Leader>M :call RunCurrentMinitestFile()<CR>

This works really nice. Leader m will run the test under your cursor. Leader M will run all the test the current file. If you navigate away to work on making the test green, you can rerun the last test using either Leader m/M. 

It's hard coded to send the command to tmux pane 1 in the current window. Tslime is more advanced and allows you to choose the session, window, and pane, but this works for me. 

Now that I have my flow back, I'm finding minitest quite fun!