Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug nightwatch tests in VS Code

I'm trying to debug nightwatch e2e tests using VS Code. I write my tests using typescript. It can work only when I put a breakpoint in js file, after that it goes to ts file and I can debug it from there. If I put it in ts file of my test - it will never stop and it is written "“Breakpoint ignored because generated code not found”. My source files are compiled using ts compiler to folder /dist/dev/specs/e2e/nightwatch/src. Code from the launch.json

        "name": "Launch e2e Tests on chrome",
        "type": "node",
        "console": "integratedTerminal",
        "program": "${workspaceRoot}/dist/dev/specs/e2e/nightwatch/nightwatch.js",
        "stopOnEntry": false,.
        "args": ["-env default,-f DatabaseChecks.js"],
        "cwd": "${workspaceRoot}",
        "runtimeExecutable": null,.
        "runtimeArgs": ["--nolazy"],
        "env": {
            "NODE_ENV": "development"
        },
        "sourceMaps": true,
        "outFiles": ["${workspaceRoot}/dist/dev/specs/e2e/nightwatch/src"],
        "request": "launch"

Maybe someone had similar problem? Any help would be appreciated.

like image 983
Katia Avatar asked May 04 '17 13:05

Katia


People also ask

Does VS code have a debugger?

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop.


2 Answers

following is working like charm in my case.

Here is the project structure. following is my launch.json.

 {
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
                   {
                      "type": "node",
                      "request": "launch",
                      "name": "Nightwatch",
                      "program": "${workspaceRoot}/node_modules/nightwatch/bin/runner.js",
                      "stopOnEntry": false,
                      "args": [
                                 "--test",
                                 "tests/functionality_e2e_test.js"
                              ],           
                       "runtimeExecutable": null,
                       "sourceMaps": false
                  },
                  {
                       "type": "node",
                       "request": "attach",
                       "name": "Attach to Process",
                       "port": 5858
                  }
                 ]
}

above code is the bare minimum requirement to debug Nightwatch js project in visual studio code latest version 1.21.1. I am using node.js v6.11.2. so debugging protocol is legacy.

Thank You Stack Overflow.

like image 96
Sameer Amrutia Avatar answered Nov 01 '22 02:11

Sameer Amrutia


One thing that usually helps me in cases when I have to debug server side node.js aps - is to use gulp-sourcemaps and there play around with generated source paths (check value of the "sources" property in your js.map files) by making them absolute and perfectly matching your ts files locations:

For example:

gulp.task('build', () => 
{
    var tsProject = ts.createProject('tsconfig.json', {
        typescript: require('typescript')
    });

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(tsProject()); 

        //Write compiled js
    return tsResult.js.pipe(sourcemaps.write(
            ".", 
            { 
                includeContent: false, 
                mapSources: function(sourcePath) 
                {
                    //Play around here - converting your relative paths to absolute ones that match location of ts file perfectly 
                    return sourcePath.replace('../../../', __dirname + '/');
                } 
            })).pipe(gulp.dest(TEMP_TARGET_FOLDER));
});

Although it is a bit hackish - it works for me every time and is quite simple to setup.

like image 29
Amid Avatar answered Nov 01 '22 02:11

Amid