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

Promised supertest #380

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Expand Up @@ -4,6 +4,9 @@
"node": true,
"mocha": true
},
"globals": {
"Promise": true
},
"rules": {
// disabled - disagree with airbnb
"func-names": [0],
Expand Down
4 changes: 4 additions & 0 deletions Readme.md
Expand Up @@ -211,6 +211,10 @@ describe('request.agent(app)', function(){

Perform the request and invoke `fn(err, res)`.

### .then(onFulfilled, onRejected)

Perform the request and return a promise.

## Notes

Inspired by [api-easy](https://github.com/flatiron/api-easy) minus vows coupling.
Expand Down
22 changes: 22 additions & 0 deletions lib/test.js
Expand Up @@ -135,6 +135,28 @@ Test.prototype.end = function(fn) {
return this;
};

/**
* Perform assertions as a Promise.
*
* @param {Function} onFulfilled
* @param {Function} onRejected
* @api public
*/

Test.prototype.then = function(onFulfilled, onRejected) {
var self = this;

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

resolve(res);
});
}).then(onFulfilled, onRejected);
};

/**
* Perform assertions and invoke `fn(err, res)`.
*
Expand Down
40 changes: 40 additions & 0 deletions test/supertest.js
Expand Up @@ -342,6 +342,46 @@ describe('request(app)', function() {
});
});

describe('.then(onFulfilled, onRejected)', function() {
it('should close the server and return a Promise on a fullfilled result', function(done) {
var app = express();
var test;

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

test = request(app)
.get('/')
.expect(200);

test.then(function() {}).should.be.an.instanceOf(Promise);

test._server.on('close', function() {
done();
});
});

it('should close the server and return a Promise on a rejected result', function(done) {
var app = express();
var test;

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

test = request(app)
.get('/')
.expect(404);

test.then(function () {}, function() {}).should.be.an.instanceOf(Promise);

test._server.on('close', function() {
done();
});
});
});

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