From 9552dd20eea3d04bba0bf95867268325e767570a Mon Sep 17 00:00:00 2001 From: Jonathan Knapp Date: Fri, 20 Oct 2017 22:51:22 -0700 Subject: [PATCH] Support embedded regular expressions. --- lib/test.js | 6 ++---- package.json | 5 +++-- test/supertest.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/test.js b/lib/test.js index da8dd3cf..af6f9c0d 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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; /** @@ -195,9 +195,7 @@ Test.prototype._assertBody = function(body, res) { // parsed if (typeof body === 'object' && !isregexp) { - try { - assert.deepEqual(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); diff --git a/package.json b/package.json index 20984045..49cc4413 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,9 @@ "test": "eslint lib/**/*.js test/**/*.js && mocha --require should --reporter spec --check-leaks" }, "dependencies": { - "superagent": "^3.0.0", - "methods": "~1.1.2" + "deep-equal-with-regex": "^1.3.0", + "methods": "~1.1.2", + "superagent": "^3.0.0" }, "devDependencies": { "body-parser": "~1.16.0", diff --git a/test/supertest.js b/test/supertest.js index c9a4927b..a3ea4956 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -511,6 +511,28 @@ describe('request(app)', function() { }); }); + it('should support embedded regular expressions', function(done) { + var 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) { var app = express();