Skip to content

Instantly share code, notes, and snippets.

@paambaati
Last active May 5, 2022 05:35
Show Gist options
  • Save paambaati/54d33e409b4f7cf059cc to your computer and use it in GitHub Desktop.
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"}
}
]
}
@omnibusinc
Copy link

@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

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

@M3ssman Thanks, works perfectly well

@joseAndresGomezTovar
Copy link

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

@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

@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

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

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