Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs mocha suite is not defined error

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);     
    });
});
like image 574
thedev Avatar asked Mar 20 '12 21:03

thedev


3 Answers

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

mocha --ui tdd card.test.js
like image 109
Domenic Avatar answered Nov 08 '22 12:11

Domenic


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

"scripts": {
"start" : "node server",      
"test": "mocha -u tdd" 
 }
like image 35
Karthik M Avatar answered Nov 08 '22 14:11

Karthik M


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

like image 1
Raj Avatar answered Nov 08 '22 13:11

Raj