I am trying to define some endpoints and do a test using nodejs
. In server.js
I have:
var express = require(\'express\');
var func1
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"
},
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
i have this error when using "--ui tdd". remove this or using "--ui bdd" fix problem.
OP asked about running from node
not from mocha
. This is a very common use case, see Using Mocha Programatically
This is what injected describe and it into my tests.
mocha.ui('bdd').run(function (failures) {
process.on('exit', function () {
process.exit(failures);
});
});
I tried tdd
like in the docs, but that didn't work, bdd worked though.
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.
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