javascript nodejs mocha 套件未定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9795254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:52:19  来源:igfitidea点击:

nodejs mocha suite is not defined error

javascriptnode.jstddmocha

提问by thedev

I am trying to run some tests using mocha but cant seem to get over this error.

我正在尝试使用 mocha 运行一些测试,但似乎无法克服这个错误。

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 ?

从我从调用堆栈中可以看出问题就在这里cart.test.js:5:1。知道是什么原因造成的吗?

Thanks

谢谢

cart.js

购物车.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

购物车.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);     
    });
});

回答by Domenic

You need to tell Mocha to use the TDD interface, instead of the default BDD one:

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

mocha --ui tdd card.test.js

回答by Karthik M

You can do the same by just specifying mocha -u tddin package.json

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

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

回答by Raj

You can also include a Makefile in your project and specify TDD like so:

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

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

.PHONY: test

Hat tip: DailyJS

帽子提示:DailyJS