0

If you make a request to sails via supertest, the response hangs if you return an error. Here, we have already lifted sails, and will run this as an integration test against a live db.

var sails = require('sails').lift();
var request = require('supertest');
var app = sails.hooks.http.app;
describe('The creation of a model',function(){
    it('should not create a duplicate',function(done){

        var user = request.agent(app);
        user
            .post('/api/create')
            .end(function(err,res){

                //never gets here, your test will hang
                done();

            });

    });
});


//controller.js

module.exports = {
    // /api/create routes here
    create:function(req,res){

        var params = {
            name:"invalid"
        };
        SomeAction(params,function(err,results){

            if (err) {
                //the problem is here.
                return err;
            }
            res.json(results);

        });

    }

};
aclave1
  • 1,680
  • 18
  • 29
  • 1
    Consider splitting this into a question and answer. Leave the question above, and put the answer below. http://stackoverflow.com/help/self-answer – aliteralmind Jul 12 '14 at 01:28

1 Answers1

0

If you make a supertest request to sails and the function just returns a value, ie. you don't use res.send() or res.json() it will hang the request for supertest. Here's the right way to do it:

var sails = require('sails').lift();
var request = require('supertest');
var app = sails.hooks.http.app;
describe('The creation of a model',function(){
    it('should not create a duplicate',function(done){

        var user = request.agent(app);
        user
            .post('/api/create')
            .end(function(err,res){

                //never gets here, your test will hang
                done();

            });

    });
});


//controller.js

module.exports = {
    // /api/create routes here
    create:function(req,res){

        var params = {
            name:"invalid"
        };
        SomeAction(params,function(err,results){

            if (err) {
                //you need to send a response to the 
                res.json({
                  error:"an error occured"
                });
            }
            res.json(results);

        });

    }

};
aclave1
  • 1,680
  • 18
  • 29