Skip to content

Commit

Permalink
Merge pull request #514 from kpdecker/prop-trace
Browse files Browse the repository at this point in the history
Fix stack trace tracking for property asserts
  • Loading branch information
keithamus committed Sep 8, 2015
2 parents a42ac43 + b6b84e5 commit 5044dc0
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
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
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 5044dc0

Please sign in to comment.