Skip to content

Commit

Permalink
Assertion error messages handle symbolic method names
Browse files Browse the repository at this point in the history
Resolves sinonjs#1640 (assertion
failure errors with "Cannot convert a Symbol value to a string")
  • Loading branch information
fongandrew committed Dec 30, 2017
1 parent ca9e2fa commit 089b734
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sinon/spy.js
Original file line number Diff line number Diff line change
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
41 changes: 41 additions & 0 deletions test/assert-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1649,4 +1649,45 @@ describe("assert", function () {
" actual = { foo: 1 }");
});
});

if (typeof Symbol === "function") {
describe("with symbol method names", function () {
function setupSymbol(symbol) {
this.obj = {};
this.obj[symbol] = function () {};
sinonSpy(this.obj, symbol);
}

beforeEach(function () {
this.setupSymbol = setupSymbol.bind(this);

/*eslint consistent-return: "off"*/
this.message = function (method) {
try { // eslint-disable-line no-restricted-syntax
sinonAssert[method].apply(sinonAssert, [].slice.call(arguments, 1));
} catch (e) {
return e.message;
}
};
});

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

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

it("assert.called exception message with symbol without description", function () {
var symbol = Symbol();
this.setupSymbol(symbol);

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

0 comments on commit 089b734

Please sign in to comment.