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 stack trace tracking for property asserts #514

Merged
merged 1 commit into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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