Question

I'm just learning testing and I've been at it for a few hours no with no success. Every time I run Mocha, my test shows up in the terminal and returns no result, like this:

Express test app started on port: 3001

  addition

It stays blank afterwards. Further, the node processes don't close after testing and when they stack up they crash my computer.

Here is my sample test:

var supertest = require('supertest');
var should = require('should');
process.env.NODE_ENV = 'test';
var app = require('../server');

describe('addition', function() {
    //... previous test
    it('should return 2 given the url /add/1/1', function(done) {
        request(app)
            .get('/add/1/1')
            .expect(200)
            .end(function(err, res) {
                should.not.exist(err);
                parseFloat(res.text).should.equal(2);
                done();
            });
    });
});

Here is my server.js

// Module Depndencies
var express = require('express'),
    fs = require('fs'),
    mongoose = require('mongoose'),
    passport = require('passport'),
    MongoStore = require('connect-mongo')(express),
    flash = require('connect-flash'),
    env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
    ErrorHandler = require('./app/controllers/api/v1/error_handler_v1'),
    config = require('./config/config'),
    auth = require('./config/middlewares/authorization');


// Connect The Database
mongoose.connect(config.db);
// Catch Database Connection Error
mongoose.connection.on("error", function(err) {
    return console.log("****** Could not connect to mongo server!");
});

// Express Settings
app = express();
app.set('showStackError', true);

//Should be placed before express.static
app.use(express.compress({
    filter: function(req, res) {
        return (/json|text|javascript|css/).test(res.getHeader('Content-Type'));
    },
    level: 9
}));

//Setting the fav icon and static folder - tejkh
app.use(express.favicon());
app.use(express.static(config.root + '/public'));

//Don't use logger for test env
if (process.env.NODE_ENV !== 'test') app.use(express.logger('dev'));

//Set views path, template engine and default layout
app.set('views', config.root + '/app/views');
app.set('view engine', 'jade');

// Enable jsonp
app.enable('jsonp callback');

//Bootstrap models
var models_path = __dirname + '/app/models';
var walk = function(path) {
    fs.readdirSync(path).forEach(function(file) {
        var newPath = path + '/' + file;
        var stat = fs.statSync(newPath);
        if (stat.isFile()) {
            if (/(.*)\.(js|coffee)/.test(file)) {
                require(newPath);
            }
        } else if (stat.isDirectory()) {
            walk(newPath);
        }
    });
};
walk(models_path);

// Bootstrap Passport
require('./config/passport')(passport);


// App.configure
app.configure(function() {

    // CookieParser should be above session
    app.use(express.cookieParser());

    // BodyParser should be above methodOverride
    app.use(express.bodyParser());
    app.use(express.methodOverride());

    // Express/mongo session storage
    app.use(express.session({
        secret: 'secret',
        cookie: {
            maxAge: 864000000 // 10 Days in miliseconds
        },
        store: new MongoStore({
            url: config.db,
            auto_reconnect: true
        })
    }));

    // Connect flash for flash messages
    app.use(flash());

    // Use Passport
    app.use(passport.initialize());
    app.use(passport.session());

    // Route
    app.use(app.router);

    // Error Handler
    app.use(function(err, req, res, next) {
        if (err.uri) delete err.uri;
        if (err.status) {
            return res.status(err.status).jsonp(err);
        } else {
            res.status(500).jsonp({
                name: 'InternalError',
                message: 'Sorry, something went wrong on our end.  We have been notified.',
                code: 'server_error',
                status: 500
            });
        }
    });

}); // app.configure

//Bootstrap routes
require('./config/routes')(app, passport, auth);

// Start App
app.listen(config.port);
console.log('Express ' + env + ' app started on port: ' + config.port);

// Expose app
exports = module.exports = app;
Was it helpful?

Solution

If you use:

var supertest = require('supertest');

then you should use:

supertest(app) 

and not:

request(app)

OTHER TIPS

try this, maybe it will work

request('http://host:port')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top