Skip to content

Commit 56b0612

Browse files
authored
Check call count type (#2410)
* Strip stack frames in `this.message` This saves us from having to do it every time, and makes things much nicer. Also use a little bit more specific regex, to avoid issues with messages that happen to contain the word "at" * 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! * A little more explanatory comment * Edit the comment about appending stack frames What's actually happening here is that we want to add a frame of context to `callStr`, but the first two stack frames will be within Sinon code and thus probably not helpful to the end-user. So, we skip the first two stack frames, and append the third stack frame, which should contain a meaningful location to the end-user. * Add test for adding stack traces to error message This ensures that if at some point we end up with another Sinon layer in the stack at some point, we'll catch it and hopefully adjust accordingly For reference, as of this commit, the Sinon portion of the stack is: lib/sinon/proxy-invoke.js:65:15 lib/sinon/proxy.js:265:26 Also convert a neighboring test to async while we're at it
1 parent 7863e2d commit 56b0612

4 files changed

Lines changed: 89 additions & 161 deletions

File tree

lib/sinon/assert.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,16 @@ function createAssertObject() {
159159
callCount: function assertCallCount(method, count) {
160160
verifyIsStub(method);
161161

162-
if (method.callCount !== count) {
163-
var msg = `expected %n to be called ${timesInWords(
164-
count
165-
)} but was called %c%C`;
162+
var msg;
163+
if (typeof count !== "number") {
164+
msg =
165+
`expected ${format(count)} to be a number ` +
166+
`but was of type ${typeof count}`;
167+
failAssertion(this, msg);
168+
} else if (method.callCount !== count) {
169+
msg =
170+
`expected %n to be called ${timesInWords(count)} ` +
171+
`but was called %c%C`;
166172
failAssertion(this, method.printf(msg));
167173
} else {
168174
assert.pass("callCount");

lib/sinon/proxy-call.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ var callProto = {
220220
}
221221
}
222222
if (this.stack) {
223-
// Omit the error message and the two top stack frames in sinon itself:
223+
// If we have a stack, add the first frame that's in end-user code
224+
// Skip the first two frames because they will refer to Sinon code
224225
callStr += (this.stack.split("\n")[3] || "unknown").replace(
225226
/^\s*(?:at\s+|@)?/,
226227
" at "

0 commit comments

Comments
 (0)