简体   繁体   English

Angular(ng 测试)调试 - VS 代码不会在断点处停止

[英]Angular (ng test) Debugging - VS Code not stopping on Breakpoints

I am currently having a problem.我目前遇到问题。 I started writing Tests for my Angular application and wanted to debug them.我开始为我的 Angular 应用程序编写测试并想调试它们。 Now I've googled alot, I tried the recipies from microsoft ( https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI ) and the closest I got to making it work was this BlogPost现在我已经用谷歌搜索了很多,我尝试了来自微软的食谱( https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI ),我最接近让它工作的是这个 BlogPost

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

Now at least I can attach the Debugger to VS-Code.现在至少我可以将调试器附加到 VS-Code。 However VS Code does still not stop on Breakpoints, but the tests just continue to run.然而 VS Code 仍然不会在断点处停止,但测试会继续运行。 The Breakpoints in VS Code will also remain unverified (see image) VS Code 中的断点也将保持未验证状态(见图)

未验证的断点

This is what I have so far (I only supply the parts i have changed, to not post too much code).这是我到目前为止所拥有的(我只提供我已经更改的部分,而不是发布太多代码)。

Any Ideas what I am doing wrong?任何想法我做错了什么? Apart from that debbuging works just fine.除此之外,调试工作正常。 I can debug my node.js applications and debugging ng serve also works fine.我可以调试我的 node.js 应用程序,调试 ng serve 也能正常工作。

launch.json启动.json

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

karma.conf.js业力.conf.js

browsers: [
    'ChromeDebugging'
],

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

Have you installed "Debugger for Chrome" extension.您是否安装了“Debugger for Chrome”扩展程序。

Look at this guide.看看这个指南。 https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI https://github.com/Microsoft/vscode-recipes/tree/master/Angular-CLI

UPDATE: This is my launch.json Maybe you can try it.更新:这是我的 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 conf因果报应

// 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
  });
};

Try this configuration, it attaches to a running ng test process with the port definded in your karma.conf.试试这个配置,它附加到一个正在运行的ng test进程,端口在你的 karma.conf 中定义。 If you have multiple browsers definde in your karma.conf, you might have to start ng test with ng test --browser=ChromeDebugging如果您在 karma.conf 中定义了多个浏览器,则可能必须使用ng test --browser=ChromeDebugging启动 ng test

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

Had this problem recently in VSCode & Intellij, it has to do with source map generation in Webpack that Angular CLi uses under the hood.最近在 VSCode 和 Intellij 中遇到了这个问题,它与 Angular CLi 在幕后使用的 Webpack 中的源映射生成有关。 I fixed the problem by setting evalSourceMap: "false" in angular.json, see the complete answer here https://stackoverflow.com/a/54883663/706012 .我通过在 angular.json 中设置 evalSourceMap: "false" 解决了这个问题,请在此处查看完整答案https://stackoverflow.com/a/54883663/706012

在此处输入图像描述

Add 'pathMapping' to launch.json as below:将“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/*"
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM