From f93cd3b1ba416acc0e877376eb7ff1fd549b5621 Mon Sep 17 00:00:00 2001 From: Kevin Ennis Date: Thu, 23 Mar 2017 10:49:38 -0400 Subject: [PATCH] Fix error on call.toString() where stack has fewer than 4 lines. Fixes #1066 --- lib/sinon/call.js | 2 +- test/call-test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/sinon/call.js b/lib/sinon/call.js index 38bf8076c..9d8a2aab4 100644 --- a/lib/sinon/call.js +++ b/lib/sinon/call.js @@ -176,7 +176,7 @@ var callProto = { } if (this.stack) { // Omit the error message and the two top stack frames in sinon itself: - callStr += this.stack.split("\n")[3].replace(/^\s*(?:at\s+|@)?/, " at "); + callStr += ( this.stack.split("\n")[3] || "unknown" ).replace(/^\s*(?:at\s+|@)?/, " at "); } return callStr; diff --git a/test/call-test.js b/test/call-test.js index 3a080a20d..31b919a4f 100644 --- a/test/call-test.js +++ b/test/call-test.js @@ -835,6 +835,23 @@ describe("sinonSpy.call", function () { assert.equals(object.doIt.getCall(0).toString().replace(/ at.*/g, ""), "doIt() => 42"); }); + + // https://github.com/sinonjs/sinon/issues/1066 + it("does not throw when the call stack is empty", function (done) { + var stub1 = sinonStub().resolves(1); + var stub2 = sinonStub().returns(1); + + function run() { + return stub1().then(stub2); + } + + run() + .then(function () { + assert.equals(stub2.getCall(0).toString().replace(/ at.*/g, ""), "stub(1) => 1"); + done(); + }) + .catch( done ); + }); }); describe("constructor", function () {