Running Mocha tests compiled with Babel in Visual

2019-03-19 17:49发布

问题:

I am using Babel in my Mocha tests. To run the test in terminal I use following command:

mocha --debug --compilers js:babel/register

Then I can use VS Code "Attach" debugging option to attach to the test process. I can set breakpoints and it stops, but because original code is in ES6 VS Code gets confused about line numbers and such.

Is there anyway to make VS Code work with this setup?

My "Attach" config:

    {
        "name": "Attach",
        "type": "node",
        // TCP/IP address. Default is "localhost".
        "address": "localhost",
        // Port to attach to.
        "port": 5858,
        "sourceMaps": false
    }

"sourceMaps": true doesn't make any difference

The project I'm trying to run the test is open source. GitHub repo: https://github.com/mohsen1/yawn-yaml/

回答1:

I got mocha running with babel locally using this config:

"configurations": [
    {
        "name": "Debug Mocha",
        "type": "node",
        "program": "./node_modules/.bin/_mocha",
        "stopOnEntry": false,
        "args": ["--compilers", "js:babel-register"],
        "cwd": ".",
        "runtimeExecutable": null,
        "runtimeArgs": [
            "--nolazy"
        ],
        "env": {
            "NODE_ENV": "development"
        },
        "externalConsole": false,
        "sourceMaps": true,
        "outDir": null
    },
    {
        "name": "Attach",
        "type": "node",
        "request": "attach",
        "port": 5858
    }
]

Which uses the _mocha executable since node is already invoked by Code. Also, make sure you have sourceMaps set to true.