Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ladjs/supertest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.1.3
Choose a base ref
...
head repository: ladjs/supertest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.1.4
Choose a head ref
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Apr 12, 2021

  1. Allows expect to accept an array of statuses

    By accepting an array of expected values for status, expect can now assert a range of returned statuses codes. This is useful when we don't know the exact code but we expect a range: 200/204, redirects, etc. See: #389
    juanvillegas committed Apr 12, 2021
    Copy the full SHA
    ab252f3 View commit details

Commits on Jul 21, 2021

  1. Merge pull request #715 from juanvillegas/feature/allows-expect-to-ta…

    …ke-an-array
    
    Allows expect to accept an array of statuses
    niftylettuce authored Jul 21, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    7e4645c View commit details
  2. 6.1.4

    niftylettuce committed Jul 21, 2021

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    cfeae1a View commit details
Showing with 60 additions and 2 deletions.
  1. +26 −0 lib/test.js
  2. +1 −1 package-lock.json
  3. +1 −1 package.json
  4. +32 −0 test/supertest.js
26 changes: 26 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -101,6 +101,7 @@ function wrapAssertFn(assertFn) {
* .expect('Content-Type', 'application/json')
* .expect('Content-Type', 'application/json', fn)
* .expect(fn)
* .expect([200, 404])
*
* @return {Test}
* @api public
@@ -125,6 +126,12 @@ Test.prototype.expect = function(a, b, c) {
return this;
}

// multiple statuses
if (Array.isArray(a)) {
this._asserts.push(wrapAssertFn(this._assertStatusArray.bind(this, a)));
return this;
}

// header field
if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) {
this._asserts.push(wrapAssertFn(this._assertHeader.bind(this, { name: '' + a, value: b })));
@@ -297,6 +304,25 @@ Test.prototype._assertStatus = function(status, res) {
}
};

/**
* Perform assertions on the response status and return an Error upon failure.
*
* @param {Array<Number>} statusArray
* @param {Response} res
* @return {?Error}
* @api private
*/

Test.prototype._assertStatusArray = function(statusArray, res) {
var b;
var expectedList;
if (!statusArray.includes(res.status)) {
b = http.STATUS_CODES[res.status];
expectedList = statusArray.join(', ');
return new Error('expected one of "' + expectedList + '", got ' + res.status + ' "' + b + '"');
}
};

/**
* Performs an assertion by calling a function and return an Error upon failure.
*
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "supertest",
"version": "6.1.3",
"version": "6.1.4",
"description": "SuperAgent driven library for testing HTTP servers",
"main": "index.js",
"author": "TJ Holowaychuk",
32 changes: 32 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
@@ -399,6 +399,38 @@ describe('request(app)', function () {
});
});

describe('.expect(statusArray)', function () {
it('should assert only status', function (done) {
const app = express();

app.get('/', function (req, res) {
res.send('hey');
});

request(app)
.get('/')
.expect([200, 404])
.end(done);
});

it('should reject if status is not in valid statuses array', function (done) {
const app = express();

app.get('/', function (req, res) {
res.send('hey');
});

request(app)
.get('/')
.expect([500, 404])
.end(function (err, res) {
err.message.should.equal('expected one of "500, 404", got 200 "OK"');
shouldIncludeStackWithThisFile(err);
done();
});
});
});

describe('.expect(status, body[, fn])', function () {
it('should assert the response body and status', function (done) {
const app = express();