简体   繁体   中英

Using Reporters with Mocha on Node.js

As my username implies, I'm brand new to Node. I really like a lot of the ideas. I'm just trying to get started with learning it. One of the things I'm looking to understand is how to setup a test driven environment with Node.

Currently, I have a basic javascript file called ToTest.min.js. This file defines a class called ToTest, which exposes a method called getItems. To test this method, I've written the following code in Test.js:

var should = require("should");
var toTest = require("../ToTest.min.js");

describe('ToTest.js', function () {
    describe('test1', function () {
        var t = new ToTest();
        var items = t.getItems();

        console.log("Result: " + items.length);
        t.should.have.length(1);
    });
});

I execute this file from the command line via:

mocha Test.js

When its executed, I see the following output:

Result: 0

C:\Projects\MyProject\node_modules\sh
ould\lib\should.js:130
    throw err;
          ^
AssertionError: expected [] to have a length of 1 but got 0
    at Object.Assertion.length (C:\Projects\MyProject\node_modules\should\lib\should.js:434:10)
    at Suite.<anonymous> (C:\Projects\MyProject\Test.js:9:27)
    at context.describe.context.context (C:\Users\Myself\AppData\Roaming\npm\n
ode_modules\mocha\lib\interfaces\bdd.js:72:10)
    at Suite.<anonymous> (C:\Projects\MyProject\Test.js:5:5)
    at context.describe.context.context (C:\Users\Myself\AppData\Roaming\npm\n
ode_modules\mocha\lib\interfaces\bdd.js:72:10)
    at Object.<anonymous> (C:\Projects\MyProject\Test.js:4:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:152
:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha
\lib\mocha.js:149:14)
    at Mocha.run (C:\Users\Myself\AppData\Roaming\npm\node_modules\mocha\lib\m
ocha.js:306:31)
    at Object.<anonymous> (C:\Users\Myself\AppData\Roaming\npm\node_modules\mo
cha\bin\_mocha:339:7)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

How do I get a reporter working so that the results of the unit tests are formatted better? I tried

mocha Test.js -R dot

and I received the same result.

Ah, you need to use the it function for actual tests, not describe .

var should = require("should");
var toTest = require("../ToTest.min.js");

describe('ToTest.js', function () {
    it('test1', function () {
        var t = new ToTest();
        var items = t.getItems();

        console.log("Result: " + items.length);
        t.should.have.length(1);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM