Reputation: 2433
I want to debug Karma tests in VS Code but I did not find out how. Is there any way to do that or will I have to use another IDE (WebStorm)?
Upvotes: 75
Views: 87920
Reputation: 1
Use karma-test-explorer extension to View and run your Karma or Angular tests in the VS Code Testing side bar.
Upvotes: 0
Reputation: 43
For me, it worked, after adding a tag called
debbuger: true
on karma.config.js:
Upvotes: 0
Reputation: 15208
Configs for triade VScode - Grunt - Karma:
launch.json
{
"version":"0.2.0",
"configurations":[
{
"type":"pwa-chrome",
"request":"attach",
"name":"Attach to Karma",
"webRoot":"${workspaceRoot}",
"address":"localhost",
"port":9222,
"timeout":180000, /* ! if required increase this value */
"pathMapping":{
"/":"${workspaceRoot}",
"/base/":"${workspaceRoot}/"
}
},
{
"type":"pwa-node",
"request":"launch",
"name":"test.debug",
"skipFiles":[
"<node_internals>/**"
],
"program":"${file}",
"cwd":"${workspaceFolder}",
"preLaunchTask":"test.debug"
}
],
"compounds":[
{
"name":"# App Angular Unit Tests Debugging",
"configurations":[
"test.debug",
"Attach to Karma"
],
"stopAll":true
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "test.debug",
"type": "shell",
"command": "npx",
"args": [
"grunt",
"unit_test_debug"
]
}
]
}
/* .. */
grunt.initConfig({
karma: {
options: {
configFile: '{path_to_karma_config}/karma.config.js',
files: [
/* ... */
]
},
debugging:{
browsers:['ChromeDebugging'],
singleRun: false
}
},
});
function registerTasks() {
const tasks = [
/* .. */
{
unit_test_debug: ['karma:debugging']
}];
/* .. */
}
}
registerTasks();
module.exports = (config) => {
config.set({
/* .. */
browsers: ['ChromeDebugging'],
customLaunchers: {
/* .. */
ChromeDebugging: {
base: 'ChromeHeadless', // 'Chrome'
flags: ['--remote-debugging-address=0.0.0.0', '--remote-debugging-port=9222', '--disable-web-security'],
}
}
});
};
Upvotes: 0
Reputation: 361
tests must run with source maps for this to work (which is true by default)
ng test --source-map true
add in .vscode/launch.json under configurations:
{
"type": "chrome",
"request": "launch",
"name": "Karma Attach",
"url": "http://localhost:9876/debug.html",
"webRoot": "${workspaceFolder}",
"pathMapping": {
"/_karma_webpack_/": "${workspaceRoot}/"
}
}
Angular CLI: 10.1.2 Node: 12.18.3
Upvotes: 5
Reputation: 6178
I'm using angular/cli: "^8.3.25"
and "karma": "1.3.0"
, Chrome 79
, VSCode 1.43.2
and VSCode extension Debugger for Chrome 4.12.6
on Windows 10.
Previously we didn't need to debug those karma unit tests. With the below settings in karma.config.js
. It's all good by running ng test --watch=true
.
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadless'],
browserNoActivityTimeout: 30000,
singleRun: true
Recently we needed to debug the unit tests in VSCode. To do so, change the settings in karma.config.js
from the above to the below.
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: [
'ChromeDebug'
],
browserNoActivityTimeout: 30000,
singleRun: true,
customLaunchers: {
ChromeDebug: {
base: 'Chrome',
flags: [
'--no-sandbox',
'--remote-debugging-port=9333'
]
}
}
Note, the flag of --no-sandbox
here. Somehow for other teammates they don't need this flag. But for me, without it I have issues to launch the Chrome instance properly as shown below.
Below is the successfully launched Chrome instance with the flag of --no-sandbox
:
To debug the unit tests in VSCode, add the following settings to VSCode's launch.json
.
{
"type": "chrome",
"request": "attach",
"name": "Debug Unit Tests",
"address": "localhost",
"port": 9333,
"webRoot": "${workspaceFolder}/src",
"sourceMaps": true,
"sourceMapPathOverrides": {
"*": "${webRoot}/*"
}
},
Note, you might need to change the value of the folders specified in the above settings to match yours.
Now the Chrome is launched after running ng test --watch=true
. Then from VSCode, Select the Debug Unit Tests
option in the Debug panel, hit F5 to attach to the unit tests' running session. Go back to the launched Chrome instance, hit F5 to refresh and re-run the tests, now the breakpoints should be able to hit.
Upvotes: 3
Reputation: 5
Try
{
"name": "jasmine",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/jasmine/bin/jasmine.js",
"stopOnEntry": false,
"args": [
"JASMINE_CONFIG_PATH=${workspaceRoot}/spec/support/jasmine.json"
],
"cwd": "${workspaceRoot}",
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
}
}
Upvotes: -2
Reputation: 3401
You can debug Karma by attaching the debugger to a Chrome instance. You'd want to set your launch.json
config to something like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "Attach Karma Chrome",
"address": "localhost",
"port": 9333,
"pathMapping": {
"/": "${workspaceRoot}/",
"/base/": "${workspaceRoot}/"
}
}
]
}
But you also need to adjust your karma.conf.js config
, so that it launches Chrome instance with dev tools listening to 9333
port, like so:
browsers: [
'ChromeDebugging'
],
customLaunchers: {
ChromeDebugging: {
base: 'Chrome',
flags: [ '--remote-debugging-port=9333' ]
}
},
With such setup you can just run your karma server (with captured browser), and then start debugging in visual studio.
If you'd like to find more details I made a tutorial on debugging Karma with Visual Studio Code.
Upvotes: 72
Reputation: 1131
I followed @Awsed excellent explanation [Thank you!] and was able to get things working, with a couple of notable caveats:
There seems to be a bug within VSCode that has trouble tracking the breakpoint lines in spec files. I wasn't hitting my breakpoint even after the correct setup because VSCode seems to be confused about what line the breakpoint is on, especially if you change any code. I could find no remedy for this (even after two reboots and restarting VSCode multiple times). I was only able to discover this by going to a test file that hadn't been changed in a while and was able to successfully hit a breakpoint. So then I moved the breakpoint to different locations in the page on the file I was having issues with and was able to find a location that it would (finally) hit the breakpoint.
Additionally, I do not recommend running Chrome headless as the "stop" button does not kill the process and you can't find it in task manager, since it is headless -- you have to use a command to kill it instead (e.g. https://superuser.com/questions/1288388/how-can-i-kill-all-headless-chrome-instances-from-the-command-line-on-windows ); if you don't, you won't be able to run it again.
Upvotes: 0
Reputation: 9214
Using Angular CLI 1.7.4: With the following steps I was able to debug a hello world Angular application with Visual Studio Code:
Generate a fresh HelloWorld project:
ng new HelloWorld
Open the project in Visual Studio Code
code HelloWorld
Create a new Debug configuration:
A .vscode/launch.json
file is generated and opened. Replace its content by the following:
{
// Use IntelliSense to learn about possible 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": "chrome",
"request": "launch",
"name": "Karma Tests",
"sourceMaps": true,
"webRoot": "${workspaceRoot}",
"url": "http://localhost:9876/debug.html",
// "runtimeArgs": [
// "--headless"
// ],
"pathMapping": {
"/": "${workspaceRoot}",
"/base/": "${workspaceRoot}/"
},
"sourceMapPathOverrides": {
"webpack:///./*": "${webRoot}/*",
"webpack:///src/*": "${webRoot}/*",
"webpack:///*": "*",
"webpack:///./~/*": "${webRoot}/node_modules/*",
"meteor://💻app/*": "${webRoot}/*"
}
}
]
}
Open karma.conf.js
and perform the following change:
Open a terminal and start karma tests:
ng test
Open app.component.spec.ts
and set a break point:
Select "Karma Tests" in the debug menu:
Press F5
to start debugging. VSCode should stop at the breakpoint:
Upvotes: 36
Reputation: 6820
Here's a simpler configuration (in launch.json
):
{
"type": "chrome",
"request": "launch",
"name": "Test",
"sourceMaps": true,
"webRoot": "${workspaceRoot}/Test",
"url": "http://localhost:9876/debug.html",
"runtimeArgs": [
"--headless"
]
}
Important:
webRoot
to the folder where Karma is serving your tests from.url
accordingly.This configuration is simpler for a number of reasons:
singleRun: false
. You could even set browsers: []
, since VS Code will launch its own browser (in headless mode, so you won't see it).preLaunchTask
that starts Karma automatically. You'll need to configure it as a background task.Upvotes: 10