Skip to content

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

@fongandrew

Description

@fongandrew
  • 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)))

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy