GutiMac
GutiMac

Reputation: 2472

How to debug with visual studio code a mocha test in javascript?

If you are using node, it is easy to debug:

//.vscode/launch.json
"configurations": [
    {
        "name": "Launch",
        "type": "node",
        "request": "launch",
        ...

But if I am using mocha tests, how can I debug it?

I have tried to use:

"configurations": [
    {
        "name": "Launch",
        "type": "mocha",
        "request": "launch",

But it is not valid. Does anyone have any idea?

Upvotes: 1

Views: 2051

Answers (1)

GutiMac
GutiMac

Reputation: 2472

  1. Create this new debug target in .vscode/launch.json

    {
        "name": "Unit tests",
        "type": "node",
        "program": "${workspaceRoot}/mocha.js",
        "stopOnEntry": true,
        "args": ["${workspaceRoot}/TESTTODEBUG.js"],
        "runtimeExecutable": null,
        "env": {
            "NODE_ENV": "test"
         }
    
  2. Create the file mocha.js

    'use strict';
    
    // Dependencies
    var Mocha = require('mocha');
    
    // Determine which tests to run based on argument passed to runner
    var args = process.argv.splice(2);
    //var args = ["./tests/unit/services/supra-statement.service.test.js"];
    var files;
    
    
    
    //Define Mocha
    var mocha = new Mocha({
      timeout: 60000,
      reporter: 'spec',
      globals: ['Associations', 'CREATE_TEST_WATERLINE', 'DELETE_TEST_WATERLINE']
    });
    
    args.forEach(mocha.addFile.bind(mocha));
    
    //Run unit tests
    mocha.run(function (failures) {
        process.exit(failures);
    });
    
  3. Run the debugger with the Unit test Option

Upvotes: 4

Related Questions