This tutorial will show you how to debug truffle tests in the Visual Studio Code debugger. I'm assuming you have Node.js, Truffle, and VisualStudio Code already installed (links below).

First, we need a truffle project to work with. Let's use the MetaCoin example that comes with Truffle. To create this project, run these commands:

mkdir MetaCoin cd MetaCoin truffle unbox metacoin

Obviously, if you already have a truffle project you're working with, you can skip that bit.

Now, go to VSCode and open up the MetaCoin folder we just created. We're going to add a debug configuration to this project, so go to the debug configurations editor by clicking the little gear icon in Debug view, or you can go to Debug -> Open Configurations in the program menu:

Once you have the launch.json file open, add this to the configurations section of the file:

{ "name": "truffle test", "type": "node", "request": "launch", "program": "${workspaceRoot}/node_modules/truffle/build/cli.bundled.js", "args": [ "test" ], "cwd": "${workspaceRoot}", "outFiles": [ "${workspaceRoot}/test/**/*" ], },

Note: If you installed truffle globally (with the -g flag), your program should instead be:

/usr/local/lib/node_modules/truffle/build/cli.bundled.js

From the same menu, run the config we just created called truffle test :

The debugger will stop at any breakpoints you set:

Enjoy!

Resources: