Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 538
Victor Ian Avatar asked Mar 20 '19 10:03

Victor Ian


3 Answers

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.

like image 89
Victor Ian Avatar answered Oct 31 '22 14:10

Victor Ian


Removing this import:

import { describe, it } from 'mocha';

Seems to fix it for me.

like image 23
Francis Upton IV Avatar answered Oct 31 '22 12:10

Francis Upton IV


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

like image 1
deerawan Avatar answered Oct 31 '22 12:10

deerawan