角度(ng测试)调试-VS代码未在断点处停止

时间:2018-12-13 07:06:06

标签: angular debugging visual-studio-code karma-coverage

我目前有问题。我开始为Angular应用程序编写测试,并希望对其进行调试。现在,我已经在Google上搜索了很多,我尝试了来自Microsoft(https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI)的建议,而使它起作用的最接近的方法是此BlogPost

http://blog.mlewandowski.com/Debugging-Karma-tests-with-VSCode.html

现在至少我可以将调试器附加到VS-Code。但是,VS Code仍不会在断点处停止,而是可以继续运行测试。 VS Code中的断点也将保持未经验证(见图)

Unverified Breakpoint

到目前为止,这就是我所拥有的(我只提供我已更改的部分,而不发布过多的代码)。

任何想法我在做什么错?除此之外,调试工作还不错。我可以调试自己的node.js应用程序,并且调试ng serve也可以正常工作。

launch.json

{
    "type": "chrome",
    "request": "attach",
    "name": "MyApp - Tests",
    "address": "localhost",
    "port": 9222,
    "pathMapping": {
        "/": "${workspaceRoot}",
        "/base/": "${workspaceRoot}"
    }
}

karma.conf.js

browsers: [
    'ChromeDebugging'
],

customLaunchers: {
    ChromeDebugging: {
        base: 'Chrome',
        flags: ['--remote-debugging-port=9222']
    }
}

4 个答案:

答案 0 :(得分:1)

尝试此配置,它将附加到正在运行的ng test进程中,并在您的karma.conf中定义端口。 如果您在karma.conf中定义了多个浏览器,则可能必须以ng test --browser=ChromeDebugging

开始ng测试。
{
        "name": "ng test",
        "type": "chrome",
        "request": "attach",
        "address": "localhost",
        "port": 9222,
        "webRoot": "${workspaceFolder}",
        "sourceMaps": true,
        "sourceMapPathOverrides": {
            "/./*": "${webRoot}/*",
            "/src/*": "${webRoot}/*",
            "/*": "*",
            "/./~/*": "${webRoot}/node_modules/*"
        }
}

答案 1 :(得分:0)

您是否已安装了“ Chrome调试器”扩展程序。

请参阅本指南。 https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI

更新: 这是我的launch.json也许您可以尝试一下。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "ng serve",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:4200/#",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "ng test",
      "type": "chrome",
      "request": "launch",
      "url": "http://localhost:9876/debug.html",
      "webRoot": "${workspaceFolder}"
    },
    {
      "name": "ng e2e",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/protractor/bin/protractor",
      "protocol": "inspector",
      "args": ["${workspaceFolder}/e2e/protractor.conf.js"]
    }
  ]
}

业力会议

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

答案 2 :(得分:0)

最近在VSCode和Intellij中出现了此问题,这与Angular CLi在后台使用的Webpack中的源地图生成有关。我通过在angular.json中设置evalSourceMap:“ false”来解决此问题,请在此处https://stackoverflow.com/a/54883663/706012中查看完整的答案。

enter image description here

答案 3 :(得分:0)

将 'pathMapping' 添加到 launch.json 如下:

{
        "name": "ng test",
        "type": "chrome",
        "request": "launch",
        "url": "http://localhost:9876/debug.html",
        "webRoot": "${workspaceFolder}",
        "sourceMaps": true,
        "pathMapping": {
            "/_karma_webpack_": "${workspaceFolder}"
        },
        "sourceMapPathOverrides": {
            "webpack:/*": "${webRoot}/*",
            "/./*": "${webRoot}/*",
            "/src/*": "${webRoot}/*",
            "/*": "*",
            "/./~/*": "${webRoot}/node_modules/*"
        }
    }