Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expectation diff logs #1203

Merged
merged 4 commits into from
Dec 21, 2016

Conversation

jdgreenberger
Copy link
Contributor

@jdgreenberger jdgreenberger commented Dec 7, 2016

Purpose

Addresses issue #1179 by using npm libraries "diff" and "colors" to display colored diffs when arguments to sinonSpy.calledWith do not match the expectation.

Background

Took a stab at implementing diffs for the base case of calledWith. In order to limit changes in this PR, there's a temporary hack to use the original formatting when arguments include sinon matchers. Would appreciate feedback on a better way to implement this, and any other architectural improvements.

Solution

Creates a new formatting type %D on spyApi.formatters in spy.js to take the diff of expected and called arguments and created a useful colored message.

Example:

expected doSomething to be called with arguments 
[{
  first: "a",
-  mismatchKey: true,
+  mismatchKeyX: true,
  second: { nest: true },
-  third: [{ fourth: { nest: true } }]
+  third: [{ fourth: { nest: false } }]
}, "fifth"] 

How to verify -

Check the added tests in assert-test.js

  1. Check out this branch
  2. npm install
  3. mocha test/assert-test.js

@coveralls
Copy link

coveralls commented Dec 7, 2016

Coverage Status

Coverage increased (+0.01%) to 96.332% when pulling 26b715e on jdgreenberger:add-expectation-diff-logs into f6e7916 on sinonjs:master.

@coveralls
Copy link

coveralls commented Dec 7, 2016

Coverage Status

Coverage increased (+0.01%) to 96.332% when pulling 26b715e on jdgreenberger:add-expectation-diff-logs into f6e7916 on sinonjs:master.

@jdgreenberger
Copy link
Contributor Author

@mroderick excited to get this in! Let me know what needs to be changed 😃

Copy link
Contributor

@fatso83 fatso83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This absolutely seems like a nice addition. I just need to understand all the changes and why :-)

@@ -78,6 +78,11 @@ function mirrorPropAsAssertion(name, method, message) {
var args = slice.call(arguments, 1);
var failed = false;

// Hack to be replaced
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a TODO statement. No one ever does anything to a TODO statement. Could you rather expand on your comment? I am not totally sure what this does.

Copy link
Contributor Author

@jdgreenberger jdgreenberger Dec 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 82-84 check for one of the sinon match objects, and if found, revert to using the original message formatting without colored diffs. I did this to avoid making a lot of changes in a single PR, and because the choice to print a diff for a sinon match comparison is a little bit more ambigious than comparing two plain objects (What do I print for expect({x: true}).to.be.calledWith(match.falsy)?). I was hoping one of the contributors could suggest a less hacky way to reserve the diff formatting just for plain object comparison.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If this is explicitly to catch matchers, can you use the built-in sinonMatch.isMatcher(...) on line 82?
  2. Can you update the comment to say why you're doing this (like some of the posted comment above) instead of this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Exactly what I was looking for, thank you 👍

@startswithaj
Copy link

@jdgreenberger great work on this. LGTM.

@@ -78,6 +78,11 @@ function mirrorPropAsAssertion(name, method, message) {
var args = slice.call(arguments, 1);
var failed = false;

// Hack to be replaced
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. If this is explicitly to catch matchers, can you use the built-in sinonMatch.isMatcher(...) on line 82?
  2. Can you update the comment to say why you're doing this (like some of the posted comment above) instead of this one?

@@ -199,7 +204,7 @@ mirrorPropAsAssertion(
);
mirrorPropAsAssertion("calledWithNew", "expected %n to be called with new");
mirrorPropAsAssertion("alwaysCalledWithNew", "expected %n to always be called with new");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %*%C");
mirrorPropAsAssertion("calledWith", "expected %n to be called with arguments %D");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only calledWith? Why not calledWithExactly and others?

@coveralls
Copy link

coveralls commented Dec 20, 2016

Coverage Status

Coverage increased (+0.06%) to 96.374% when pulling f67ab71 on jdgreenberger:add-expectation-diff-logs into f6e7916 on sinonjs:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.06%) to 96.374% when pulling f67ab71 on jdgreenberger:add-expectation-diff-logs into f6e7916 on sinonjs:master.

@coveralls
Copy link

coveralls commented Dec 20, 2016

Coverage Status

Coverage increased (+0.06%) to 96.374% when pulling 4af2809 on jdgreenberger:add-expectation-diff-logs into f6e7916 on sinonjs:master.

@jdgreenberger
Copy link
Contributor Author

jdgreenberger commented Dec 20, 2016

@fearphage knowing about .isMatcher, it wasn't much harder to use colored diffs for the matchers. Updated PR to apply formatting to other relevant calledWith methods
@fatso83

@fatso83
Copy link
Contributor

fatso83 commented Dec 21, 2016

This now looks to be in great IMHO! Could I ask you just to clean up the commit history before merging? Just do an interactive rebase, or whatever you prefer, to get rid of the cleanup log commits.

PS. I am not asking you to squash all the commits. They tell a good story.

@jdgreenberger jdgreenberger force-pushed the add-expectation-diff-logs branch from 4af2809 to 6f346b3 Compare December 21, 2016 16:21
@jdgreenberger
Copy link
Contributor Author

@fatso83 updated!

@coveralls
Copy link

coveralls commented Dec 21, 2016

Coverage Status

Coverage increased (+0.02%) to 96.374% when pulling 6f346b3 on jdgreenberger:add-expectation-diff-logs into 1924a89 on sinonjs:master.

@fatso83 fatso83 merged commit a0c77c0 into sinonjs:master Dec 21, 2016
@fatso83
Copy link
Contributor

fatso83 commented Dec 21, 2016

Perfect! Thanks for the work on this.

@jdgreenberger
Copy link
Contributor Author

No problem! Thanks for the review 😄

BuiltByWalsh pushed a commit to BuiltByWalsh/sinon-chai that referenced this pull request Jul 17, 2017
BuiltByWalsh pushed a commit to BuiltByWalsh/sinon-chai that referenced this pull request Jul 17, 2017
domenic pushed a commit to chaijs/sinon-chai that referenced this pull request Jul 18, 2017
Closes #104. Follows sinonjs/sinon#1203 mostly, although we still include the %* arguments list, as doing otherwise is a bit tricky for the negated case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants