Skip to content

Instantly share code, notes, and snippets.

@paambaati
Last active August 19, 2025 10:21
Show Gist options
  • Select an option

  • Save paambaati/54d33e409b4f7cf059cc to your computer and use it in GitHub Desktop.

Select an option

Save paambaati/54d33e409b4f7cf059cc to your computer and use it in GitHub Desktop.
Debug mocha tests using Visual Studio Code
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run app.js",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "app.js",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { "NODE_ENV": "production"}
},
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run mocha",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "/Users/gp/Projects/evol_dealermgmt/node_modules/mocha/bin/_mocha",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": ["test/unit.js"],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Environment variables passed to the program.
"env": { "NODE_ENV": "production"}
}
]
}
@limafelipe

Copy link
Copy Markdown

@cameronwp Thanks man, I really appreciate it!!!

@jhenaoz

jhenaoz commented Oct 20, 2016

Copy link
Copy Markdown

@cameronwp thanks!

@fruscitti

Copy link
Copy Markdown

Hi, thanks for the script! I have a question: I'm running mocha with the following parameters:
"args": ["--compilers js:babel-core/register", "--require ./test/test_helper.js"],

and I get the following error on vscode:

node --debug-brk=48652 node_modules\mocha\bin_mocha '--compilers js:babel-core/register' '--require ./test/test_helper.js'
Debugger listening on 127.0.0.1:48652
error: unknown option `--compilers js:babel-core/register'

But running it from the command line without the quotes works. So do you know how to pass parameter to mocha without the single quotes?

Thanks again.

@roa-nyx

roa-nyx commented Nov 14, 2016

Copy link
Copy Markdown

@fruscitti: All my parameters get through without any quotes. Can you try the same thing with all extensions disabled? And can you make sure that your launch configuration matches cameronwp's snippet exactly other then the args you'd like to be passed through?

@jackbrown

Copy link
Copy Markdown

@fruscitti i got this working by passing the args through like this:

"args": ["--compilers", "js:babel-core/register", "--require",  "./test/test_helper.js"],

@bnek

bnek commented Dec 2, 2016

Copy link
Copy Markdown

@jackbrown +1
that worked for me

@swaghmare

Copy link
Copy Markdown

@jackbrown Thanks a lot!! 👍

@M3ssman

M3ssman commented Jan 5, 2017

Copy link
Copy Markdown

Hello anybody,
and Thanks for your Suggestions!

Unfortunately, I can't get VS Code (1.8.1.) respecting my Breakpoints using the above, not on Windows (Win10), neither on Ubuntu (16.04 LTS).
All my Tests are properly located and executed, but the Launch ignores any Breakpoints. When I enable the "stopOnEntry", it halts immediately on Line 8 inside the mocha-Script, and I can step from there Line-by-Line, but that is obviously not what I aim for.
Platform is NodeJS 6.9.3 and TSC 2.0.x - 2.1.x.

Is this only working on Mac, doesn't like TypeScript or what's the reason?

@omnibusinc

Copy link
Copy Markdown

@M3ssman:
It's working for me in Win10 (VSCode version 1.8.1) using the configuration that @cameronwp posted above:

{
        "name": "Run mocha",
        "type": "node",
        "request": "launch",
        "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
	"stopOnEntry": false,
	"args": ["test/**/*.js", "--no-timeouts"],
	"cwd": "${workspaceRoot}",
	"runtimeExecutable": null,
	"env": { "NODE_ENV": "testing"}
}

@reyou

reyou commented Jul 5, 2017

Copy link
Copy Markdown

@cameronwp that should be sent as PR to this repo.
Since once you google "run mocha on VS code" this page is coming in first place.

@iolevsky

iolevsky commented Jul 11, 2017

Copy link
Copy Markdown

For TypeScript make sure that the launch config enables source maps if you want to use breakpoints.

 {
     "name": "Run mocha",
     "type": "node",
     "request": "launch",
     "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
     "stopOnEntry": false,

     "sourceMaps": true,

     "args": ["test/**/*.js", "--no-timeouts"],
     "cwd": "${workspaceRoot}",
     "runtimeExecutable": null,
     "env": { "NODE_ENV": "testing"}
 }

@akobler

akobler commented Jul 12, 2017

Copy link
Copy Markdown

To run only the file you have currently open (the active tab). Very convenient workflow:

  • open file
  • set breakpoint
  • hit F5
        {
            "name": "Run mocha",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
            "stopOnEntry": false,

            "args": ["${file}", "--no-timeouts"],

            "cwd": "${workspaceRoot}",
            "runtimeExecutable": null,
            "env": { "NODE_ENV": "testing"}
        }

@M-Yankov

Copy link
Copy Markdown

thanks! it works 👍

@mafshin

mafshin commented Aug 6, 2017

Copy link
Copy Markdown

Thanks @akobler, your solution is great

@joncodo

joncodo commented Aug 24, 2017

Copy link
Copy Markdown

FYI - you need your source maps to be separate files. You can't do this if your source maps are in the .js files themselves.

@M3ssman

M3ssman commented Nov 10, 2017

Copy link
Copy Markdown

FYI on Win10 (VSC 1.18, node 6.9.4 my current working Debugging config:

    {
	"name": "Run mocha",
	"type": "node",
	"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
	"args": ["${workspaceFolder}/test/**/*.spec.js"],
        "request": "launch",
        "outFiles": [
          "${workspaceFolder}/**/*.js"
        ]
    }

@kenjiO

kenjiO commented Nov 22, 2017

Copy link
Copy Markdown

I tried many variations of the things in this thread and wasn't able to get the breakpoints to correspond to the correct lines. Finally I was able to get it working by adding "retainLines": true to my .babelrc file.

@dkent600

dkent600 commented Jan 4, 2018

Copy link
Copy Markdown

I tried many variations of the things in this thread and wasn't able to get the breakpoints to correspond to the correct lines. Finally I was able to get it working by adding "retainLines": true to my .babelrc file.

@kenjiO Better, for me, but still no cigar. :-( Some breakpoints are still on the wrong line...

@Jasonlhy

Copy link
Copy Markdown

@M3ssman Thanks, works perfectly well

@joseAndresGomezTovar

Copy link
Copy Markdown

It's working for me in Win10 (VSCode version 1.20.1).
Using the gui interface:
1-debug tap
2-add new configuration
3-mocha tests

this is my launch.json file

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Mocha Tests",
            "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
            "args": [
                "-u",
                "tdd",
                "--timeout",
                "999999",
                "--colors",
                "${workspaceFolder}/test",
                "test/**/*.js"
            ],
            "internalConsoleOptions": "openOnSessionStart"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Iniciar programa",
            "program": "${workspaceFolder}\\bin\\www"
        }
    ]
}

@andrewking0207

Copy link
Copy Markdown

@kenjiO Thanks very much! That was the piece I needed.

@caiofcm

caiofcm commented Mar 1, 2018

Copy link
Copy Markdown

For ES6 this configuration worked for me reference

{
    "name": "Run ES6 Tests",
    "type": "node",
    "request": "launch",
    "cwd": "${workspaceRoot}",
    "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
    "args": [
        "./test/*.js",
        "--require", "babel-register",
        "-u", "tdd",
        "--timeout", "999999",
        "--colors"
    ],
    "runtimeArgs": [
        "--nolazy"
    ],
    "sourceMaps": true
},

@gabenitez

gabenitez commented Sep 16, 2018

Copy link
Copy Markdown

For mocha 8 and VS Code 1.27.2 on OSX this worked for me (needed to specify ports)

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Iniciar prueba actual",
            "program": "[..path to mocha 8]/v8.11.1/bin/mocha",            
            "args": ["${file}", "--no-timeouts","--inspect-brk=9229"],
            "port": 9229
        }
    ]
}

Moreover, take into account that this launches the actual opened file... not a specific one. Modify ${file} to match your tests path if you want to.

"args": ["${file}", "--no-timeouts","--inspect-brk=9229"],

@ivanpersan

Copy link
Copy Markdown

@fruscitti i got this working by passing the args through like this:

"args": ["--compilers", "js:babel-core/register", "--require",  "./test/test_helper.js"],

Simple and works perfect, thanks. Only to say that --compilers is now deprecated and you have to use --required instead (Mocha warn you about that).

@conorw89

Copy link
Copy Markdown

Does anyone know how to configure this to debug when executing tests via a test script? Setup is:

  • "test" config in package.json of project specifying the mocha command to execute
  • 'npm test' command used in internal terminal of vscode with '-g' param to allow for execution of specific descriptions within coffescript files

I want to be able to debug the execution of these individual tests(i.e. run 'npm test -- -g ' in vscode and break when it reaches a bp). Is this possible? Would an 'attach' config be needed instead of 'launch'? I've tried the standard debug configs provided in vscode, and tried to modify them based on info found in various places, but no success so far. Any help would be great.

@bhflm

bhflm commented Mar 20, 2019

Copy link
Copy Markdown

i love you <3 thanks

@bfelbo

bfelbo commented Jul 23, 2019

Copy link
Copy Markdown

Is there some way to make VS Code interpret a failed test as a breakpoint so one can quickly see more details about what went wrong?

I'm already using the --bail mocha argument, but that just makes the debugger finish after a test fails.

@TimHeap46

Copy link
Copy Markdown

not working for me, I get : ReferenceError: describe is not defined using the config above.

@nsa-itv

nsa-itv commented Sep 1, 2019

Copy link
Copy Markdown

Didn't work for me - if I add the =xxxx (port number) after --inspect-brk, then I get SyntaxError: .../.vscode/launch.json: Unexpected token / in JSON at position 6.

If I don't add it, it tries port 9229, which is not available, regardless of the separate port: xxxx in the json configuration.

@e5pe

e5pe commented Aug 7, 2020

Copy link
Copy Markdown

FYI on Win10 (VSC 1.18, node 6.9.4 my current working Debugging config:

    {
	"name": "Run mocha",
	"type": "node",
	"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
	"args": ["${workspaceFolder}/test/**/*.spec.js"],
        "request": "launch",
        "outFiles": [
          "${workspaceFolder}/**/*.js"
        ]
    }

Thank you! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment