mocha-webpack icon indicating copy to clipboard operation
mocha-webpack copied to clipboard

Use with VS Code

Open notenoughneon opened this issue 8 years ago • 9 comments

I'd like to use Visual Studio Code's debugger with mocha-webpack tests. VS Code can debug mocha tests, but you have to tell it to use "node_modules/mocha/bin/_mocha" and not "mocha".

For mocha-webpack I tried both "node_modules/mocha-webpack/bin/mocha-webpack" and "node_modules/mocha-webpack/bin/_mocha" and it would run the tests but not attach the debugger. Not sure what is going wrong. Possibly related to #19

notenoughneon avatar Jul 07 '16 15:07 notenoughneon

I don't use VS Code, but it's perhaps similiar to Intellij's mocha plugin.

The following config works for me. Have a look at devtoolModuleFilenameTemplate, devtoolFallbackModuleFilenameTemplate and devtool.

webpack.config.test.js

module.exports = {
  ....
  output: {
    devtoolModuleFilenameTemplate: '[absolute-resource-path]',
    devtoolFallbackModuleFilenameTemplate: '[absolute-resource-path]?[hash]'
  }
  target: 'node',
  devtool: "source-map"
};

Moreover i had to include source-map-support with --require source-map-support/register in my mocha-webpack.opts or as command argument.

zinserjan avatar Jul 07 '16 17:07 zinserjan

Thanks for the suggestion. The devtool- options fixed VS Code debugging of my manual test build, but Code still doesn't attach to mocha-webpack.

I tried "--require source-map-support/register", but it breaks both mocha and mocha-webpack with the following error:

node_modules/source-map-support/source-map-support.js:411
      var hasStack = (arguments[1] && arguments[1].stack);
                                                  ^
SyntaxError: 

notenoughneon avatar Jul 07 '16 20:07 notenoughneon

I managed to fix the source-map-support syntax error by adding the 'node' environment option: evanw/node-source-map-support#137

The VS Code attachment issue remains though.

notenoughneon avatar Jul 08 '16 18:07 notenoughneon

+1 it would be useful to have a --debug-brk option to run mocha in debug mode The workaround is to compile with webpack, run mocha with --debug-brk and the attach VS Code debugger

correttojs avatar Jul 19 '16 10:07 correttojs

--debug-brk option isn't necessary. Mocha have this option only cause they are spawning another mocha process (see here). mocha-webpack does not spawn another process, so --debug-brk should work without further adjustments.

zinserjan avatar Jul 19 '16 18:07 zinserjan

Are you manage to run it? I'm getting an error:

Module parse failed: Unexpected token You may need an appropriate loader to handle this file type

but it's the same webpack config that I run in the console. what can be the problem here?

doronbrikman avatar Sep 14 '16 16:09 doronbrikman

Does anybody have any success with that?

rosieks avatar Apr 20 '17 16:04 rosieks

+1, not been able to debug tests in vscode using mocha-webpack. Any updates?

xugao avatar Jun 08 '17 17:06 xugao

I have tried the following code is feasible, but I don't know why.

webpack.config.js

if (process.env.NODE_ENV === "test") {
    //If you would to use breakpoint in vscode, then must be set devtool to "eval" base
    config.devtool = "eval"; 
    config.output = Object.assign(config.output, {
        devtoolModuleFilenameTemplate: "[absolute-resource-path]",
    });
}

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Mocha-webpack Tests",
        "program": "${workspaceFolder}/node_modules/mocha-webpack/bin/mocha-webpack",
        "args": [
            "--full-trace",
            "--timeout",
            "999999",
            "--colors",
            "tests/**/*.js"
        ],
        "sourceMaps": true,
        "env": {
            "NODE_ENV": "test"
        },
        "internalConsoleOptions": "openOnSessionStart"
    }]
}

lpreterite avatar Nov 22 '18 14:11 lpreterite