Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

limafelipe commented Sep 13, 2016

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

@jhenaoz
Copy link

jhenaoz commented Oct 20, 2016

@cameronwp thanks!

@fruscitti
Copy link

fruscitti commented Nov 3, 2016

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
Copy link

roa-nyx commented Nov 14, 2016

@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

jackbrown commented Nov 28, 2016

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

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

@bnek
Copy link

bnek commented Dec 2, 2016

@jackbrown +1
that worked for me

@swaghmare
Copy link

swaghmare commented Dec 14, 2016

@jackbrown Thanks a lot!! 👍

@M3ssman
Copy link

M3ssman commented Jan 5, 2017

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

omnibusinc commented Jan 26, 2017

@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
Copy link

reyou commented Jul 5, 2017

@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
Copy link

iolevsky commented Jul 11, 2017

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
Copy link

akobler commented Jul 12, 2017

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

M-Yankov commented Jul 24, 2017

thanks! it works 👍

@mafshin
Copy link

mafshin commented Aug 6, 2017

Thanks @akobler, your solution is great

@joncodo
Copy link

joncodo commented Aug 24, 2017

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
Copy link

M3ssman commented Nov 10, 2017

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
Copy link

kenjiO commented Nov 22, 2017

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
Copy link

dkent600 commented Jan 4, 2018

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

Jasonlhy commented Jan 11, 2018

@M3ssman Thanks, works perfectly well

@joseAndresGomezTovar
Copy link

joseAndresGomezTovar commented Feb 18, 2018

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

andrewking0207 commented Feb 23, 2018

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

@caiofcm
Copy link

caiofcm commented Mar 1, 2018

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
Copy link

gabenitez commented Sep 16, 2018

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

ivanpersan commented Sep 19, 2018

@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

conorw89 commented Nov 29, 2018

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
Copy link

bhflm commented Mar 20, 2019

i love you <3 thanks

@bfelbo
Copy link

bfelbo commented Jul 23, 2019

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

TimHeap46 commented Aug 9, 2019

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

@nsa-itv
Copy link

nsa-itv commented Sep 1, 2019

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
Copy link

e5pe commented Aug 7, 2020

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