Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cfb3b8

Browse files
committed
Make proxies optional
1 parent 87c3be5 commit 9cfb3b8

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

lib/chai/config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ module.exports = {
5050
* @api public
5151
*/
5252

53-
truncateThreshold: 40
53+
truncateThreshold: 40,
5454

55+
/**
56+
* ### config.useProxy
57+
*
58+
* User configurable property, defines if chai will use a Proxy to throw
59+
* an error when a non-existent property is read, which protects users
60+
* from typos when using property-based assertions.
61+
*
62+
* Set it to false if you want to disable this feature.
63+
*
64+
* chai.config.useProxy = false; // disable use of Proxy
65+
*
66+
* @param {Boolean}
67+
* @api public
68+
*/
69+
70+
useProxy: true
5571
};

lib/chai/utils/proxify.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var config = require('../config');
2+
13
/*!
24
* Chai - proxify utility
35
* Copyright(c) 2012-2014 Jake Luer <[email protected]>
@@ -17,7 +19,7 @@
1719
*/
1820

1921
module.exports = function proxify (obj) {
20-
if (typeof Proxy === 'undefined' || typeof Reflect === 'undefined')
22+
if (!config.useProxy || typeof Proxy === 'undefined' || typeof Reflect === 'undefined')
2123
return obj;
2224

2325
return new Proxy(obj, {

test/configuration.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
describe('configuration', function () {
22
var assert = chai.assert;
3+
var expect = chai.expect;
34

45
var origConfig;
56

@@ -73,7 +74,6 @@ describe('configuration', function () {
7374
assert.include(err.stack, 'fooPropThrows', 'should have user stack trace in error message');
7475
}
7576
}
76-
7777
});
7878

7979
it('is false for property assertions', function () {
@@ -167,6 +167,33 @@ describe('configuration', function () {
167167
chai.config.showDiff = !chai.config.showDiff;
168168
assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
169169
});
170-
170+
});
171+
172+
describe('useProxy', function() {
173+
var readNoExistentProperty = function() {
174+
expect(false).to.be.tue; // typo: tue should be true
175+
};
176+
177+
it('should have default value equal to true', function() {
178+
expect(chai.config.useProxy).to.be.true;
179+
});
180+
181+
describe('when true', function() {
182+
it('should use proxy unless user\'s environment doesn\'t support', function() {
183+
if (typeof Proxy !== 'undefined' && typeof Reflect !== 'undefined') {
184+
expect(readNoExistentProperty).to.throw('Invalid Chai property: tue');
185+
} else {
186+
expect(readNoExistentProperty).to.not.throw('Invalid Chai property: tue');
187+
}
188+
});
189+
});
190+
191+
describe('when false', function() {
192+
it('should not use proxy', function() {
193+
chai.config.useProxy = false;
194+
195+
expect(readNoExistentProperty).to.not.throw('Invalid Chai property: tue');
196+
});
197+
});
171198
});
172199
});

0 commit comments

Comments
 (0)