Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on call.toString() where stack has fewer than 4 lines. #1359

Merged
merged 1 commit into from Mar 27, 2017
Merged

Fix error on call.toString() where stack has fewer than 4 lines. #1359

merged 1 commit into from Mar 27, 2017

Conversation

kevincennis
Copy link
Contributor

Fixes #1066

Purpose

Fixes #1066 by not calling String#replace on undefined, which could happen when an error stack had fewer than 4 lines (e.g. when the offending function was called point-free in Promise#then()).

Background

Here's a very simple reduced test case that reproduces the issue:

let sinon = require('sinon');

function test() {

  let stub1 = sinon.stub().returns( Promise.resolve({}) );
  let stub2 = sinon.stub();

  function run() {
    return stub1().then( stub2 );
  }

  run()
  .then( () => sinon.assert.calledTwice( stub2 ) )
  .catch( console.log );

}

setTimeout( test, 0 );

Because stub2 is invoked directly by the VM, it has nothing in its stack. That breaks call.toString(), which assumes each stack consists of at least 4 lines (the first 3 of which are Sinon-related, and the 4th being the beginning of the user's call stack).

Solution

Simply safe-guard against the String#replace() call by providing a default stack value of "unknown".

How to verify

  1. Check out this branch (see github instructions below)
  2. npm install
  3. npm test

The new test sinonSpy.call > call.toString > does not throw when the call stack is empty will pass.

Or try the example under Background above.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 95.375% when pulling f93cd3b on kevincennis:kce/fix-1066 into f701abe on sinonjs:master.

@fatso83 fatso83 merged commit c5bc9ab into sinonjs:master Mar 27, 2017
@fatso83
Copy link
Contributor

fatso83 commented Mar 27, 2017

Thanks for the work. Much appreciated!

@mroderick mroderick added the semver:patch changes will cause a new patch version label May 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:patch changes will cause a new patch version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with getStackFrames()[0]
4 participants