sunpat
sunpat

Reputation: 399

How to run jasmine tests in visual studio code?

I have setup visual studio code with jasmine and typescript installed.

I have below spec file

TestSpec.ts

describe("Testing", () =>{
    it("should pass", () =>{
   let msg = "Welcome to TypeScript";
    //I want to print the msg first like a log
    expect(msg).toBe("Welcome to TypeScript")
    })
})

Please guide me how can I print the msg value as logs and run the jasmine test in visual studio code?

I have tried to run by using specrunner.html, but result just gives pass or fail, but could not able to print any logs on specrunner result file.

Upvotes: 11

Views: 15308

Answers (1)

Pace
Pace

Reputation: 43817

Here is what I ended up doing.

  1. npm install --save-dev jasmine @types/jasmine
  2. Configure tsconfig.json to include jasmine types globally and generate source maps and send all output to the dist folder.
{
  "compilerOptions": {
    /* ... */
    "sourceMap": true,
    "outDir": "./dist",
    "types": [
      "node",
      "jasmine"
    ],
    /* ... */
  }
}
  1. Created an npm task to run jasmine with node inspector inspect-brk requires node version 8 or above. You can use inspect with 7 and some versions of 6 but I was worried it might not capture my breakpoints in time and didn't investigate that option much.
{
  /* ... */
  "scripts": {
    "build": "tsc --project .",
    "test:debug": "npm run build && node --inspect-brk node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=jasmine.json"
  },
  /* ... */
}
  1. Created a launch task in VS code (launch.json) to launch the NPM task.
{
  /* ... */
  "configurations": [
    /* ... */
    {
      "type": "node",
      "request": "launch",
      "name": "Run Jasmine Tests",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "test:debug"
      ],
      "outFiles": [
        "${workspaceRoot}/dist/**.js"
      ],
      "protocol": "inspector",
      "port": 9229,
      "sourceMaps": true
    },
    /* ... */
  ]
  /* ... */
}

Once you've done all this you can run the launch task from visual studio. It will run your code and stop at applicable breakpoints.

Upvotes: 8

Related Questions