Debugging AVA with VS Code is not that simple
In general, I strongly dislike debugging in Visual Studio Code, because it is a pain in the ass to configure for every project from the beginning, besides it sometimes simply doesn’t work.
One of the worst things to debug are unit tests running in Node.js. They run in weird threads, start with complicated frameworks, and often simply don’t hit breakpoints.
The worst so far was AVA.
Table of contents
Introduction
AVA is a nice test runner for Node.js with moderate disk consumption. Despite documentation and many tutorials, debugging it is not trivial.
Note: I'll soon be sharing short, practical tips on Angular — a good way to pick up something new if you're interested.
Debugging in VS Code requires creating a Launch Configuration stored in the .vscode/launch.json file.
VS Code documentation
A very long manual on debugging Node.js applications in VS Code states that debugging a JavaScript file should require the following launch configuration:
It works well for simple scripts. It fails if the script requires a framework like AVA to run. In this case, the entry point should be AVA.
AVA documentation – for version 3
There is an instruction in the AVA project how to set up Visual Studio Code:
Information:
.bin/avaor.bin/ava.cmdessentially runnode ../ava/cli.js${file}means you have to open the test file to run with AVA and then launch the debug
Please note that the current document is for version 3.
In AVA version 2, there is an error that debug is not a valid CLI switch. A valid switch would be --debug, but despites that, I cannot stop at any breakpoint – neither in the test.js nor in cli.js.
In AVA version 3, I get the error “Debugging is not available in CI”. Even after commenting out the check for CI it did it work, this time with the error “SyntaxError: Unexpected identifier: import test from ‘ava’;”.
So, it started working, except that ES6 import style is not recognized. After replacing them with require() it worked, but that is a workaround. It is possible to configure AVA to run Babel first, but it requires installation of further npm modules.
AVA-TS documentation – for version 2
For version 2, the previous official instruction, copied also here, does a better job:
I suggest adding another args: --serial to disable concurrent execution of tests.
This method won’t work for version 3, since it lacks the profile.js file.
Breakpoints
Sometimes my breakpoints move around the file or are not hit. In short, they are not very reliable. A better solution is adding the debugger keyword to the source code, it always works. But it modifies the source code and it’s not listed in the breakpoints list.
Hey, good article! Have you figured out debugging with ava-ts yet?!
Thanks :).
I didn’t figure out and I gave up. It was used in version 2 in a project I made a pull request to and I simply didn’t upgrade it to v3 ;).