Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug jasmine tests written in typescript node in vs code

I have my unit tests written in jasmine and those are in typescript

// about.service.spec.ts
// say 4 to 5 test cases

// spec/support/jasmine.json
{
  "spec_dir": "src/tests/",
  "spec_files": ["**/*.spec.ts"],
  "helpers": ["jasmine-helpers/**/*.ts"],
  ...
}

// launch.json - vscode file
{
  "version": "0.2.0",
  "configurations": [{
      "type": "node",
      "request": "launch",
      "name": "Jasmine tests",
      "preLaunchTask": "debuggertests",
   }]
}

// tasks.json - vscode 
{
 "version": "2.0.0",
 "tasks": [{
    "label": "debuggertests",
    "type": "npm",
    "script": "test:unit",
    "problemMatcher": []
  }]
}

// package.json
// have to use jasmine-ts which is flavor over ts-node
"test:unit": "jasmine-ts JASMINE_CONFIG_PATH=spec/support/jasmine.json"

I have used this configuration to debug .spec.ts files in vscode but it did not fire debugger instead it run all tests and debugging started.

I have put a breakpoint in one of the test case of about.service.spec.ts but no breakpoint fired. Could anyone help me on setting up vscode debugging for jasmine tests?

like image 521
user3205479 Avatar asked May 06 '18 20:05

user3205479


People also ask

How do you debug a test case in VS code?

Once you have your launch configuration set, start your debug session with F5. Alternatively, you can run your configuration through the Command Palette (Ctrl+Shift+P) by filtering on Debug: Select and Start Debugging or typing 'debug ' and selecting the configuration you want to debug.

How do I test a TypeScript code in Visual Studio?

ts open in the editor, press F5. If you have other debugger extensions installed, you need to select Node. js from the dropdown. The debugger will start a session, run your code, and display the "Hello World" message in the Debug console panel.


2 Answers

In new jasmine-ts version, you have to include the jasmine.json to the args as this:

{
  "type": "node",
  "request": "launch",
  "name": "Jasmine Current File",
  "program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
  "args": ["--config=jasmine.json", "${file}"],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen"
}

To avoid this issue:

No specs found Finished in 0.003 seconds Incomplete: No specs found Randomized with seed 60766 (jasmine --random=true --seed=60766)

like image 136
isaacfi Avatar answered Nov 15 '22 04:11

isaacfi


Below configuration will debug current test file - please open the required test file in VS Code and start debugging with this configuration:

{
      "type": "node",
      "request": "launch",
      "name": "Jasmine Current File",
      "program": "${workspaceFolder}/node_modules/jasmine-ts/lib/index",
      "args": ["${file}"],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
 }
like image 38
Chirag Rupani Avatar answered Nov 15 '22 03:11

Chirag Rupani