Search code examples
javascriptnode.jsmocha.jschaichai-http

Mocha chai-http response always returns null


I'm testing a server response in Node.js via mocha chai-http. The response I get is always null. However, When I run requests regularly without mocha test runner, Everything is fine.

This is the sever code:

var Babble = { messages: new Array() , users: new Array(),userCount:0 }; // 

var http = require('http');
var urlUtil = require('url');
var queryUtil = require('querystring');

var Babble = { messages: new Array() , users: new Array(),userCount:0 }; // Data object to save all chat logs
var server = http.createServer(function(request, response) {
    var strData,name,index;
    response.setHeader('Access-Control-Allow-Origin', '*');
    if (request.method === 'GET') {
        var url = urlUtil.parse(request.url);
        var data = queryUtil.parse(url.query);
        console.log(data.message);
        if (!data.message) {
            response.writeHead(400);
        }
        response.end();
    } else if (request.method === 'POST') {
        var requestBody = '';
        request.on('data', function(chunk) {
                requestBody += chunk.toString();
        });
        request.on('end', function() {
            var data = queryUtil.parse(requestBody);
            strData = JSON.stringify(data);
            // Handle different requests

                ...

            response.end(JSON.stringify(Babble));

            }   
        });
});

    module.exports = {server,Babble};

And this is the mocha chai-http test code:

var module = require('./testedServer');
var assert = require('assert');

var chai = require('chai')
  , chaiHttp = require('chai-http');

chai.use(chaiHttp);

 describe('Check receiving data from server:', function() {

                beforeEach(function() {

                        module.server.listen(9099);

                });

                it('Should receive a non-null object containing the chat logs', function(done) {

                        chai.request('http://localhost:9099')
                                .post('/')
                                .send(
                                'poll0,0/Annonymous.'
                        ).end(function(res){
                                        console.log(res);
                                        assert.equal(res != null,true,"Error: test has failed - server response is null");
                                        done();
                        });

                });

                afterEach(function() {

                        // runs after each test in this block

                        module.server.close();

                });

        });

I think I pronounced the request wrong.


Solution

  • chai.end uses the standard err first callback style. See this example in the chai-http docs.

    In your test code, including the err first should fix your problem.

    ).end(function(err, res){ // err first callback
    

    In terms of writing a decent minimal test, you'll probably want at least one assertion (or it) for:

    • err: e.g. assert.equal
    • res: Some attribute of the res object, e.g. res.statusCode

    Going forward, there's loads written on error first callbacks. Start here for a simple explanation.