Skip to content

Commit

Permalink
Make proxies optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Aug 14, 2016
1 parent 87c3be5 commit 5330fcc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
18 changes: 17 additions & 1 deletion lib/chai/config.js
Expand Up @@ -50,6 +50,22 @@ module.exports = {
* @api public
*/

truncateThreshold: 40
truncateThreshold: 40,

/**
* ### config.useProxy
*
* User configurable property, define if chai will use a Proxy to throw
* an error when a non-existent property is read, which protects users
* from typos when using property-based assertions.
*
* Set it to false if you want to disable this feature.
*
* chai.config.useProxy = false; // disable use of Proxy
*
* @param {Boolean}
* @api public
*/

useProxy: true
};
4 changes: 3 additions & 1 deletion lib/chai/utils/proxify.js
@@ -1,3 +1,5 @@
var config = require('../config');

/*!
* Chai - proxify utility
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
Expand All @@ -17,7 +19,7 @@
*/

module.exports = function proxify (obj) {
if (typeof Proxy === 'undefined' || typeof Reflect === 'undefined')
if (!config.useProxy || typeof Proxy === 'undefined' || typeof Reflect === 'undefined')
return obj;

return new Proxy(obj, {
Expand Down
31 changes: 29 additions & 2 deletions test/configuration.js
@@ -1,5 +1,6 @@
describe('configuration', function () {
var assert = chai.assert;
var expect = chai.expect;

var origConfig;

Expand Down Expand Up @@ -73,7 +74,6 @@ describe('configuration', function () {
assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
}
}

});

it('is false for property assertions', function () {
Expand Down Expand Up @@ -167,6 +167,33 @@ describe('configuration', function () {
chai.config.showDiff = !chai.config.showDiff;
assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
});

});

describe('useProxy', function() {
var readNoExistentProperty = function() {
expect(false).to.be.tue; // typo: tue should be true
};

it('should have default value equal to true', function() {
expect(chai.config.useProxy).to.be.true;
});

describe('when true', function() {
it('should use proxy unless user\'s environment doesn\'t support', function() {
if (typeof Proxy !== 'undefined' || typeof Reflect !== 'undefined') {
expect(readNoExistentProperty).to.throw('Invalid Chai property: tue');
} else {
expect(readNoExistentProperty).to.not.throw('Invalid Chai property: tue');
}
});
});

describe('when false', function() {
it('should not use proxy', function() {
chai.config.useProxy = false;

expect(readNoExistentProperty).to.not.throw('Invalid Chai property: tue');
});
});
});
});

0 comments on commit 5330fcc

Please sign in to comment.