11

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.

Victor Ian
  • 1,034
  • 13
  • 26
  • Found the culprit - it was mocha. Downgraded to version 5.2.0 from 6+ and it was working again. However this is just a workaround and I need to find out what really was the issue. – Victor Ian Mar 27 '19 at 05:07
  • When migrating a JS project to TypeScript, I got this error. It went away when I renamed the test file from `.jsx` to `.tsx` – HelloWorld101 May 18 '23 at 11:56

4 Answers4

24

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.

Victor Ian
  • 1,034
  • 13
  • 26
3

Removing this import:

import { describe, it } from 'mocha';

Seems to fix it for me.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
2

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

deerawan
  • 8,002
  • 5
  • 42
  • 51
1

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"
}
Daniel Persson
  • 2,171
  • 1
  • 17
  • 24