Skip to content

Commit

Permalink
backport: backport: Check type of callCount argument and error accord…
Browse files Browse the repository at this point in the history
…ingly

This is to fixes sinonjs#2408, which could result in error messages like
"expected spy to be called 10 times but was called 10 times".

Now we will instead say "expected '10' to be a number, but was of type
string", which is much clearer!
  • Loading branch information
cincodenada committed Nov 5, 2021
1 parent 5d0adf7 commit 2849e90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,14 @@ assert = {
callCount: function assertCallCount(method, count) {
verifyIsStub(method);

if (method.callCount !== count) {
var msg = "expected %n to be called " + timesInWords(count) + " but was called %c%C";
var msg;
if (typeof count !== "number") {
msg =
`expected ${format(count)} to be a number ` +
`but was of type ${typeof count}`;
failAssertion(this, msg);
} else if (method.callCount !== count) {
msg = "expected %n to be called " + timesInWords(count) + " but was called %c%C";
failAssertion(this, method.printf(msg));
} else {
assert.pass("callCount");
Expand Down
12 changes: 12 additions & 0 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,18 @@ describe("assert", function() {
);
});

it("assert.callCount exception message with non-numeric argument", function () {
this.obj.doSomething();

assert.equals(
this.message("callCount", this.obj.doSomething, "3").replace(
/ at.*/g,
""
),
"expected '3' to be a number but was of type string"
);
});

it("assert.calledOnce exception message", function() {
this.obj.doSomething();
this.obj.doSomething();
Expand Down

0 comments on commit 2849e90

Please sign in to comment.