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

Spying on objects with Symbol method names breaks assertion error messages #1640

Closed
fongandrew opened this issue Dec 28, 2017 · 3 comments
Closed

Comments

@fongandrew
Copy link
Contributor

  • Sinon version : 4.1.3
  • Environment : Ubuntu

Code for formatting assertion error messages does not properly account for non-string method names (i.e. symbols). This may be a regression of #1002.

Reproduction Steps

Running this bit of code:

const sinon = require('sinon');
const sym = Symbol('my symbol');
const obj = { [sym]: (a, b) => a + b };
const spy = sinon.spy(obj, sym)
sinon.assert.calledWith(spy, 1, 2);

Results in this error:

TypeError: Cannot convert a Symbol value to a string
    at String.replace (<anonymous>)
    at Function.printf (/path/to/node_modules/sinon/lib/sinon/spy.js:375:31)
    at Object.assert.(anonymous function) [as calledWith] (/path/to/node_modules/sinon/lib/sinon/assert.js:80:68)

Instead of the expected AssertError: expected Symbol(my symbol) to be called with arguments.

Possible fix would be to explicitly type-cast symbol to string at

return formatter.call(null, spyInstance, args);
(i.e. return String(formatter.call(null, spyInstance, args)))

@mroderick
Copy link
Member

Thank you for this very thorough bug report!

Would you be up for creating a pull request to fix it?

fongandrew added a commit to fongandrew/sinon that referenced this issue Dec 30, 2017
Resolves sinonjs#1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
@fongandrew
Copy link
Contributor Author

Created!

fongandrew added a commit to fongandrew/sinon that referenced this issue Jan 7, 2018
Resolves sinonjs#1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
fatso83 pushed a commit that referenced this issue Jan 7, 2018
Resolves #1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
@mroderick
Copy link
Member

Thank you for your contribution 👍

franck-romano pushed a commit to franck-romano/sinon that referenced this issue Oct 1, 2019
Resolves sinonjs#1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
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

No branches or pull requests

2 participants