Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed end() to return a Promise when called without arguments. #163

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ Test.prototype.end = function(fn){
var server = this._server;
var end = Request.prototype.end;

if (!fn) {
return new Promise(function (resolve, reject) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably be using any-promise.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware that the .pipe() method of Superagent expects .end() to return the instance itself and not a promise. IE. this breaks .pipe(). I managed to "promisify" .end() like shown below.

var originalEnd = Test.prototype.end;

Test.prototype.end = function (callback) {
  var deferred = Promise.defer();

  originalEnd.call(this, function (err, res) {
    if (callback) {
      callback(err, res);
    }

    if (err) {
      deferred.reject(err);
    } else {
      deferred.resolve(res);
    }
  });

  this.then = deferred.promise.then.bind(deferred.promise);
  this.catch = deferred.promise.catch.bind(deferred.promise);

  return this;
};

self.end(function (err, res) {
if (err) { return reject(err); }
resolve(res);
});
});
}

end.call(this, function(err, res){
if (err) return fn(err);
if (server) return server.close(assert);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"methods": "1.1.0"
},
"devDependencies": {
"es6-promise": "1.0.0",
"express": "3.1.0",
"mocha": "1.19.0",
"should": "3.3.1"
Expand Down
16 changes: 16 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('es6-promise').polyfill();

var request = require('..')
, https = require('https')
Expand Down Expand Up @@ -228,6 +229,21 @@ describe('request(app)', function(){
});
});

describe('.end()', function() {
it('should return a promise of the callback', function() {
var app = express();
var test = request(app);

app.get('/', function(req, res){
res.send('supertest FTW!');
});

return test
.get('/')
.end();
});
});

describe('.expect(status[, fn])', function(){
it('should assert the response status', function(done){
var app = express();
Expand Down