Node.js with mocha test

if I have a function as the following:

function foo(request,response){
  var val = request.param('data');
  //code here
});

how can I create a mocha test function for this to pass the request and response parameters.

The function you wrote above can be seen as a controller - it handles the request and gives back a response.

There are few things you can do:

  1. You can test the route itself - make a http request to the endpoint which uses this controller and test if it behaves correctly - you can use request/supertest/superagent libraries for example.

  2. You can mock the request and response objects and test the code directly - it doesn't require a server to be started, but you need to spend some time to mock out the objects correctly.

It depends on what your "code here" does, and what you want to do :

Test the logic

If you can separate the code in a method that accepts "val", and returns a result, then just test that. Usually, getting the params from a request, and passing the results to a response is a no-brainer, and not worth testing.

foo : function (req, res) {

  // Do you really need to test that ?
  var data = req.param("data");

  // You probably want to test that      
  var bar = doFooLogic(data);

  // Do you really need to test that ?
  res.json(bar);

},

doFooLogic : function (data) {
    ...
}

And a test like :

describe("foo's logic", function () {

  it("does stuff", function () {
     // Just test the logic.
     // This assumes you exposed the doFooLogic, which is probably acceptable
     var bar = doFooLogic(42);
     assert(bar.xxxx); // Whatever
  });

});

Test the req/response function:

If you really want that, if you're just using "param" on the request object, you might be able to easily mock the request / response (this is JS, you just need to pass something that has the same functions available) :

describe(..., function () {

  it("does whatever", function () {

    var mockRequest = {
      param : function (key) {
         if (key === "data") {
           return 42;
         } else {
           throw new Error("UNexpected key", key)
         }
      }
    }

    var mockResponse = {
      // mock whatever function you need here 
      json : function (whatever) {
           assert(whatever.xxxx) // compare what you put into the response, for example
      }
    }

    // Then do the call
    foo (mockRequest, mockResponse);

    // The hard part is then how to test the response was passed the right stuff.
    // That's why testing the logic is probably easier.

I think you can simply mock it with something like Sinon.js . It should be something like this:

describe('...', function( done ){
 it('should test something', function(done){
   var mock = sinon.stub(request, "param").withArgs("data").returns("Whatever");

   var val = request.param('data');

   //do your logic with that value

   assert.equal(/*whatever value you want check*/);

   mock.restore();

   done();
 }
}

And you don't have to take care about request's content.