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 31, 2016
1 parent 481b630 commit 46c24d8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 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 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 in supports proxies and
* if the `useProxy` configuration setting is enabled.
* By default, `then` and `inspect` will not throw an error if they do not exist on the
* assertion object because we should not throw errors on Symbol properties such as
* `Symbol.toStringTag` nor on `.then` because it is necessary for for promise type-checking.
*
* // By default these keys will not throw an error if they do not exist on the assertion object
* chai.config.proxyExcludedKeys = ['then', 'inspect'];
*
* @param {Array}
* @api public
*/

proxyExcludedKeys: ['then', 'inspect']
};
6 changes: 3 additions & 3 deletions lib/chai/utils/proxify.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ module.exports = function proxify (obj) {

return new Proxy(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` in 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 in 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 46c24d8

Please sign in to comment.