Skip to content

Commit

Permalink
Update error message on assert when representation of expected and ac…
Browse files Browse the repository at this point in the history
…tual value is equal, fixing issue #2084 (#2303)

Co-authored-by: Roman Balayan <roman.balayan@paymaya.com>
  • Loading branch information
romanbalayan and Roman Balayan committed Oct 13, 2020
1 parent 60e2739 commit 3b1fa42
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
27 changes: 23 additions & 4 deletions lib/sinon/spy-formatters.js
Expand Up @@ -10,6 +10,7 @@ var jsDiff = require("diff");
var join = arrayProto.join;
var map = arrayProto.map;
var push = arrayProto.push;
var slice = arrayProto.slice;

function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
var calledArgumentMessage = calledArgMessage;
Expand Down Expand Up @@ -38,6 +39,13 @@ function colorDiffText(diff) {
return join(objects, "");
}

function quoteStringValue(value) {
if (typeof value === "string") {
return JSON.stringify(value);
}
return value;
}

module.exports = {
c: function(spyInstance) {
return timesInWords(spyInstance.callCount);
Expand All @@ -57,13 +65,24 @@ module.exports = {
message += "\nCall " + (i + 1) + ":";
}
var calledArgs = spyInstance.getCall(i).args;
for (var j = 0; j < calledArgs.length || j < args.length; ++j) {
var expectedArgs = slice(args);

for (var j = 0; j < calledArgs.length || j < expectedArgs.length; ++j) {
if (calledArgs[j]) {
calledArgs[j] = quoteStringValue(calledArgs[j]);
}

if (expectedArgs[j]) {
expectedArgs[j] = quoteStringValue(expectedArgs[j]);
}

message += "\n";

var calledArgMessage = j < calledArgs.length ? sinonFormat(calledArgs[j]) : "";
if (match.isMatcher(args[j])) {
message += colorSinonMatchText(args[j], calledArgs[j], calledArgMessage);
if (match.isMatcher(expectedArgs[j])) {
message += colorSinonMatchText(expectedArgs[j], calledArgs[j], calledArgMessage);
} else {
var expectedArgMessage = j < args.length ? sinonFormat(args[j]) : "";
var expectedArgMessage = j < expectedArgs.length ? sinonFormat(expectedArgs[j]) : "";
var diff = jsDiff.diffJson(calledArgMessage, expectedArgMessage);
message += colorDiffText(diff);
}
Expand Down
43 changes: 28 additions & 15 deletions test/assert-test.js
Expand Up @@ -1657,7 +1657,7 @@ describe("assert", function() {
color.green("1") +
" \n" +
"3\n" +
"hey"
'"hey"'
);
});

Expand All @@ -1674,13 +1674,13 @@ describe("assert", function() {
color.green("1") +
" \n" +
"3\n" +
"hey\n" +
'"hey"\n' +
"Call 2:\n" +
"1\n" +
"3\n" +
color.red("not") +
color.red('"not"') +
" " +
color.green("hey") +
color.green('"hey"') +
" "
);
});
Expand Down Expand Up @@ -1892,7 +1892,7 @@ describe("assert", function() {
color.green("4") +
" \n" +
"3\n" +
"hey"
'"hey"'
);
});

Expand All @@ -1907,13 +1907,13 @@ describe("assert", function() {
"1\n" +
color.red("3") +
" " +
color.green("hey") +
color.green('"hey"') +
" \n" +
color.red("hey") +
color.red('"hey"') +
"\n" +
"Call 2:\n" +
"1\n" +
"hey"
'"hey"'
);
});

Expand All @@ -1928,13 +1928,13 @@ describe("assert", function() {
"1\n" +
color.red("3") +
" " +
color.green("hey") +
color.green('"hey"') +
" \n" +
color.red("hey") +
color.red('"hey"') +
"\n" +
"Call 2:\n" +
"1\n" +
"hey"
'"hey"'
);
});

Expand All @@ -1943,7 +1943,7 @@ describe("assert", function() {

assert.equals(
this.message("calledWithExactly", this.obj.doSomething, 1, 3).replace(/ at.*/g, ""),
"expected doSomething to be called with exact arguments \n1\n3\n" + color.red("hey")
"expected doSomething to be called with exact arguments \n1\n3\n" + color.red('"hey"')
);
});

Expand All @@ -1962,7 +1962,7 @@ describe("assert", function() {
color.green("1") +
" \n" +
"3\n" +
"bob"
'"bob"'
);

this.obj.doSomething();
Expand All @@ -1974,12 +1974,25 @@ describe("assert", function() {
"\n" +
color.red("3") +
"\n" +
color.red("bob") +
color.red(JSON.stringify('"bob"')) +
"\n" +
"Call 2:"
);
});

it("assert.calledWith exception message with equal string representations", function() {
this.obj.doSomething(1234);

assert.equals(
this.message("calledWith", this.obj.doSomething, "1234"),
"expected doSomething to be called with arguments \n" +
color.red(1234) +
" " +
color.green('"1234"') +
" "
);
});

it("assert.alwaysCalledWithExactly exception message", function() {
this.obj.doSomething(1, 3, "hey");
this.obj.doSomething(1, 3);
Expand All @@ -1990,7 +2003,7 @@ describe("assert", function() {
"Call 1:\n" +
"1\n" +
"3\n" +
color.red("hey") +
color.red('"hey"') +
"\n" +
"Call 2:\n" +
"1\n" +
Expand Down
4 changes: 2 additions & 2 deletions test/proxy-test.js
Expand Up @@ -161,7 +161,7 @@ describe("proxy", function() {
"\n" +
color.red("1") +
"\n" +
color.red("a") +
color.red('"a"') +
"\n" +
color.red("true") +
"\n" +
Expand Down Expand Up @@ -198,7 +198,7 @@ describe("proxy", function() {
"\n" +
color.red("1") +
"\n" +
color.red("a") +
color.red('"a"') +
"\n" +
color.red("true") +
"\nCall 2:" +
Expand Down

0 comments on commit 3b1fa42

Please sign in to comment.