From 42ac44fd7ce285fdafab60b0b3f9a861c1fea1c5 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Wed, 3 Nov 2021 10:47:54 -0700 Subject: [PATCH] Check type of callCount argument and error accordingly This is to fixes #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! --- lib/sinon/assert.js | 14 ++++++++++---- test/assert-test.js | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/sinon/assert.js b/lib/sinon/assert.js index ffd34e550..5af71bbb6 100644 --- a/lib/sinon/assert.js +++ b/lib/sinon/assert.js @@ -159,10 +159,16 @@ function createAssertObject() { 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"); diff --git a/test/assert-test.js b/test/assert-test.js index e571d2247..f59dff901 100644 --- a/test/assert-test.js +++ b/test/assert-test.js @@ -1570,6 +1570,15 @@ 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"), + "expected '3' to be a number but was of type string" + ); + }); + it("assert.calledOnce exception message", function () { this.obj.doSomething(); this.obj.doSomething();