我目前遇到了问题。我开始为我的Angular应用程序编写Tests并想要调试它们。现在我用谷歌搜索了很多,我尝试了微软(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中的断点也将保持未验证状态(参见图像)
这是我到目前为止(我只提供我已更改的部分,不发布太多代码)。
任何想法我做错了什么?除了调试工作正常。我可以调试我的node.js应用程序和调试ng服务也可以正常工作。
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']
}
}
您是否安装了“Debugger for 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 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
});
};
尝试这个配置,它会附加到正在运行的ng test
进程,并在您的karma.conf中定义端口。如果你的karma.conf中有多个浏览器,你可能需要用ng test --browser=ChromeDebugging
开始测试
{
"name": "ng test",
"type": "chrome",
"request": "attach",
"address": "localhost",
"port": 9222,
"webRoot": "${workspaceFolder}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"/./*": "${webRoot}/*",
"/src/*": "${webRoot}/*",
"/*": "*",
"/./~/*": "${webRoot}/node_modules/*"
}
}
最近在VSCode和Intellij中出现了这个问题,它与Angular CLi使用的Webpack中的源映射生成有关。我通过在angular.json中设置evalSourceMap:“false”来修复问题,请在此处查看完整答案https://stackoverflow.com/a/54883663/706012。