Debugging Mocha tests in VS Code using launch.json not working

  Kiến thức lập trình

I’m trying to debug my mocha tests in VS Code using a launch.json, but this error shows up:

ReferenceError: before is not defined

I also found that I get these errors when I move around and comment out sections of code:

ReferenceError: describe is not defined

ReferenceError: it is not defined

The before, it, and describe are mocha functions. I checked that mocha is being launched and it is. The debugger works fine but once it gets to a mocha function it throws an error.

Here’s my launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "args": [
                "-u",
                "tdd",
                "--timeout",
                "999999",
                "--colors",
                "${workspaceFolder}/test/**/*.test.js" // <-- minor change from default config
            ],
            "internalConsoleOptions": "openOnSessionStart",
            "name": "Mocha Tests",
            "program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js", // <-- mocha
            "request": "launch",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "type": "node"
        }
    ]
}

This is the default configuration for mocha that VS Code automatically generates.

Theirs two different interfaces for Mocha: BDD, and TDD. BDD is the interface that has the ‘describe’, ‘it’, and ‘before’ function names. TDD has the same functions but with different names.

You are using the BDD interface in your code, but are telling VS Code in your launch.json to launch Mocha with the TDD interface!

This is what your launch.json should be:

{
    "version": "0.2.0",
    "configurations": [
        {
            "args": [
                "-u",
                "bdd", // <- SOLUTION
                "--timeout",
                "999999",
                "--colors",
                "${workspaceFolder}/test/**/*.test.js" // <-- minor change from default config
            ],
            "internalConsoleOptions": "openOnSessionStart",
            "name": "Mocha Tests",
            "program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js", // <-- mocha
            "request": "launch",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "type": "node"
        }
    ]
}

https://mochajs.org/#command-line-usage

https://joshldavis.com/2013/05/27/difference-between-tdd-and-bdd/

LEAVE A COMMENT