Victor Ian
Victor Ian

Reputation: 1070

Describe is not defined exception on Typescript, Mocha and VSCode

For some reason my mocha test scripts are throwing an exception of "describe is not defined".

I have read and tried the solutions suggested by these SO questions but no luck:
describe is not a function
"Mocha describe is not defined duplicate"

other links are:
typescript mocha describe is not a function

This is my VSCode launch.json.

{
  "type": "node",
  "request": "launch",
  "name": "Mocha Tests",
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
  "args": [
    "-u",
    "tdd",
    "--timeout",
    "999999",
    "--colors",
    "${workspaceRoot}/dist/tests/**/*.js"
  ],
  "outFiles": ["${workspaceFolder}/dist/tests/**/*.js"],
  "sourceMaps": true,
  "protocol": "inspector",
  "internalConsoleOptions": "openOnSessionStart"
}

This is my mocha test script:

import "mocha";
import assert = require("assert");

describe("Init", () => {
  before(() => {
    console.log("before-hook");
  });

  it("connected", () => {
    assert(true, "is not true");
  });
});

And this is my tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "strict": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es6",
    "lib": [ "es6" ],
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "strictNullChecks": true,
    "allowJs": false,
    "checkJs": false,
    "types": [
      "node"
    ]
  },
  "compileOnSave": true
}

What am I doing wrong here? I really need to get back to using mocha.

Upvotes: 11

Views: 5982

Answers (4)

Francis Upton IV
Francis Upton IV

Reputation: 19443

Removing this import:

import { describe, it } from 'mocha';

Seems to fix it for me.

Upvotes: 3

Victor Ian
Victor Ian

Reputation: 1070

Answering my own question here.

I have figured out the issue after installing Mocha 6.1.1.

On the launch.json, change the args array from "tdd" to "bdd" so that:
"-u", "bdd"

Version 5.x worked with the "tdd" option so the next major version caused this hiccup of a badly written configuration.

Upvotes: 24

Daniel Persson
Daniel Persson

Reputation: 2233

Are your test files written in Javascript (you refer to *.js in your launch.json)?

I'm using ts-node to debug the unit tests, and refer directly to the Typescript test files, so my launch.json entry looks like below. Before I used ts-node, I got the 'describe is not defined' error when running from inside VS Code.

{
  "type": "node",
  "request": "launch",
  "name": "Unit tests (mocha)",
  "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
  "args": [
    "-r",
    "ts-node/register",
    "--timeout",
    "999999",
    "--colors",
    "${workspaceFolder}/test/**/*Test.ts",
  ],
  "console": "integratedTerminal",
  "internalConsoleOptions": "neverOpen",
  "protocol": "inspector"
}

Upvotes: 1

deerawan
deerawan

Reputation: 8443

Perhaps it may works by specifying mocha in types inside tsconfig.json

{
  "compilerOptions": {
    ...
    "types": [
      "node",
      "mocha" <--- specify here
    ]
  },
  "compileOnSave": true
}

Also don't forget to install @types/mocha

npm install @types/mocha --save-dev

Hope it can solve your issue

Upvotes: 2

Related Questions