How to get my node.js mocha test running?

221 views Asked by At

I have developed a service in node.js and looking to create my first ever mocha test for this in a seperate file test.js, so I can run the test like this:

mocha test

I could not figure out how to get the reference to my app, routes.js:

var _ = require('underscore');
module.exports = function (app) {
    app.post('/*', function (req, res) {
        var schema={
            type: Object,
            "schema":
            {
                "totalRecords": {type:Number}
            }
        };
        var isvalid = require('isvalid');
        var validJson=true;
        isvalid(req.body,schema
            , function(err, validObj) {
                if (!validObj) {
                    validJson = false;
                }
                handleRequest(validJson,res,err,req);
            });
    })
}

This is the server.js:

// set up ======================================================================
var express = require('express');
var app = express();                               // create our app w/ express
var port = process.env.PORT || 8080;                // set the port
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use (function (error, req, res, next){
    res.setHeader('content-type', 'application/json');
    res.status(400);
    res.json({
        "error": "errormsg"
    });
});

// routes ======================================================================
require('./routes.js')(app);

// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

And finally test.js:

"use strict";

var request = require('supertest');
var assert = require('assert');
var express = require('express');
var app = express();

describe('testing filter', function() {
    it('should return an error', function (done) {
        request(app)
            .post('/')
            .send({"hh":"ss"})
            .expect(400,{"error": "errormsg"})
            .end(function (err, res) {
                if (err) {
                    done(err);
                } else {
                    done();
                }
            });
    });
});
1

There are 1 answers

9
Bidhan On BEST ANSWER

Create a separate file called app.js. The only purpose of this file would be to run the server. You'll also need to export your app object from server.js. So, your server.js would look like this

    // set up ======================================================================
var express = require('express');
var app = express();                               // create our app w/ express
var port = process.env.PORT || 8080;                // set the port
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use (function (error, req, res, next){
    res.setHeader('content-type', 'application/json');
    res.status(400);
    res.json({
        "error": "errormsg"
    });
});

// routes ======================================================================
require('./routes.js')(app);

module.exports = app;

Create a new file called app.js and put this inside of it

var app = require('./app');
var port = process.env.port || 8000;

app.listen(port, function() {
    console.log("App listening on port " + port);
});

Now, inside of your test file, import your app as follows

var request = require('supertest');
var assert = require('assert');
var app = require('./app.js');
....

Note that I assume all your files are in the same directory. If you've put your test file in a different folder then you'll need to give the right path while requiring your app object.