How to debug nightwatch tests in VS Code

百般思念 提交于 2019-12-05 08:28:51

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.

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.

You can use this configuration if you don't need the Typescript part (Based on WebStorm suggestion):

{
"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "${workspaceRoot}/node_modules/nightwatch/bin/runner.js",
        "args": [
          "--config",
          "test/e2e/nightwatch.conf.js"
        ]
    }
]

}

jlm

I use that one :

{
    "type": "node",
    "request": "launch",
    "name": "Nightwatch 2",
    "port": 9229,
    "program": "${workspaceRoot}/node_modules/nightwatch/bin/nightwatch",
    "stopOnEntry": false,
    "args": [
        "--env",
        "localchrome"
    ],
    "cwd": "${workspaceRoot}",
    "sourceMaps": false,
    "env": {
        "ENV": "s11",
        "TAGS": "@WIP"
    },
    "runtimeArgs": [
        "--inspect"
    ]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!