问题
I am trying to run some tests using mocha but cant seem to get over this error.
E:\tdd\nodejs\cart>mocha cart.test.js
node.js:201
throw e; // process.nextTick error, or 'err
^
ReferenceError: suite is not defined
at Object.<anonymous> (E:\tdd\nodejs\cart\cart.test.js:5:1
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at C:\Users\lex\AppData\Roaming\npm\node_module
at Array.forEach (native)
at load (C:\Users\lex\AppData\Roaming\npm\node_
9)
at Object.<anonymous> (C:\Users\lex\AppData\Roa
in\_mocha:237:1)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
From what I can tell from the call stack the problem is here cart.test.js:5:1
.
Any idea what is causing this ?
Thanks
cart.js
var GetTotalSum = function (input) {
var total = 0,
differentTitles = 0,
discountMap = [0, 1, 0.95, 0.9, 0.8, 0.75],
BOOK_PRICE = 8;
for (var i in input) {
total += input[i] * BOOK_PRICE;
if (input[i] > 0) {
differentTitles++;
}
}
if (differentTitles > 1) {
total = total * discountMap[differentTitles];
}
return total;
}
module.exports.GetTotalSum = GetTotalSum;
cart.test.js
var assert = require('assert'),
cart = require('./cart.js');
suite('cart', function () {
test('buy one book', function () {
// Arrange
var input = [1, 0, 0, 0, 0],
expected = 8;
// Act
var actual = cart.GetTotalSum(input);
// Assert
assert.equal(actual, expected);
});
});
回答1:
You need to tell Mocha to use the TDD interface, instead of the default BDD one:
mocha --ui tdd card.test.js
回答2:
You can do the same by just specifying mocha -u tdd in package.json
"scripts": {
"start" : "node server",
"test": "mocha -u tdd"
}
回答3:
You can also include a Makefile in your project and specify TDD like so:
test:
@./node_modules/.bin/mocha -u tdd
.PHONY: test
Hat tip: DailyJS
来源:https://stackoverflow.com/questions/9795254/nodejs-mocha-suite-is-not-defined-error