Debugging AVA with VS Code is not that simple

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.


Introduction

AVA is a nice test runner for Node.js with moderate disk consumption. Despite documentation and many tutorials, debugging it is not trivial.

My related project

E-book: How to set up free WordPress platform for IT blogs

Do you want to set up a typical blog but you don’t know how to start, which plugins to use or how to customize the site? I spent a few weeks in total doing that from scratch – from learning the ecosystem to adjusting the platform to my very specific needs. Now I want to share with my experience and make the start easier for you.

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:

JSON
.vscode/launch.json

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:

JSON
.vscode/launch.json

Information:

  • .bin/ava or .bin/ava.cmd essentially run node ../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:

JSON
.vscode/launch.json

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.

0 0 votes
Article Rating

Want more?

Share!

Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
Nerdlinger
Nerdlinger
2 years ago

Hey, good article! Have you figured out debugging with ava-ts yet?!

2
0
Would love your thoughts, please comment.x
()
x