Skip to content

Commit

Permalink
Check call count type (#2410)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cincodenada authored Jan 20, 2022
1 parent 7863e2d commit 56b0612
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 161 deletions.
14 changes: 10 additions & 4 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion lib/sinon/proxy-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ var callProto = {
}
}
if (this.stack) {
// Omit the error message and the two top stack frames in sinon itself:
// If we have a stack, add the first frame that's in end-user code
// Skip the first two frames because they will refer to Sinon code
callStr += (this.stack.split("\n")[3] || "unknown").replace(
/^\s*(?:at\s+|@)?/,
" at "
Expand Down
Loading

0 comments on commit 56b0612

Please sign in to comment.