NodeJS, WebStorm and Jasmine: ReferenceError: describe is not defined when debugging

NodeJS, WebStorm and Jasmine: ReferenceError: describe is not defined when debugging

我正在尝试调试我使用 WebStorm 2016.1.2 编写的一些 Jasmine 测试。

我的测试代码是这样的:

var should = require("should");
var myLib = require("../my-lib");

describe("Scenario", () => {
    it("works as expected", () => {
        myLib.do().should.not.throw()
    });
});

我的目录结构是这样的:

│
├───node_modules
│   ├───.bin
│   ├───aws-sdk
│   │   └───<snip>
│   ├───jasmine
│   │   └───<snip>
│   ├───jasmine-core
│   │   └───<snip>
│   ├───karma
│   │   └───<snip>
│   ├───karma-jasmine
│   │   └───<snip>
│   ├───should
│   │   └───<snip>
│   └───sinon
│       └───<snip>
├───spec
│   ├───support
│   │   └───jasmine.json
│   └───my-lib.spec.js
└───my-lib.js

我在 WebStorm 中的 NodeJS 设置如下所示:

要进行调试,我只需按 F5 并选择 my-lib.spec.js 文件到 运行。然后我得到以下堆栈跟踪:

"C:\Program Files (x86)\JetBrains\WebStorm 2016.1.2\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=22714 my-lib.spec.js
Debugger listening on port 22714
c:\Users\<me>\WebstormProjects\my-lib\spec\my-lib.spec.js:4
describe("Scenario", () => {
^

ReferenceError: describe is not defined
    at Object.<anonymous> (c:\Users\<me>\WebstormProjects\my-lib\spec\<my-lib>.js:4:1)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Module.runMain [as _onTimeout] (module.js:442:10)
    at Timer.listOnTimeout (timers.js:92:15)

Process finished with exit code 1

如果有人知道如何让 WebStorm 识别 Jasmine 是全局安装的,那就太好了。

编辑: 我按照 lena 的建议设置了 Karma 运行 配置,配置如下:

当我按 F5 到 运行 时,Chrome 浏览器弹出并且是空白的(我安装了 Chrome 的 JetBrains 插件)

尝试使用 jasmine-node 模块。

这取决于您按F5时发送到js文件的命令。它需要 jasmine-node <test files> 而不是 node <test files>

尝试在 console/terminal 中这样做,看看是否有效。可能是网络风暴发送了错误的命令。

如果你还没有安装 jasmine 节点,你可以

npm install jasmine-node -g

您正在使用 Node.js 运行 配置来 运行 您的测试 - 而 Node 对您的测试框架一无所知。您应该使用测试 运行ner(例如 karma - 因为您安装了 karma)。尝试使用 karma 运行 配置。参见 https://confluence.jetbrains.com/display/WI/Running+JavaScript+tests+with+Karma

顺便说一句,如果你喜欢将 Should 与 karma 一起使用,请尝试 karma-should

在您的主文件夹中,有一个 'package.json' 文件。 打开它,你会看到这样的东西:

{
  "name": "prep",
  "version": "1.0.0",
  "main": "server/server.js", //This can be different
  "scripts": {
    "lint": "eslint .",
    "start": "node .",
    "posttest": "npm run lint && nsp check"
  }, ....

然后在"start"后面添加:"node .",=>"test":"jasmine"。 它应该是这样的:

{
  "name": "prep",
  "version": "1.0.0",
  "main": "server/server.js",
  "scripts": {
    "lint": "eslint .",
    "start": "node .",
    "test": "jasmine",
    "posttest": "npm run lint && nsp check"
  },....

然后在终端运行'npm test'

使用mocha test.js,而不是node test.js

为了找到这个问题的答案,我在我的项目根目录上 运行 DiffMerge 和在 Webstorm 中创建的一个新的 Angular CLI 项目,它正确地检测到了 jasmine 类型。

我发现 tsconfig.jsontsconfig.spec.json 文件 来自我的项目 定义了 typeRootstypes,而另一个项目没有。

typeRoots 选项的行为是:

If typeRoots is specified, only packages under typeRoots will be included.

Types 行为类似。

Typescript 文档接着说:

By default all visible ”@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible.

所以,对于我的项目,这种行为对我来说已经足够好了。从我的 tsconfig.jsontsconfig.spec.json 文件中删除 typeRootstypes 立即.

解决了这个问题