From 0f34cb50a553074093b22b81ba18a6ca804b7981 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 13 Aug 2016 18:01:14 -0300 Subject: [PATCH] Make proxies optional --- lib/chai/config.js | 21 ++++++++++++++++++++- lib/chai/utils/proxify.js | 4 +++- test/configuration.js | 31 +++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/chai/config.js b/lib/chai/config.js index a25fed39e..20f79cb98 100644 --- a/lib/chai/config.js +++ b/lib/chai/config.js @@ -50,6 +50,25 @@ module.exports = { * @api public */ - truncateThreshold: 40 + truncateThreshold: 40, + /** + * ### config.useProxy + * + * User configurable property, defines 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 + * + * This feature is automatically disabled regardless of this config value + * in environments that don`t support proxies. + * + * @param {Boolean} + * @api public + */ + + useProxy: true }; diff --git a/lib/chai/utils/proxify.js b/lib/chai/utils/proxify.js index 581a9dc98..6baaff260 100644 --- a/lib/chai/utils/proxify.js +++ b/lib/chai/utils/proxify.js @@ -1,3 +1,5 @@ +var config = require('../config'); + /*! * Chai - proxify utility * Copyright(c) 2012-2014 Jake Luer @@ -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, { diff --git a/test/configuration.js b/test/configuration.js index 3ce43254c..991681c04 100644 --- a/test/configuration.js +++ b/test/configuration.js @@ -1,5 +1,6 @@ describe('configuration', function () { var assert = chai.assert; + var expect = chai.expect; var origConfig; @@ -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 () { @@ -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'); + }); + }); }); });