Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i test my express app with mocha?

I've just added shouldjs and mocha to my express app for testing, but I'm wondering how to test my application. I would like to do it like this:

app = require '../app' routes = require '../src/routes'  describe 'routes', ->   describe '#show_create_user_screen', ->     it 'should be a function', ->       routes.show_create_user_screen.should.be.a.function     it 'should return something cool', ->       routes.show_create_user_screen().should.be.an.object 

Of course, the last test in that test-suite just tells med that the res.render function (called within show_create_user_screen) is undefined, probably becouse the server is not running and the config has not been done. So I wonder how other people set up their tests?

like image 340
Robin Heggelund Hansen Avatar asked Jan 12 '12 08:01

Robin Heggelund Hansen


People also ask

Can Mocha be used for API testing?

In the past, we've worked on Node API Authentication with JSON Web Tokens and Passport. In this tutorial, we are going to write a simple RESTful API with Node. js and use Mocha and Chai to write tests against it. We will test CRUD operations on a bookstore.


2 Answers

found an alternative in connect.js tests suites

They are using supertest to test a connect app without binding the server to any port and without using mock-ups.

Here is an excerpt from connect's static middleware test suite (using mocha as the test runner and supertest for assertions)

var connect = require('connect');  var app = connect(); app.use(connect.static(staticDirPath));  describe('connect.static()', function(){   it('should serve static files', function(done){     app.request()     .get('/todo.txt')     .expect('contents', done);   }) }); 

This works for express apps as well

like image 182
alexandru.topliceanu Avatar answered Sep 28 '22 00:09

alexandru.topliceanu


OK, first although testing your routing code is something you may or may not want to do, in general, try to separate your interesting business logic in pure javascript code (classes or functions) that are decoupled from express or whatever framework you are using and use vanilla mocha tests to test that. Once you've achieved that if you want to really test the routes you configure in mocha, you need to pass mock req, res parameters into your middleware functions to mimic the interface between express/connect and your middleware.

For a simple case, you could create a mock res object with a render function that looks something like this.

describe 'routes', ->   describe '#show_create_user_screen', ->     it 'should be a function', ->       routes.show_create_user_screen.should.be.a.function     it 'should return something cool', ->       mockReq = null       mockRes =         render: (viewName) ->           viewName.should.exist           viewName.should.match /createuser/        routes.show_create_user_screen(mockReq, mockRes).should.be.an.object 

Also just FYI middleware functions don't need to return any particular value, it's what they do with the req, res, next parameters that you should focus on in testing.

Here is some JavaScript as you requested in the comments.

describe('routes', function() {     describe('#show_create_user_screen', function() {       it('should be a function', function() {         routes.show_create_user_screen.should.be.a["function"];       });       it('should return something cool', function() {         var mockReq = null;         var mockRes = {           render: function(viewName) {             viewName.should.exist;             viewName.should.match(/createuser/);           }         };         routes.show_create_user_screen(mockReq, mockRes);       });     });   }); 
like image 30
Peter Lyons Avatar answered Sep 28 '22 00:09

Peter Lyons