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

Support embedded regular expressions. #441

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var request = require('superagent');
var util = require('util');
var http = require('http');
var https = require('https');
var assert = require('assert');
var deepMatch = require('deep-equal-with-regex');
var Request = request.Request;

/**
Expand Down Expand Up @@ -197,9 +197,7 @@ Test.prototype._assertBody = function(body, res) {

// parsed
if (typeof body === 'object' && !isregexp) {
try {
assert.deepStrictEqual(body, res.body);
} catch (err) {
if (!deepMatch(body, res.body, { regex: true })) {
a = util.inspect(body);
b = util.inspect(res.body);
return error('expected ' + a + ' response body, got ' + b, body, res.body);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
},
"dependencies": {
"methods": "^1.1.2",
"superagent": "^3.8.3"
"superagent": "^3.8.3",
"deep-equal-with-regex": "^1.3.0"
},
"devDependencies": {
"body-parser": "^1.18.3",
Expand Down
22 changes: 22 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,28 @@ describe('request(app)', function () {
});
});

it('should support embedded regular expressions', function (done) {
const app = express();

app.set('json spaces', 0);

app.get('/', function(req, res) {
res.send({ foo: 'bar' });
});

request(app)
.get('/')
.expect({ foo: /baz/ })
.end(function(err, res) {
err.message.should.equal('expected { foo: /baz/ } response body, got { foo: \'bar\' }');

request(app)
.get('/')
.expect({ foo: /bar/ })
.end(done);
});
});

it('should assert response body multiple times', function (done) {
const app = express();

Expand Down