2

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.

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much. For Q1, if A is the only method that I export in the file, do I need to test B? or use the before section you told me?
It depends on your working approach. You can only test A, if that covers test for B also. Otherwise, you can test A and B method independently. before section should be used, when you need some initialization(like server starting etc.) before the method testing.
Hi thanks so much. For some helper function, for example function(cookie,remoteIP, req, res), do you know how to mock these input?
you should get help from the question - stackoverflow.com/questions/8021956/…
2

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!

1 Comment

Thanks so much for your advice. Your answer is very helpful
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.