Running Elixir tests in VSCode

<p>In Vim-land, I use the <a href="https://www.github.com/janko-m/vim-test">vim-test plugin</a> for quickly executing tests from a command line shortcut<sup id="fnref1"><a href="#fn1">1</a></sup>. I wanted to reproduce this behavior in <a href="https://code.visualstudio.com">Visual Studio Code</a>, but I couldn&rsquo;t find an extension that worked in multiple languages (namely, Ruby, Elixir, Javascript, and Elm). I&rsquo;m mostly just using VSCode for Elixir, but I still liked the idea of finding a more general purpose solution.</p> <p>So instead I used VSCode&rsquo;s support for <a href="https://code.visualstudio.com/docs/editor/tasks">Tasks</a> to build the functionality myself. So in my project&rsquo;s <code>tasks.json</code> file, I have the following 3 tasks for running all tests, a single test (whatever is under the cursor), and the current file&rsquo;s tests.</p> <div class="highlight"><pre class="highlight json"><code><span class="p">{</span><span class="w"> </span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"2.0.0"</span><span class="p">,</span><span class="w"> </span><span class="nl">"tasks"</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"label"</span><span class="p">:</span><span class="w"> </span><span class="s2">"mix test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shell"</span><span class="p">,</span><span class="w"> </span><span class="nl">"group"</span><span class="p">:</span><span class="w"> </span><span class="s2">"test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"command"</span><span class="p">:</span><span class="w"> </span><span class="s2">"mix test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"presentation"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"echo"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nl">"reveal"</span><span class="p">:</span><span class="w"> </span><span class="s2">"always"</span><span class="p">,</span><span class="w"> </span><span class="nl">"focus"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nl">"panel"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shared"</span><span class="p">,</span><span class="w"> </span><span class="nl">"clear"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"label"</span><span class="p">:</span><span class="w"> </span><span class="s2">"single test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shell"</span><span class="p">,</span><span class="w"> </span><span class="nl">"group"</span><span class="p">:</span><span class="w"> </span><span class="s2">"test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"command"</span><span class="p">:</span><span class="w"> </span><span class="s2">"mix test ${relativeFile}:${lineNumber}"</span><span class="p">,</span><span class="w"> </span><span class="nl">"presentation"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"echo"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nl">"reveal"</span><span class="p">:</span><span class="w"> </span><span class="s2">"always"</span><span class="p">,</span><span class="w"> </span><span class="nl">"focus"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nl">"panel"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shared"</span><span class="p">,</span><span class="w"> </span><span class="nl">"clear"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="nl">"runOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"reevaluateOnRerun"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"label"</span><span class="p">:</span><span class="w"> </span><span class="s2">"test current file"</span><span class="p">,</span><span class="w"> </span><span class="nl">"type"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shell"</span><span class="p">,</span><span class="w"> </span><span class="nl">"group"</span><span class="p">:</span><span class="w"> </span><span class="s2">"test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"command"</span><span class="p">:</span><span class="w"> </span><span class="s2">"mix test ${relativeFile}"</span><span class="p">,</span><span class="w"> </span><span class="nl">"presentation"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"echo"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"> </span><span class="nl">"reveal"</span><span class="p">:</span><span class="w"> </span><span class="s2">"always"</span><span class="p">,</span><span class="w"> </span><span class="nl">"focus"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nl">"panel"</span><span class="p">:</span><span class="w"> </span><span class="s2">"shared"</span><span class="p">,</span><span class="w"> </span><span class="nl">"clear"</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="nl">"runOptions"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"reevaluateOnRerun"</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="p">}</span><span class="w"> </span></code></pre></div> <p>Next, I setup keyboard shortcuts to run these. You can access this json file by going to Preferences &ndash;&gt; Keyboard Shortcuts, and then clicking the <code>{}</code> icon. I used <code>alt-t</code> and <code>alt-shift-t</code> for single test and current file, respectively. I didn&rsquo;t create one for running all of the tests because I don&rsquo;t do that frequently enough to warrant a shortcut.</p> <div class="highlight"><pre class="highlight json"><code><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"key"</span><span class="p">:</span><span class="w"> </span><span class="s2">"alt+t"</span><span class="p">,</span><span class="w"> </span><span class="nl">"command"</span><span class="p">:</span><span class="w"> </span><span class="s2">"workbench.action.tasks.runTask"</span><span class="p">,</span><span class="w"> </span><span class="nl">"args"</span><span class="p">:</span><span class="w"> </span><span class="s2">"single test"</span><span class="p">,</span><span class="w"> </span><span class="nl">"when"</span><span class="p">:</span><span class="w"> </span><span class="s2">"editorLangId == elixir"</span><span class="w"> </span><span class="p">}</span><span class="err">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nl">"key"</span><span class="p">:</span><span class="w"> </span><span class="s2">"alt+shift+t"</span><span class="p">,</span><span class="w"> </span><span class="nl">"command"</span><span class="p">:</span><span class="w"> </span><span class="s2">"workbench.action.tasks.runTask"</span><span class="p">,</span><span class="w"> </span><span class="nl">"args"</span><span class="p">:</span><span class="w"> </span><span class="s2">"test current file"</span><span class="p">,</span><span class="w"> </span><span class="nl">"when"</span><span class="p">:</span><span class="w"> </span><span class="s2">"editorLangId == elixir"</span><span class="w"> </span><span class="p">}</span><span class="err">,</span><span class="w"> </span></code></pre></div> <div class="footnotes"> <hr> <ol> <li id="fn1"> <p><a href="/articles/2016/02/01/run-your-ruby-tests-using-vim-and-tmux">I have an article about this</a>, but my setup has since changed to use this plugin instead.&nbsp;<a href="#fnref1">&#8617;</a></p> </li> </ol> </div>