Skip to content

Commit

Permalink
Add proxyExcludedKeys config setting. Closes chaijs#765
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfcosta committed Aug 23, 2016
1 parent 481b630 commit 9b2163f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
22 changes: 21 additions & 1 deletion lib/chai/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,25 @@ module.exports = {
* @api public
*/

useProxy: true
useProxy: true,

/**
* ### config.proxyExcludedKeys
*
* User configurable property, defines which non-existing properties should be ignored
* instead of throwing an error if they do not exist on the assertion.
* This is only applied if the environment Chai is running into supports proxies and
* if the `useProxy` configuration setting is enabled.
*
* // By default these keys will not throw an error if they do not exist on the assertion object
* chai.config.proxyExcludedKeys = ['then', 'inspect'];
*
* This feature is automatically disabled regardless of this config value
* in environments that don't support proxies.
*
* @param {Array}
* @api public
*/

proxyExcludedKeys: ['then', 'inspect']
};
4 changes: 3 additions & 1 deletion lib/chai/utils/proxify.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ module.exports = function proxify (obj) {
get: function getProperty (target, property) {
// Don't throw error on Symbol properties such as Symbol.toStringTag, nor
// on .then because it's necessary for promise type-checking.
// The values for which an error should be thrown can be configured using
// the `config.proxyExcludedKeys` setting.
if (typeof property === 'string' &&
property !== 'then' &&
config.proxyExcludedKeys.indexOf(property) === -1 &&
!Reflect.has(target, property))
throw Error('Invalid Chai property: ' + property);

Expand Down
33 changes: 33 additions & 0 deletions test/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,37 @@ describe('configuration', function () {
});
});
});

describe('proxyExcludedKeys', function() {
var readNoExistentProperty = function(prop) {
return function() {
var assertion = expect(false);
expect(assertion).to.not.have.key(prop);
assertion[prop];
}
};

it('should have default value equal to `[\'then\', \'inspect\']`', function() {
expect(chai.config.proxyExcludedKeys).to.be.deep.equal(['then', 'inspect']);
});

it('should not throw when accessing non-existing `then` and `inspect` on an environment with proxy support', function() {
// Since these will not throw if the environment does not support proxies we don't need any `if` clause here
expect(readNoExistentProperty('then')).to.not.throw();
expect(readNoExistentProperty('inspect')).to.not.throw();
});

it('should throw for properties which are not on the `proxyExcludedKeys` Array on an environment with proxy support', function() {
chai.config.proxyExcludedKeys = [];

if (typeof Proxy !== 'undefined' && typeof Reflect !== 'undefined') {
expect(readNoExistentProperty('then')).to.throw('Invalid Chai property: then');
expect(readNoExistentProperty('inspect')).to.throw('Invalid Chai property: inspect');
} else {
expect(readNoExistentProperty('then')).to.not.throw();
expect(readNoExistentProperty('inspect')).to.not.throw();
}
});
});

});

0 comments on commit 9b2163f

Please sign in to comment.