nodejs摩卡套件没有定义错误

我尝试使用摩卡运行一些testing,但似乎无法克服这个错误。

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) 

从我可以告诉堆栈的问题是在这里cart.test.js:5:1 。 任何想法是什么造成这个?

谢谢

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); }); }); 

       

网上收集的解决方案 "nodejs摩卡套件没有定义错误"

您需要告诉Mocha使用TDD接口,而不是默认的BDD接口:

 mocha --ui tdd card.test.js 

你也可以在你的项目中包含一个Makefile,并像这样指定TDD:

 test: @./node_modules/.bin/mocha -u tdd .PHONY: test 

帽子提示: DailyJS

你可以通过在package.json中指定mocha -u tdd来做同样的事情

 "scripts": { "start" : "node server", "test": "mocha -u tdd" }