Skip to content

Commit

Permalink
Assertion error messages handle symbolic method names
Browse files Browse the repository at this point in the history
Resolves #1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
  • Loading branch information
fongandrew authored and fatso83 committed Jan 7, 2018
1 parent 8fa1e14 commit a8262dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sinon/spy.js
Expand Up @@ -376,7 +376,7 @@ var spyApi = {
formatter = spyApi.formatters[specifyer];

if (typeof formatter === "function") {
return formatter.call(null, spyInstance, args);
return String(formatter.call(null, spyInstance, args));
} else if (!isNaN(parseInt(specifyer, 10))) {
return sinonFormat(args[specifyer - 1]);
}
Expand Down
38 changes: 38 additions & 0 deletions test/assert-test.js
Expand Up @@ -1649,4 +1649,42 @@ describe("assert", function () {
" actual = { foo: 1 }");
});
});

if (typeof Symbol === "function") {
describe("with symbol method names", function () {
var obj = {};

function setupSymbol(symbol) {
obj[symbol] = function () {};
sinonSpy(obj, symbol);
}

function createExceptionMessage(method, arg) {
try { // eslint-disable-line no-restricted-syntax
sinonAssert[method](arg);
} catch (e) {
return e.message;
}
}

it("should use the symbol's description in exception messages", function () {
var symbol = Symbol("Something Symbolic");
setupSymbol(symbol);

assert.equals(createExceptionMessage("called", obj[symbol]),
"expected Symbol(Something Symbolic) to have been " +
"called at least once but was never called");
});

it("should indicate that an assertion failure with a symbol method name " +
"occured in exception messages, even if the symbol has no description", function () {
var symbol = Symbol();
setupSymbol(symbol);

assert.equals(createExceptionMessage("called", obj[symbol]),
"expected Symbol() to have been " +
"called at least once but was never called");
});
});
}
});

0 comments on commit a8262dd

Please sign in to comment.