How to debug busted unit tests in VSCode

It took quite a bit of research to figure out how to get this to work, so I decided to write it here as it makes the lua track much smoother, since you can debug your solution by running the tests themselves.

First, I could not get either the ‘Lua Debug’ extension or the ‘Lua’ one by tencent to work with this configuration. Both of them had issues resolving paths.

Instead I used the ‘Local Lua Debugger’ module, listed here: Local Lua Debugger - Visual Studio Marketplace

Remove/Disable other lua debuggers and install this one. Then add this launch configuration to launch.json:

    {
      "name": "Busted",
      "type": "lua-local",
      "request": "launch",
      "program": {
        "command": "busted"
      },
      "cwd": "${workspaceFolder}/${relativeFileDirname}",
      "args": [
        "${file}"
      ],
      "integratedTerminal": true ,
    }

The cwd property specifices the current working directory, this is important for resolving paths. By default Exercism installs each exercise into it’s own folder under the folder of the track name. So in the case of lua it looks like this:

.
└── Exercism/
    ├── lua/
    │   ├── armstrong-numbers
    │   ├── bank-account
    │   ├── binarysearch
    │   └── etc...
    └── python/
        └── etc...

So when I open VSCode from the lua folder, I will have all the exercises in my workspace, that means I need the launch config to change the cwd to each individual exercise folder. The args property is what argument should be passed to busted (.i.e. as if you were running it from the command line, like c:\<command> <args> would equate to c:\busted ${file}, where ${file} is a vscode variable that specifices the currently opened file.) The variable ${relativeFileDirname} specifies the folder of the currently open file.

So that means when I open the test file I want to debug, for example Exercism/lua/armstrong-numbers/armstrong-numbers_spec.lua and run the ‘Busted’ launch config, the cwd will be set to Exercism/lua/armstrong-numbers/ and args will be set to armstrong-numbers_spec.lua. This should solve any path issues.

Then add these three lines to the top of your test_spec.lua file:

if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
  require("lldebugger").start()
end

Then add breakpoints to the test file or the module you are testing and it should stop at your breakpoints.

The line "integratedTerminal": true , gives me a warning but works fine, you should see the output from your tests in the Debug Console in VSCode.

3 Likes