Steven
Steven

Reputation: 51

Unit test for node js

I am new to nodejs, and need to write unit test for a node project. I try to learn mocha and there are two questions:

  1. when I write unit test for function A, in A it also use function B, so how can I mock an output for B?

  2. how can I unit test these endpoints in app.js. like app.get, app.put. can someone give me some suggestions or simple examples?

Can someone also give me some advice on writing unit test for nodejs, thanks so much.

Thanks so much everyone.

Upvotes: 2

Views: 501

Answers (3)

zcmyworld
zcmyworld

Reputation: 413

Answering Q1,

If the funcitons in different modules, You can use a mock tool : fremock

Using freemock You can do this:

your code

//function a exports in the module named mA
function a(){
    return "1";
}
//function a exports in the module named mB
function b(){
    a();
}

test code

var freemock = require('freemock');
freemock.start()
var mock_b = freemock.getMock('mB');
mock_b.setMethod({
    "a":{
        willReturn:"1"
    }
})
freemock.end();

Some advice:

Mocha is good test framework for node.js .

For example,

  1. Assert tool: should.js
  2. Code coverage tool:istanbul
  3. ...

Mocha combines all this tools;

Here is a demo using Mocha:

your code(filename:mA.js)

//in the module named mA
function a(){
    return true;
}

test code(filename:testmA.js)

var should = require('should');
beforeEach(function(){
    //do something before testing
});
afterEach(function(){
    //do something after testing
});

describe("test",function(){
    describe("test1",function(){
        it("if true",function(){
            var mA = require('./mA');
            var result = mA.a();
            should.ok(result);
        });
        it("if false",function(){
            //other test
        });
    });
    describe("test2",function(){
        it("test2-1",function(){
            //other test
        })
    })
})

We should need run.js to start the test:

//run.js
var Mocha = require('mocha');
var mocha = new Mocha;
mocha.addFile(__dirname+'/test/testmA.js')
mocha.run();

The project dir tree is:

|- run.js
|
|- mA.js
|
|- test   -  testMA.js

Finally

Run this command:

istanbul cover run.js

Hope you enjoy!

Upvotes: 2

noamtcohen
noamtcohen

Reputation: 4612

I am recently involved in a node project where I have to run unit tests. Eventually I wrote a small script runner for karma using NW.JS This allowed me to access all node modules and run my tests on the server itself. I uploaded this project to github, Narma. Right now it was only tested on a Mac

Upvotes: 0

Piyas De
Piyas De

Reputation: 1764

Answering Q1,

If the output of b method is used in a metheod, then you can make the test of b method first.

Otherwise you can prepare result of b in before section of your test method and use it in a method.

It depends on your approach of testing.

Answering Q2 -

You can use superagent for sending get or post request ...

Some code examples ...

require('should'); var assert = require("assert"); var request = require('superagent'); var expect = require('expect.js'); then,

describe('yourapp', function(){ before(function(){ // function start start your server code // function end }) describe('server', function(){ describe('some-description', function(){ it('should return json in response', function(done){ request.post('http path') .send(JSON.parse("your json")) .end(function(res){ expect(res).to.exist; expect(res.status).to.equal(200); expect(res.text).to.contain('ok'); done(); }); }) }); }) after(function(){ //stop your server }) }); Here done is an important aspect in a unit testing component for asynchronous method testing.

Some reference -

superagent

this blog post

Hope this will help you,

Thanks

Upvotes: 3

Related Questions