Skip to content

Commit

Permalink
Fix stack trace tracking for property asserts
Browse files Browse the repository at this point in the history
The ssfi parameter was not being updated for these methods, which ended up causing the trace to be omitted.

Fixes chaijs#358
  • Loading branch information
kpdecker committed Sep 8, 2015
1 parent a42ac43 commit b6b84e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/chai/utils/addProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* MIT Licensed
*/

var config = require('../config');
var flag = require('./flag');

/**
* ### addProperty (ctx, name, getter)
*
Expand Down Expand Up @@ -31,7 +34,11 @@

module.exports = function (ctx, name, getter) {
Object.defineProperty(ctx, name,
{ get: function () {
{ get: function addProperty() {
var old_ssfi = flag(this, 'ssfi');
if (old_ssfi && config.includeStack === false)
flag(this, 'ssfi', addProperty);

var result = getter.call(this);
return result === undefined ? this : result;
}
Expand Down
41 changes: 39 additions & 2 deletions test/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ describe('configuration', function () {
function fooThrows () {
chai.expect('foo').to.be.equal('bar');
}
function fooPropThrows () {
chai.expect('foo').to.not.exist;
}

it('includeStack is true', function () {
it('includeStack is true for method assertions', function () {
chai.config.includeStack = true;

try {
Expand All @@ -38,7 +41,7 @@ describe('configuration', function () {

});

it('includeStack is false', function () {
it('includeStack is false for method assertions', function () {
chai.config.includeStack = false;

try {
Expand All @@ -55,6 +58,40 @@ describe('configuration', function () {
}
});

it('includeStack is true for property assertions', function () {
chai.config.includeStack = true;

try {
fooPropThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// not all browsers support err.stack
// Phantom does not include function names for getter exec
if ('undefined' !== typeof err.stack && 'undefined' !== typeof Error.captureStackTrace) {
assert.include(err.stack, 'addProperty', 'should have internal stack trace in error message');
assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
}
}

});

it('includeStack is false for property assertions', function () {
chai.config.includeStack = false;

try {
fooPropThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
// IE 10 supports err.stack in Chrome format, but without
// `Error.captureStackTrace` support that allows tuning of the error
// message.
if ('undefined' !== typeof err.stack && 'undefined' !== typeof Error.captureStackTrace) {
assert.notInclude(err.stack, 'addProperty', 'should not have internal stack trace in error message');
assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
}
}
});

describe('truncateThreshold', function() {
it('is 20', function() {
chai.config.truncateThreshold = 20;
Expand Down

0 comments on commit b6b84e5

Please sign in to comment.