Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make proxies optional #770

Merged
merged 1 commit into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/chai/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had another thought on this: We should mention that this feature is automatically disabled regardless of this config value in environments that don't support proxies.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, should I put this before the example or after?

  /**
   * ### 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.
   * This feature is automatically disabled regardless of this config value
   * in environments that don`t support proxies.
   *
   * Set it to false if you want to disable this feature.
   *
   *     chai.config.useProxy = false;  // disable use of Proxy
   *
   * @param {Boolean}
   * @api public
   */

vs

  /**
   * ### 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
   */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the second

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*
* 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
};
4 changes: 3 additions & 1 deletion lib/chai/utils/proxify.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
});