Skip to content

Commit 7e4645c

Browse files
authoredJul 21, 2021
Merge pull request #715 from juanvillegas/feature/allows-expect-to-take-an-array
Allows expect to accept an array of statuses
2 parents 1bb8c66 + ab252f3 commit 7e4645c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎lib/test.js

+26
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function wrapAssertFn(assertFn) {
101101
* .expect('Content-Type', 'application/json')
102102
* .expect('Content-Type', 'application/json', fn)
103103
* .expect(fn)
104+
* .expect([200, 404])
104105
*
105106
* @return {Test}
106107
* @api public
@@ -125,6 +126,12 @@ Test.prototype.expect = function(a, b, c) {
125126
return this;
126127
}
127128

129+
// multiple statuses
130+
if (Array.isArray(a)) {
131+
this._asserts.push(wrapAssertFn(this._assertStatusArray.bind(this, a)));
132+
return this;
133+
}
134+
128135
// header field
129136
if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) {
130137
this._asserts.push(wrapAssertFn(this._assertHeader.bind(this, { name: '' + a, value: b })));
@@ -297,6 +304,25 @@ Test.prototype._assertStatus = function(status, res) {
297304
}
298305
};
299306

307+
/**
308+
* Perform assertions on the response status and return an Error upon failure.
309+
*
310+
* @param {Array<Number>} statusArray
311+
* @param {Response} res
312+
* @return {?Error}
313+
* @api private
314+
*/
315+
316+
Test.prototype._assertStatusArray = function(statusArray, res) {
317+
var b;
318+
var expectedList;
319+
if (!statusArray.includes(res.status)) {
320+
b = http.STATUS_CODES[res.status];
321+
expectedList = statusArray.join(', ');
322+
return new Error('expected one of "' + expectedList + '", got ' + res.status + ' "' + b + '"');
323+
}
324+
};
325+
300326
/**
301327
* Performs an assertion by calling a function and return an Error upon failure.
302328
*

‎test/supertest.js

+32
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ describe('request(app)', function () {
399399
});
400400
});
401401

402+
describe('.expect(statusArray)', function () {
403+
it('should assert only status', function (done) {
404+
const app = express();
405+
406+
app.get('/', function (req, res) {
407+
res.send('hey');
408+
});
409+
410+
request(app)
411+
.get('/')
412+
.expect([200, 404])
413+
.end(done);
414+
});
415+
416+
it('should reject if status is not in valid statuses array', function (done) {
417+
const app = express();
418+
419+
app.get('/', function (req, res) {
420+
res.send('hey');
421+
});
422+
423+
request(app)
424+
.get('/')
425+
.expect([500, 404])
426+
.end(function (err, res) {
427+
err.message.should.equal('expected one of "500, 404", got 200 "OK"');
428+
shouldIncludeStackWithThisFile(err);
429+
done();
430+
});
431+
});
432+
});
433+
402434
describe('.expect(status, body[, fn])', function () {
403435
it('should assert the response body and status', function (done) {
404436
const app = express();

0 commit comments

Comments
 (0)
Please sign in to comment.