Make calledWith() assertions idempotent #2407
Merged
+24
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose (TL;DR)
Fix bug where checking
calledWith()
on a spy was not idempotent. Previously, if called repeatedly in such a way that it generated multiple exceptions for a given spy, it would repeatedly quote the called arguments array.Background (Problem in detail)
The best explainer is the failing test I added before fixing the issue:
Which results in this AssertionError:
As you can see, the argument is quoted recursively four times, resulting in an absurd number of backslashes. This is because the arguments array was modified in place while generating the error message.
Obviously this is pretty edge-case - most of the time error messages will only be generated once, because they will throw and end the test. But there's one instance of working around this bug in the Sinon tests themselves (I removed, the workaround, now that it is fixed), and I ran into this while trying to make improvements to sinon-chai, where I am also asserting based on the contents of the error messages, and ran into unexpected deeply-quoted strings.
Solution
We just escape a copy of the arguments, instead of escaping the arguments in place. Again, since this is usually only called once, this should have no other downstream effects.
How to verify
npm install
git checkout HEAD^
to get back to the broken statenpm run test-node -- -f idempotent
if you're impatientgit checkout idempotent-calledWith
Checklist for author
npm run lint
passesnpm run lint
doesn't pass, but none of the errors are related to my changes - they're mostly missing JSDoc comments, and those are the only ones in the files that I modified.Backports
It looks like this was introduced in v9.2.0 with commit 3b1fa42, so this could be backported to 9 and 10 as well - I can open up PRs for that as well once this PR is vetted.