Changed end() to return a Promise when called without arguments.#163
Changed end() to return a Promise when called without arguments.#163jussi-kalliokoski wants to merge 1 commit intoforwardemail:masterfrom
Conversation
|
Let's keep this around until we can get superagent updated to keep them somewhat similar. It needs lots of work though, so might be a while :-/ |
|
@gjohnson What's the status of this? This would be a great addition, specifically for use with mocha |
|
👍 |
|
I desperately needed this and hacked around it with this: var Bluebird = require('bluebird'); // Promise lib
var Test = require('supertest/lib/test');
Test.prototype.end = Bluebird.promisify(Test.prototype.end); |
|
Exists a library for this for anyone interested. |
|
Hmm. I've also been using supertest-as-promised, which doesn't even require end() to return a promise, but I don't really know how hacky it is. That said, supertest-as-promised seems to function great! |
|
It is not very hacky — you just need to add |
|
This is an example for var q = require('q');
Test.prototype.then = function(onFulfilled, onRejected) {
var self = this;
return q.Promise(function(resolve, reject) {
self.end(function(err, res) {
if (err) {
return reject(err);
}
resolve(res);
});
}).then(onFulfilled, onRejected);
};With that fix you could use var supertest = require('supertest');
it('should be ok', function() {
return supertest(app).get('/').expect(200);
}); |
|
I can confirm supertest-as-promised is a tiny wrap around supertest. |
|
👍 for this, would be great to see it in mainline project even if "promisified" solutions exist. |
|
+1 |
|
bump |
|
+1 |
| var end = Request.prototype.end; | ||
|
|
||
| if (!fn) { | ||
| return new Promise(function (resolve, reject) { |
There was a problem hiding this comment.
You should probably be using any-promise.
There was a problem hiding this comment.
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;
};|
+1 |
|
I've opened a new PR for this functionality which uses native promises by default and the .then() method as suggested by @arikon |
|
@jussi-kalliokoski can you resolve these conflicts ? Could you please take in consideration the @PlasmaPower and @robinvdvleuten comments. |
|
@rimiti I think this PR can be closed at least in favor of #380 |
This is a pretty handy thing to have, especially with Mocha, allowing you to go from this (example from supertest's own tests):
to this:
Or, in a simpler case, the boilerplate reduction is even more obvious:
vs
Do note that this depends on ES6 Promises so it works only on node 0.11.x and later, but the
es6-promisepolyfill can be used for older versions.