Skip to content

calledWith ignores symbols used as property keys #1974

@BrandonE

Description

@BrandonE

Describe the bug
Asserting calledWith ignores mismatched properties if they keys are JavaScript symbols.

To Reproduce
Steps to reproduce the behavior:

var sinon = require('sinon');
var mock = sinon.mock();

mock({
    [Symbol("a")]: true
});

sinon.assert.calledWith(
    mock,
    {
        [Symbol("a")]: false
    }
);

Expected behavior
The above script should throw an AssertError because the mock was called with an object containing a true value when we were expecting a false value. It should behave similarly to this example that uses non-symbolic keys:

var sinon = require('sinon');
var mock = sinon.mock();

mock({
    a: true
});

sinon.assert.calledWith(
    mock,
    {
        a: false
    }
);

image

Context (please complete the following information):

  • Library version: 7.2.3
  • Environment: Node.js v10.8.0
  • Other libraries you are using: None; I reproduced this issue in a newly initialized npm project.

Additional context
I started working on a solution, but I wanted to raise an issue before opening pull requests like the contributing guide suggests to make sure I'm going down the right path. Using this change for samsam and this change for formatio, I get the behavior I expected for the script in the To Reproduce section:

image

Because symbols are unique, this:

var sinon = require('sinon');
var mock = sinon.mock();

mock({
    [Symbol("a")]: true
});

sinon.assert.calledWith(
    mock,
    {
        [Symbol("a")]: true
    }
);

will throw an AssertError (thanks @pettyalex for pointing this out):

image

This, however, will not throw an AssertError:

var sinon = require('sinon');
var mock = sinon.mock();

const symbol = Symbol("a");

mock({
    [symbol]: true
});

sinon.assert.calledWith(
    mock,
    {
        [symbol]: true
    }
);

Unfortunately, the samsam change breaks one unit test:

image

This is because arguments has a property symbol called iterator:

> function test() { return Object.getOwnPropertySymbols(arguments) } test();
[ Symbol(Symbol.iterator) ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions