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

Make calledWith() assertions idempotent #2407

Merged
merged 2 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
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
22 changes: 11 additions & 11 deletions lib/sinon/spy-formatters.js
Expand Up @@ -72,29 +72,29 @@ module.exports = {
j < calledArgs.length || j < expectedArgs.length;
++j
) {
if (calledArgs[j]) {
calledArgs[j] = quoteStringValue(calledArgs[j]);
var calledArg = calledArgs[j];
var expectedArg = expectedArgs[j];
if (calledArg) {
calledArg = quoteStringValue(calledArg);
}

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

message += "\n";

var calledArgMessage =
j < calledArgs.length ? sinonFormat(calledArgs[j]) : "";
if (match.isMatcher(expectedArgs[j])) {
j < calledArgs.length ? sinonFormat(calledArg) : "";
if (match.isMatcher(expectedArg)) {
message += colorSinonMatchText(
expectedArgs[j],
calledArgs[j],
expectedArg,
calledArg,
calledArgMessage
);
} else {
var expectedArgMessage =
j < expectedArgs.length
? sinonFormat(expectedArgs[j])
: "";
j < expectedArgs.length ? sinonFormat(expectedArg) : "";
var diff = jsDiff.diffJson(
calledArgMessage,
expectedArgMessage
Expand Down
14 changes: 13 additions & 1 deletion test/assert-test.js
Expand Up @@ -2172,7 +2172,7 @@ describe("assert", function () {
"expected doSomething to be called once and with exact arguments \n" +
"Call 1:\n"
}${color.red("4")}\n${color.red("3")}\n${color.red(
inspect(JSON.stringify('"bob"'))
inspect('"bob"')
)}\nCall 2:`
);
});
Expand All @@ -2188,6 +2188,18 @@ describe("assert", function () {
);
});

it("assert.calledWith message is idempotent", function () {
this.obj.doSomething("hey");

this.message("calledWith", this.obj.doSomething, "");
this.message("calledWith", this.obj.doSomething, "");
this.message("calledWith", this.obj.doSomething, "");
assert.contains(
this.message("calledWith", this.obj.doSomething, ""),
'"hey"'
);
});

it("assert.alwaysCalledWithExactly exception message", function () {
this.obj.doSomething(1, 3, "hey");
this.obj.doSomething(1, 3);
Expand Down