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.
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.
Removing this import:
import { describe, it } from 'mocha';
Seems to fix it for me.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With