Running Elixir tests in VSCode

In Vim-land, I use the vim-test plugin for quickly executing tests from a command line shortcut1. I wanted to reproduce this behavior in Visual Studio Code, but I couldn’t find an extension that worked in multiple languages (namely, Ruby, Elixir, Javascript, and Elm). I’m mostly just using VSCode for Elixir, but I still liked the idea of finding a more general purpose solution.

So instead I used VSCode’s support for Tasks to build the functionality myself. So in my project’s tasks.json file, I have the following 3 tasks for running all tests, a single test (whatever is under the cursor), and the current file’s tests.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "mix test",
      "type": "shell",
      "group": "test",
      "command": "mix test",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "clear": true
      }
    },
    {
      "label": "single test",
      "type": "shell",
      "group": "test",
      "command": "mix test ${relativeFile}:${lineNumber}",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "clear": true
      },
      "runOptions": {
        "reevaluateOnRerun": false
      }
    },
    {
      "label": "test current file",
      "type": "shell",
      "group": "test",
      "command": "mix test ${relativeFile}",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
        "clear": true
      },
      "runOptions": {
        "reevaluateOnRerun": false
      }
    }
  ]
}

Next, I setup keyboard shortcuts to run these. You can access this json file by going to Preferences –> Keyboard Shortcuts, and then clicking the {} icon. I used alt-t and alt-shift-t for single test and current file, respectively. I didn’t create one for running all of the tests because I don’t do that frequently enough to warrant a shortcut.

    {
        "key": "alt+t",
        "command": "workbench.action.tasks.runTask",
        "args": "single test",
        "when": "editorLangId == elixir"
    },
    {
        "key": "alt+shift+t",
        "command": "workbench.action.tasks.runTask",
        "args": "test current file",
        "when": "editorLangId == elixir"
    },

  1. I have an article about this, but my setup has since changed to use this plugin instead.