Skip to content

Commit

Permalink
Reduce actual for match and matchJson
Browse files Browse the repository at this point in the history
When asserting plain objects using match or matchJson, set the "actual"
object to an excerpt only containing the properties specified by the
matcher. If the original "actual" object is used, the diff produced by
test runners shows all properties that are not defined in the matcher as
missing. With this change, only properties that are actually compared
are shown.
  • Loading branch information
mantoni committed Aug 20, 2018
1 parent df5968e commit 9648ae3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
5 changes: 4 additions & 1 deletion demo/diff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ describe("diff", function() {
});

it("matchJson", function() {
assert.matchJson('{"foo":42,"bar":true}', { foo: 42, bar: false });
assert.matchJson('{"foo":42,"bar":true,"ignored":true}', {
foo: 42,
bar: false
});
});
});
19 changes: 19 additions & 0 deletions lib/actual-for-match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict";

var toString = Object.prototype.toString;

function actualForMatch(actual, match) {
if (
toString.call(actual) === "[object Object]" &&
toString.call(match) === "[object Object]"
) {
var copy = {};
Object.keys(match).forEach(function(key) {
copy[key] = actual[key];
});
return copy;
}
return actual;
}

module.exports = actualForMatch;
5 changes: 5 additions & 0 deletions lib/assertions/match-json.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var actualForMatch = require("../actual-for-match");

module.exports = function(referee) {
referee.add("matchJson", {
assert: function(actual, matcher) {
Expand Down Expand Up @@ -28,6 +30,9 @@ module.exports = function(referee) {
} catch (e) {
// Do nothing
}
if (parsed) {
parsed = actualForMatch(parsed, matcher);
}
return {
actualRaw: actual,
actual: parsed || actual,
Expand Down
10 changes: 8 additions & 2 deletions lib/assertions/match.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var samsam = require("samsam");
var actualAndExpectedMessageValues = require("../actual-and-expected-message-values");
var actualForMatch = require("../actual-for-match");

module.exports = function(referee) {
referee.match = function(actual, matcher) {
Expand Down Expand Up @@ -49,7 +49,13 @@ module.exports = function(referee) {
refuteMessage:
"${customMessage}${actual} expected not to match ${expected}",
expectation: "toMatch",
values: actualAndExpectedMessageValues
values: function(actual, matcher, message) {
return {
actual: actualForMatch(actual, matcher),
expected: matcher,
customMessage: message
};
}
});

referee.assert.match.exceptionMessage = referee.refute.match.exceptionMessage =
Expand Down
24 changes: 19 additions & 5 deletions lib/referee.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,20 @@ testHelper.assertionTests("assert", "match", function(pass, fail, msg, error) {
"Vim",
"Emacs"
);
error(
"if object does not contain all properties in matcher",
{
code: "ERR_ASSERTION",
actual: { id: 42, doIt: "yes" },
expected: { id: 42, doIt: "no" },
operator: "assert.match"
},
object,
{
id: 42,
doIt: "no"
}
);
});

testHelper.assertionTests("refute", "match", function(pass, fail, msg, error) {
Expand Down Expand Up @@ -3632,7 +3646,7 @@ testHelper.assertionTests("assert", "matchJson", function(
});
msg(
"with descriptive message",
'[assert.matchJson] Expected { and: 42, key: "value" } to match { key: "different" }',
'[assert.matchJson] Expected { key: "value" } to match { key: "different" }',
'{"key":"value","and":42}',
{ key: "different" }
);
Expand All @@ -3648,11 +3662,11 @@ testHelper.assertionTests("assert", "matchJson", function(
{
code: "ERR_ASSERTION",
actual: { key: "value", and: 42 },
expected: { key: "different" },
expected: { key: "different", and: 42 },
operator: "assert.matchJson"
},
'{"key":"value","and":42}',
{ key: "different" }
'{"key":"value","and":42,"ignoring":true}',
{ key: "different", and: 42 }
);
});

Expand All @@ -3670,7 +3684,7 @@ testHelper.assertionTests("refute", "matchJson", function(
});
msg(
"with descriptive message",
'[refute.matchJson] Expected { and: 42, key: "value" } not to match { key: "value" }',
'[refute.matchJson] Expected { key: "value" } not to match { key: "value" }',
'{"key":"value","and":42}',
{ key: "value" }
);
Expand Down

0 comments on commit 9648ae3

Please sign in to comment.