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

Improves error message for toHaveBeenCalledWith #48

Closed
wants to merge 1 commit into from
Closed

Improves error message for toHaveBeenCalledWith #48

wants to merge 1 commit into from

Conversation

everson
Copy link
Contributor

@everson everson commented Nov 28, 2015

Often when trying test using spied on functions, I find it hard to debug because you cannot tell if the function was called at all or which arguments it was called with. In this PR I try to address both issues by:

  • short circuit and let the user know that the function was not called, instead of the spy was never called with xxx.
  • in case it was called but not with the expected arguments, list the arguments it was called.

Again, thanks for the library and I am happy to address any issues you have with my proposed changes.

for(const arg of spy.calls) {
actualArguments = actualArguments + '- ' + JSON.stringify(arg.arguments) + '\n'
}
const message = 'spy was never called with:\n%s.\n\nIt was called with:\n' + actualArguments
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using the %s place holder instead of the concatenation here, but it did not interpreted the \n, outputting it literally without breaking the line.

Copy link
Owner

Choose a reason for hiding this comment

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

One of the drawbacks to computing this message ahead of time like this is that we may not actually need it. For example, if the assertion passes, we will have done all this computation for nothing (i.e. we won't ever actually have to show this message). That's the great thing about the way our assert function works–it doesn't do any work at all if the assertion passes. It would be nice to keep this behavior so we don't slow tests down.

Also, please keep with existing code style everywhere (spacing, etc.)

@mjackson
Copy link
Owner

mjackson commented Dec 9, 2015

Hmm, not sure why the build failed. I just restarted it.

@everson
Copy link
Contributor Author

everson commented Dec 24, 2015

I have updated PR to reflect your comments and rebased commits. Please take a look when you have a moment.

@everson
Copy link
Contributor Author

everson commented Apr 5, 2016

@mjackson Do you intend to merge this or you are going to wait for #60?

)
const genMessage = () => {
const actualArguments = spy.calls
.map(call => `- ${JSON.stringify(call.arguments)}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

json doesn't support functions, or null, and a few other JS types - is this the best method to represent the items?

you may want to look into the object-inspect module here.

@ljharb
Copy link
Collaborator

ljharb commented Apr 5, 2016

@khronnuz there's still a merge commit in the history - would you mind rebasing this on latest master?

@everson
Copy link
Contributor Author

everson commented Apr 9, 2016

@ljharb done.

@ljharb
Copy link
Collaborator

ljharb commented Apr 9, 2016

Thanks! It might be nice to add some tests too, sorry I didn't mention that earlier :-/

@everson
Copy link
Contributor Author

everson commented Apr 10, 2016

@ljharb I've added a test case. Please take a look if you are happy with the test case or need more examples. Also, please check if you like how I've removed the indentation from the expected message here 0a11d3b#diff-c0516c644bb1bfc36e0180d5855333e8R51

@ljharb
Copy link
Collaborator

ljharb commented Apr 10, 2016

Seems good! I'm not sure if I like the indentation approach but I think it's totally reasonable for now.

@robcolburn
Copy link

Is this enough? I feel like you need to get a complete-ish history of calls (may have been called multiple times) with detailed output of each.

For instance, maybe you want to do:

expect(someReduxStore.dispatch).toHaveBeenCalledWith({
  type: 'ACTION',
  mega: {death: {deep: [args, from, an, api: {of: stuff
});

I ran a similar issues with chai-spies, attempted expect as an alternative ;).

Chai-spies issue is here: chaijs/chai-spies#29

This still wasn't quite enough, so I wrote my own dirty patch. Dirty because console logging isn't exactly kosher chai, and npm is not friendly about patching.

@mjackson
Copy link
Owner

In general I think our error messages leave a lot to be desired, but this is an ok first pass. I'd like to take a hard look at the way we're generating error messages soon and rework it to be more flexible.

@khronnuz Would you mind rebasing on current master? Thanks.

@everson
Copy link
Contributor Author

everson commented Apr 23, 2016

@mjackson done.

@shw6rn
Copy link

shw6rn commented Nov 18, 2016

Are there any plans to complete this PR? This would be a huge improvement over the current error messages.

@everson
Copy link
Contributor Author

everson commented Nov 18, 2016

@shw6rn I believe we need to wait for #60. Closing this PR was it was not accepted.

@everson everson closed this Nov 18, 2016
@everson everson deleted the toHaveBeenCalledWith branch September 5, 2017 06:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants