如何在 Visual Studio Code 中运行 Jasmine 测试?

10
我已经安装了Jasmine和TypeScript来配置Visual Studio Code。
下面是我的测试文件:
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")
    })
})

请指导我如何将msg值作为日志打印并在Visual Studio Code中运行Jasmine测试?
我尝试使用specrunner.html运行,但结果只会显示通过或失败,无法在specrunner结果文件上打印任何日志。
1个回答

7

以下是我最终所做的。

  1. npm install --save-dev jasmine @types/jasmine(安装jasmine及其类型声明文件)
  2. 配置 tsconfig.json 文件,全局引入 jasmine 类型,并生成源映射文件,将所有输出发送到 dist 文件夹中。
{
  "compilerOptions": {
    /* ... */
    "sourceMap": true,
    "outDir": "./dist",
    "types": [
      "node",
      "jasmine"
    ],
    /* ... */
  }
}
  1. 创建了一个npm任务来使用node inspector运行jasmine inspect-brk需要node 8或以上的版本。你可以在7和一些版本的6上使用inspect,但我担心它可能无法及时捕获我的断点,并且没有深入研究那个选项。
{
  /* ... */
  "scripts": {
    "build": "tsc --project .",
    "test:debug": "npm run build && node --inspect-brk node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=jasmine.json"
  },
  /* ... */
}
  1. 在 VS Code 中创建一个启动任务 (launch.json),以启动 NPM 任务。
{
  /* ... */
  "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
    },
    /* ... */
  ]
  /* ... */
}

完成上述步骤后,您可以从 Visual Studio 中运行启动任务。它将运行您的代码并在适用的断点处停止。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接