ReferenceError: describe is not defined NodeJs
Solution 1:
Assuming you are testing via mocha
, you have to run your tests using the mocha
command instead of the node
executable.
So if you haven't already, make sure you do npm install mocha -g
. Then just run mocha
in your project's root directory.
Solution 2:
if you are using vscode, want to debug your files
I used tdd
before, it throw ReferenceError: describe is not defined
But, when I use bdd
, it works!
waste half day to solve it....
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"bdd",// set to bdd, not tdd
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
Solution 3:
To run tests with node/npm without installing Mocha globally, you can do this:
• Install Mocha locally to your project (npm install mocha --save-dev
)
• Optionally install an assertion library (npm install chai --save-dev
)
• In your package.json
, add a section for scripts
and target the mocha binary
"scripts": {
"test": "node ./node_modules/mocha/bin/mocha"
}
• Put your spec files in a directory named /test
in your root directory
• In your spec files, import the assertion library
var expect = require('chai').expect;
• You don't need to import mocha, run mocha.setup
, or call mocha.run()
• Then run the script from your project root:
npm test