node.js - ReferenceError: describe is not defined NodeJs

ID : 20347

viewed : 48

Tags : node.jsresttestingnode.js

Top 5 Answer for node.js - ReferenceError: describe is not defined NodeJs

vote vote

91

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.

vote vote

80

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" }, 
vote vote

80

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 
vote vote

67

You can also do like this:

  var mocha = require('mocha')   var describe = mocha.describe   var it = mocha.it   var assert = require('chai').assert    describe('#indexOf()', function() {     it('should return -1 when not present', function() {       assert.equal([1,2,3].indexOf(4), -1)     })   }) 

Reference: http://mochajs.org/#require

vote vote

56

i have this error when using "--ui tdd". remove this or using "--ui bdd" fix problem.

Top 3 video Explaining node.js - ReferenceError: describe is not defined NodeJs

Related QUESTION?