How to check my express application with mocha?

I just added tojs and mocha to my express test app, but I'm wondering how to test my app. I would like to do it as follows:

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 this test case simply tells med that the res.render function (called in show_create_user_screen) is undefined, possibly because the server is not running and the configuration was not completed. So I wonder how other people set up their tests?

+63
express mocha
Jan 12 '12 at 8:27
source share
5 answers

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 from pure javascript code (classes or functions) that are separated from explicit or any other structure using and use mocha vanilla tests to test this. Once you achieve this, if you want to really check the routes that you configure in mocha, you need to pass the mock req, res parameters to your middleware functions to mimic the interface between the express connection and your middleware.

For a simple case, you can 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 the FYI middleware functions should not return any particular value, this is what they do with the req, res, next parameters that you should focus on when testing.

Here are 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); }); }); }); 
+32
Jan 13 2018-12-12T00:
source share

found an alternative in connect.js test suites

They use supertest to test the connection application without binding the server to any port and without using layouts.

Here is an excerpt from the middleware test suite (using mocha as a test runner and supertest for claims)

 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 also works for express applications.

+63
Jul 09 '12 at 16:26
source share

You can try SuperTest and then start and close the server:

 var request = require('supertest') , app = require('./anExpressServer').app , assert = require("assert"); describe('POST /', function(){ it('should fail bad img_uri', function(done){ request(app) .post('/') .send({ 'img_uri' : 'foobar' }) .expect(500) .end(function(err, res){ done(); }) }) }); 
+20
Jul 19 '12 at 9:59
source share

mocha comes with before, beforeEach, after and afterEach for testing bdd. In this case, you should use this in your descriptive call.

 describe 'routes' -> before (done) -> app.listen(3000) app.on('connection', done) 
+6
Jan 12 2018-12-12T00:
source share

It was easier for me to configure the TestServer class for use as an assistant, as well as a helper-http-client and just make real requests to a real http-server. There may be times when you want to make fun of and put out this stuff instead.

 // Test file var http = require('the/below/code'); describe('my_controller', function() { var server; before(function() { var router = require('path/to/some/router'); server = http.server.create(router); server.start(); }); after(function() { server.stop(); }); describe("GET /foo", function() { it('returns something', function(done) { http.client.get('/foo', function(err, res) { // assertions done(); }); }); }); }); // Test helper file var express = require('express'); var http = require('http'); // These could be args passed into TestServer, or settings from somewhere. var TEST_HOST = 'localhost'; var TEST_PORT = 9876; function TestServer(args) { var self = this; var express = require('express'); self.router = args.router; self.server = express.createServer(); self.server.use(express.bodyParser()); self.server.use(self.router); } TestServer.prototype.start = function() { var self = this; if (self.server) { self.server.listen(TEST_PORT, TEST_HOST); } else { throw new Error('Server not found'); } }; TestServer.prototype.stop = function() { var self = this; self.server.close(); }; // you would likely want this in another file, and include similar // functions for post, put, delete, etc. function http_get(host, port, url, cb) { var options = { host: host, port: port, path: url, method: 'GET' }; var ret = false; var req = http.request(options, function(res) { var buffer = ''; res.on('data', function(data) { buffer += data; }); res.on('end',function(){ cb(null,buffer); }); }); req.end(); req.on('error', function(e) { if (!ret) { cb(e, null); } }); } var client = { get: function(url, cb) { http_get(TEST_HOST, TEST_PORT, url, cb); } }; var http = { server: { create: function(router) { return new TestServer({router: router}); } }, client: client }; module.exports = http; 
+5
Jan 17 2018-12-12T00:
source share



All Articles