debugging mocha tests

2016-11-13

Today I learnt new about debugging mocha tests.

The following is how I debugged using Microsoft’s awesome VSCode editor

In the root of your project, create a folder with the following name .vscode
Within that folder, create a file named launch.json

Open your launch.json and add the following configuration:

1
2
3
4
5
6
7
8
9
10
11
12
{
"version": "0.1.0",
"configurations": [
{
"name": "Debug Mocha Test",
"type": "node",
"address": "localhost",
"port": 5858,
"sourceMaps": false
}
]
}

From your projects root, run your test runner with the debug argument like

1
mocha test --debug-brk

Or if you’ve created a debug script in your package.json like

1
2
3
"scripts": {
"debug": "mocha --debug-brk"
}

then run it like

1
npm run debug

In your vscode editor, switch over to the debug panel on the left-hand side and start the debugger.
Adding breakpoints in the test code will cause the runner to pause giving you all the time you need to debug you tests!!