How to debug Unit Tests with Karma/Jasmine in Visual Studio Code? - debugging

I'd like to be able to debug unit tests in Visual Studio Code, but so far it has been a mixed bag.
My setup:
launch.json
{
"version": "0.2.0",
"configurations": [
         {
             "name": "Debug tests",
             "type": "chrome",
             "request": "attach",
             "port": 9222,
             "sourceMaps": true,
             "webRoot": "${workspaceRoot}"
         }
]
}
karma.config.js
customLaunchers: {
Chrome_with_debugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9222']
}
}
This does seem to work in a way, if I launch the VS Code debugger it appears to attach (bottom bar turns orange). If I make a change, Karma kicks in and the debugger, too - but it invariably pauses in zone.js (this is an Angular project by the way) without me interfering in any way:
If I hit 'Continue' it actually hits my breakpoint
and I can inspect some variables but not all of them,
For example, I can't see the value of actual passed into Jasmine's expect method.
So a) Why does the debugger always pause inside zone.js - the tested code is from a Redux reducer and is invoked outside of any Angular context, and b) What am I missing in regards to not being able to inspect local variables (which is a showstopper right now)?

In karma.conf.js I updated added debug option in your version.
customLaunchers: {
Chrome_with_debugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9222'],
debug: true
}}
launch.json
Add below snippet as launch configuration,
{
"name": "Debug tests",
"type": "chrome",
"request": "attach",
"port": 9222,
"sourceMaps": true,
"webRoot": "${workspaceRoot}"
}
Then triggered the tests using below command,
ng test --browsers Chrome_with_debugging
Use Visual Studio Code debug option "Debug tests" to get attached to UT. With this I am able to debug unit tests using breakpoints in "Visual Studio Code + Debugger for Chrome extension".

Related

Couldn't start dlv dap

When I launch in VSCode dlv dap debug, I get this message:
Couldn't start dlv dap:
Error:timed out while waiting for DAP server to start
I already have launch configurations for the project:
lunch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"showLog": true,
"env": {
"GO111MODULE": "on"
}
}
]
}
and setting.json is :
{
"folders": [
{
"path": "."
}
],
"settings": {
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.autocompleteUnimportedPackages": true,
"go.gocodePackageLookupMode": "go",
"go.gotoSymbol.includeImports": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"explorer.confirmDelete": false,
"go.formatTool": "goimports",
"go.docsTool": "gogetdoc",
"go.buildFlags": [],
"explorer.confirmDragAndDrop": false,
"window.zoomLevel": 0.8,
"editor.minimap.enabled": false,
"go.useLanguageServer": true,
"go.delveConfig":{
"debugAdapter":"dlv-dap"
},
"[go]": {
"editor.snippetSuggestions": "none",
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"gopls": {
"experimentalWorkspaceModule": true,
"usePlaceholders": true, // add parameter placeholders when completing a function
"completionDocumentation": true // for documentation in completion items
}
},
}
The structure of the project is shown in the figure:
This might be happening due to recent updates to VS Code Go extension.
First options is to fix it by running "Go: Install/Update Tools" command from the Command Palette (Linux/Windows: Ctrl+Shift+P, Mac: ⇧+⌘+P).
Then, mark dlv & dlv-dap from the menu, and hit ok to start install/update.
Delve’s native DAP implementation is under active development, so take advantage of the most recent features and bug fixes by using Delve built from its master branch. The Go extension maintains this newest version of Delve separately from the officially released version of dlv and installs it with the name dlv-dap.
Second option, is to use legacy debug adapter. More on this in the link below ...
Check out the full documentation at https://github.com/golang/vscode-go/blob/master/docs/debugging.md
You might have some luck switching the delveConfig to use legacy mode:
"go.delveConfig":{
"debugAdapter":"legacy"
}
My team and I recently began seeing the same issue after updating VSCode. There's a little more info on this setting here: https://go.googlesource.com/vscode-go/+/HEAD/docs/debugging.md#switching-to-legacy-debug-adapter, but I believe root cause (if this does indeed solve your issue) is going to be your version of Golang is not the version targeted by dlv-dap. Anything below Go version 1.15 needs to use legacy mode, and the latest version of the delve debugger happens to skip legacy mode by default now.
I also needed to kill VSCode before this change took effect. According to the dlv-dap docs, you can also force it into legacy mode by switching launch.json's mode to "remote", so there's likely a few (maybe better) ways to resolve this issue.
For macOS users:
brew install delve
Linux/Windows: Ctrl+Shift+P, Mac: ⇧+⌘+P

Why aren't my Visual Studio Code task executing?

I am struggling with getting my Visual Studios code task to work. The problem is that it seems like the task/.bat file is not being executed.
VS Code task configuration:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "My Label",
"type": "shell",
"windows": {
"command": "'c:\\Program Files (x86)\\path\\to\\the\\file.bat${file}'"
},
"presentation": {
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
For testing purposes the file.bat contains:
echo "------"
echo %1
echo "------"
The output in Visual Studio Code terminal is:
> Executing task in folder User: 'c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc' <
c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc
Terminal will be reused by tasks, press any key to close it.
I expected that the ${file} argument/value to printed in the console. The problem is that nothing is printed. It doesn't matter if I intentionally make syntax errors in the bat file. From VS Code it seems like the .bat file is not executed at all.
The shell being used is PowerShell
Configuration:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
Thank you in advance!
T
You just have a slight syntax error using double quotes and single quotes together.
Change to:
...
"windows": {
"command": "c:\\Program Files (x86)\\path\\to\\the\\file.bat ${file}"
}
...

Performance Issues - Chutzpah Console.exe and TypeScript

I am running into some performance issues trying to run the chutzpah.console.exe with my chutzpah.json file. For this project, I am using TypeScript and pre-compiling the TypeScript files into .js files before running the tests. Each test file that I have generally references one other TypeScript file that it depends on - this is so Visual Studio is happy and so that the Chutzpah extensions work. For example, my drawerBusiness.spec.ts file contains this reference:
/// <reference path="../../../../app/business/documents/drawerBusiness.ts"/>
My chutzpah.json file is as follows:
{
"Compile": {
"Mode": "External",
"Extensions": [ ".ts" ],
"ExtensionsWithNoOutput": [ ".d.ts" ]
},
"Framework": "jasmine",
"TestHarnessReferenceMode": "Normal",
"TestHarnessLocationMode": "SettingsFileAdjacent",
"References": [
{ "Path": "./lib/jquery/jquery.js" },
{ "Path": "./lib/angular/angular.js" },
{ "Path": "./lib/angular-mocks/angular-mocks.js" },
{ "Path": "./lib/jasmine-jquery/jasmine-jquery.js" },
{ "Path": "./lib/underscore/underscore.js" },
{ "Path": "./lib/angular-bootstrap/ui-bootstrap-tpls.js" },
{ "Path": "./tests/dependencies.js" }
],
"Tests": [
{ "Include": "*/tests/specs/*.ts", "ExpandReferenceComments": "true" }
]
}
As you can see from above, I need to load some external libraries. I presume these get loaded once and then are used for each of the tests?
When I was using pure JavaScript, this configuration was blazing fast. It completed quite quickly in Visual Studio and through running the tests from the chutzpah.console.exe. However, since I have switched over to Chutzpah, the tests are much slower:
Tests complete: 263
=== 263 total, 0 failed, took 74.64 seconds ===
These results are even worse on our build machine, and it's timing out. It seems like there's a huge delay between each spec file that is run, and I think that is causing the overall slowness - once the spec file starts running, it completes in a few one-hundredths of a second. Does anyone have any ideas about what could possibly be configured incorrectly?
Here are a few things you can try to help get to the bottom of this issue:
Can you try enableding "EnableTestFileBatching": true in your chutzpah.json file to see if it helps? There can be slowness if you have MANY test files and this setting tells chutzpah to batching them into one harness.
Can you provide the output of the /trace option so I can see all the timings?
Can you provide a repro that I can run locally?

Visual Studio Code Task Argument

I'm trying to create some tasks in Visual Studio Code to run all the tests in my go project.
I usually execute the tests on the command line using:
go test ./...
In Visual Studio Code my tasks.json looks like this:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "test",
"isTestCommand": true,
"args": ["./..."]
}
]
}
So Build works fine (CTRL + SHIFT + B)
But when I try to run the tests (CTRL + SHIFT + T) the following error occurs:
go: unknown subcommand "./..."
It seems to be omitting the "test" param, but when I comment out the args it runs go test fine.
Any ideas?
THIS MAY BE A BUG
VSCode Reverse Args and Task as of v0.8.0
This may be a bug that still persists in the newer versions. As of v0.9.1 I have not had a chance to test. Prior to 0.9.1 at least one hack worked by reversing the task and it's arg as in the following example:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "./...",
"isTestCommand": true,
"args": ["test"]
}
]
}
It's hard to believe that this has still persisted until v0.8.0 so there may be a preferred solution that I have not discovered.
Here is a link to a prior post that deals with a similar issue:
Define multiple tasks in VSCode
Scroll down to my answer for more explanation.

Debug Electron using Visual Studio Code on Mac

Refer to this stackoverflow question:
I am trying to do the same but on Mac. I have the same as above,except instead of
"runtimeExecutable": "node_modules/electron-prebuilt/dist/electron.exe"
I have it as
"runtimeExecutable": "/usr/local/bin/electron"
Since F5 on mac is mapped to screen dimmer, I launched the app from command line as follows:
electron --debug-brk=5858 .
My program launched and ran without breaking.
So I modified keybindings.json like so:
[
{ "key": "shift+ctrl+f5", "command": "workbench.action.debug.play",
"when": "inDebugMode" },
{ "key": "shift+ctrl+f5", "command": "workbench.action.debug.start",
"when": "!inDebugMode" },
]
I tried launching the program by pressing shift+ctrl+f5 - I am still unable to debug my program.
I get the following error:
Error: Connection Failed
when I run node instead of electron, the debugger works fine when the the app is launched from command line
PLEASE HELP!
Thanks in advance
This is your launch.json. The important parts are runtimeExecutable and env. For VS Code 0.8.0, debugging only mostly works using electron 0.30.6.
{
"version": "0.1.0",
// List of configurations. Add new configurations or edit existing ones.
// ONLY "node" and "mono" are supported, change "type" to switch.
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch electron",
// Type of configuration. Possible values: "node", "mono".
"type": "node",
// Workspace relative or absolute path to the program.
"program": "main.js",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": ".",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": "node_modules/electron-prebuilt/dist/electron.app/Contents/MacOS/electron",
// Optional arguments passed to the runtime executable.
"runtimeArgs": [],
// Environment variables passed to the program.
"env": {"ATOM_SHELL_INTERNAL_RUN_AS_NODE": "0"},
// Use JavaScript source maps (if they exist).
"sourceMaps": false,
// If JavaScript source maps are enabled, the generated code is expected in this directory.
"outDir": null
},
{
"name": "Attach",
"type": "node",
// TCP/IP address. Default is "localhost".
"address": "localhost",
// Port to attach to.
"port": 5858,
"sourceMaps": false
}
]
}
Install 0.30.6 of electron-prebuilt in your project directory using npm install –-save-dev electron-prebuilt#0.30.6

Resources