brimble2010
brimble2010

Reputation: 18364

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

I'm trying to debug some Jasmine tests that I have written using WebStorm 2016.1.2.

My test code looks like this:

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

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

My directory structure looks like this:

│
├───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

And my NodeJS settings in WebStorm look like this:

WebStorm Javascript Library Settings

To debug I'm just hitting F5 and choosing the my-lib.spec.js file to run. I Then get the following stack trace:

"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

If anyone knows how to make WebStorm recognise that Jasmine is installed globally that'd be great.

EDIT: I've set up a Karma run configuration as suggested by lena with the following configuration: Karma configuration

When I hit F5 to run this, a Chrome browser pops up and is blank (I have the JetBrains plugin for Chrome installed)

Upvotes: 4

Views: 13026

Answers (5)

MDMoore313
MDMoore313

Reputation: 3311

To find the answer to this, I ran DiffMerge on my project root and a new Angular CLI project created in Webstorm that properly detected jasmine types.

What I found is that the tsconfig.json and tsconfig.spec.json files from my project defined typeRoots and types, whereas the other project did not.

enter image description here

The behavior of the typeRoots option is:

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

Types behaves a similar way.

Typescript documentation goes on to say:

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

So, for my project, this behavior is good enough for me. Deleting typeRoots and types from both my tsconfig.json and tsconfig.spec.json files solved this immediately.

Upvotes: 0

GH1995
GH1995

Reputation: 51

use mocha test.js, not node test.js

Upvotes: 1

s.n
s.n

Reputation: 703

In your main folder, there is a 'package.json' file. Open that and you will see something like this:

{
  "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"
  }, ....

Then add the following after "start": "node .", => "test": "jasmine". It should look like this:

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

Then run 'npm test' in the terminal

Upvotes: -2

lena
lena

Reputation: 93728

You are using Node.js run configuration to run your tests - and Node knows nothing about your test framework. You should be using a test runner (karma, for example - as you have karma installed). Try using karma run configuration. See https://confluence.jetbrains.com/display/WI/Running+JavaScript+tests+with+Karma.

BTW, if you like using Should with karma, try karma-should

Upvotes: 2

rxjmx
rxjmx

Reputation: 2183

Try using jasmine-node module.

It depends on the command send to the js file when you press F5. It needs to be jasmine-node <test files> not node <test files>.

Try doing that in the console/terminal and see if it works. It could be web storm sending the wrong command.

If you haven't got jasmine node installed you can do

npm install jasmine-node -g

Upvotes: 1

Related Questions