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

Assertion error messages handle symbolic method names #1642

Merged
merged 1 commit into from
Jan 7, 2018

Conversation

fongandrew
Copy link
Contributor

Resolves #1640 (assertion failure errors with "Cannot convert a Symbol value to a string")

Background (Problem in detail)

Assertion error messages may reference a method's name (which may now be a symbol, not a string). However, symbols cannot be implicitly converted to strings.

Solution

Explicitly cast symbols to strings with String. The value returned should match http://www.ecma-international.org/ecma-262/6.0/#sec-symboldescriptivestring.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. Run the following in node:
const sinon = require('./lib/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);

The error message should say something about expecting Symbol(my symbol) to be called, not an error about converting the symbol to a string.

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.5% when pulling 089b734 on fongandrew:master into ca9e2fa on sinonjs:master.

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.

Thanks for the PR! This is very welcome, but it needs some touch up 😃


if (typeof Symbol === "function") {
describe("with symbol method names", function () {
function setupSymbol(symbol) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please simplify the logic by making it more functional and more explicit? It seems needlessly complex right now 😃

  • change the setup method
  • remove the bind call in line 1662
  • simplify this.message function (see below) and rename it
  • beforeEach => before
  • improve test descriptions

}

beforeEach(function () {
this.setupSymbol = setupSymbol.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

remove

this.setupSymbol = setupSymbol.bind(this);

/*eslint consistent-return: "off"*/
this.message = function (method) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Simplify into

function createExceptionMessage(method, arg) {
    try { sinonAssert[method](arg); }
    catch (e) { return e.message; }
}

All the uses have at most one argument so we can drop the slicing.

};
});

it("assert.called exception message with symbol with description", function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

The sentence is not valid English. Make it read out aload in a natural way, for instance it("should create an exception message for symbols that describe the symbol"


it("assert.called exception message with symbol with description", function () {
var symbol = Symbol("Something Symbolic");
this.setupSymbol(symbol);
Copy link
Contributor

Choose a reason for hiding this comment

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

setupSymbol(symbol);

var symbol = Symbol("Something Symbolic");
this.setupSymbol(symbol);

assert.equals(this.message("called", this.obj[symbol]),
Copy link
Contributor

Choose a reason for hiding this comment

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

assert.equals(createExceptionMessage("called", obj[symbol]), ...

Resolves sinonjs#1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
@fongandrew
Copy link
Contributor Author

Cleaned up the tests. Please take another look. Thanks!

@coveralls
Copy link

Coverage Status

Coverage remained the same at 96.5% when pulling 8dc158a on fongandrew:master into 8fa1e14 on sinonjs:master.

@fatso83 fatso83 merged commit a8262dd into sinonjs:master Jan 7, 2018
@fatso83
Copy link
Contributor

fatso83 commented Jan 7, 2018

Very nice, thanks!

@mroderick
Copy link
Member

@fatso83 shall we cut a new release?

@fatso83
Copy link
Contributor

fatso83 commented Jan 8, 2018

yes, please :)

@mroderick
Copy link
Member

This has been released with sinon@4.1.4

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.

None yet

4 participants