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

Improve response body mismatch error message #65

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 31 additions & 5 deletions lib/test.js
Expand Up @@ -7,7 +7,6 @@ var request = require('superagent')
, util = require('util')
, http = require('http')
, https = require('https')
, assert = require('assert')
, Request = request.Request;

/**
Expand All @@ -16,6 +15,35 @@ var request = require('superagent')

var port = 3456;

/**
* An implementation of deepEqual.
* If should or Chai are available, use them for better output.
*/
var deepEqual;
[
['should', function(module, a, b) { a.should.eql(b); }],
['chai', function(module, a, b) { module.expect(a).to.eql(b); }],
['assert', function(module, a, b) {
try {
module.deepEqual(a, b);
} catch (err) {
var astr = util.inspect(a, {depth: null});
var bstr = util.inspect(b, {depth: null});
throw new Error('expected ' + bstr + ' response body, got ' + astr);
}
}]
].some(function(pair) {
var module, moduleDeepEqual = pair[1], moduleName = pair[0];
try {
module = require(moduleName);
deepEqual = moduleDeepEqual.bind(null, module);
return true;
} catch (err) {
// Module not available
return false;
}
});

/**
* Expose `Test`.
*/
Expand Down Expand Up @@ -156,11 +184,9 @@ Test.prototype.assert = function(res, fn){
// parsed
if ('object' == typeof body && !isregexp) {
try {
assert.deepEqual(body, res.body);
deepEqual(res.body, body);
} catch (err) {
var a = util.inspect(body);
var b = util.inspect(res.body);
return fn(new Error('expected ' + a + ' response body, got ' + b));
return fn(err);
}
} else {
// string
Expand Down