From be6e78003ba7d5a793e54e5e17e4e7934d5257ea Mon Sep 17 00:00:00 2001 From: Andrew Miller Date: Mon, 4 Dec 2017 14:46:13 -0800 Subject: [PATCH] feat(chai/config): add 'catch' keyword to proxyExcluedKeys closes #1050 --- chai.js | 77 ++++++++++++++++++++++++------------------- lib/chai/config.js | 2 +- test/configuration.js | 4 +-- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/chai.js b/chai.js index 792edbae9..2973625e5 100644 --- a/chai.js +++ b/chai.js @@ -355,7 +355,7 @@ module.exports = { * @api public */ - proxyExcludedKeys: ['then', 'inspect', 'toJSON'] + proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON'] }; },{}],5:[function(require,module,exports){ @@ -571,7 +571,6 @@ module.exports = function (chai, _) { flag(this, 'all', false); }); - /** * ### .all * @@ -950,9 +949,9 @@ module.exports = function (chai, _) { /** * ### .ok * - * Asserts that the target is loosely (`==`) equal to `true`. However, it's - * often best to assert that the target is strictly (`===`) or deeply equal to - * its expected value. + * Asserts that the target is a truthy value (considered `true` in boolean context). + * However, it's often best to assert that the target is strictly (`===`) or + * deeply equal to its expected value. * * expect(1).to.equal(1); // Recommended * expect(1).to.be.ok; // Not recommended @@ -1503,6 +1502,7 @@ module.exports = function (chai, _) { , ssfi = flag(this, 'ssfi') , objType = _.type(obj).toLowerCase() , nType = _.type(n).toLowerCase() + , errorMessage , shouldThrow = true; if (doLength) { @@ -1599,6 +1599,7 @@ module.exports = function (chai, _) { , ssfi = flag(this, 'ssfi') , objType = _.type(obj).toLowerCase() , nType = _.type(n).toLowerCase() + , errorMessage , shouldThrow = true; if (doLength) { @@ -1694,6 +1695,7 @@ module.exports = function (chai, _) { , ssfi = flag(this, 'ssfi') , objType = _.type(obj).toLowerCase() , nType = _.type(n).toLowerCase() + , errorMessage , shouldThrow = true; if (doLength) { @@ -1789,6 +1791,7 @@ module.exports = function (chai, _) { , ssfi = flag(this, 'ssfi') , objType = _.type(obj).toLowerCase() , nType = _.type(n).toLowerCase() + , errorMessage , shouldThrow = true; if (doLength) { @@ -1885,6 +1888,7 @@ module.exports = function (chai, _) { , objType = _.type(obj).toLowerCase() , startType = _.type(start).toLowerCase() , finishType = _.type(finish).toLowerCase() + , errorMessage , shouldThrow = true , range = (startType === 'date' && finishType === 'date') ? start.toUTCString() + '..' + finish.toUTCString() @@ -2119,10 +2123,30 @@ module.exports = function (chai, _) { , isOwn = flag(this, 'own') , flagMsg = flag(this, 'message') , obj = flag(this, 'object') - , ssfi = flag(this, 'ssfi'); + , ssfi = flag(this, 'ssfi') + , nameType = typeof name; + + flagMsg = flagMsg ? flagMsg + ': ' : ''; + + if (isNested) { + if (nameType !== 'string') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string when using nested syntax', + undefined, + ssfi + ); + } + } else { + if (nameType !== 'string' && nameType !== 'number' && nameType !== 'symbol') { + throw new AssertionError( + flagMsg + 'the argument to property must be a string, number, or symbol', + undefined, + ssfi + ); + } + } if (isNested && isOwn) { - flagMsg = flagMsg ? flagMsg + ': ' : ''; throw new AssertionError( flagMsg + 'The "nested" and "own" flags cannot be combined.', undefined, @@ -2131,7 +2155,6 @@ module.exports = function (chai, _) { } if (obj === null || obj === undefined) { - flagMsg = flagMsg ? flagMsg + ': ' : ''; throw new AssertionError( flagMsg + 'Target cannot be null or undefined.', undefined, @@ -2610,6 +2633,7 @@ module.exports = function (chai, _) { , isDeep = flag(this, 'deep') , str , deepStr = '' + , actual , ok = true , flagMsg = flag(this, 'message'); @@ -2626,7 +2650,6 @@ module.exports = function (chai, _) { if (keysType !== 'Array') { keys = Array.prototype.slice.call(arguments); } - } else { actual = _.getOwnEnumerableProperties(obj); @@ -2659,8 +2682,7 @@ module.exports = function (chai, _) { var len = keys.length , any = flag(this, 'any') , all = flag(this, 'all') - , expected = keys - , actual; + , expected = keys; if (!any && !all) { all = true; @@ -3362,7 +3384,7 @@ module.exports = function (chai, _) { var contains = flag(this, 'contains'); var ordered = flag(this, 'ordered'); - var subject, failMsg, failNegateMsg, lengthCheck; + var subject, failMsg, failNegateMsg; if (contains) { subject = ordered ? 'an ordered superset' : 'a superset'; @@ -3434,7 +3456,6 @@ module.exports = function (chai, _) { Assertion.addMethod('oneOf', oneOf); - /** * ### .change(subject[, prop[, msg]]) * @@ -4082,7 +4103,7 @@ module.exports = function (chai, _) { var obj = flag(this, 'object'); this.assert( - typeof obj === "number" && isFinite(obj) + typeof obj === 'number' && isFinite(obj) , 'expected #{this} to be a finite number' , 'expected #{this} to not be a finite number' ); @@ -4096,9 +4117,7 @@ module.exports = function (chai, _) { * MIT Licensed */ - module.exports = function (chai, util) { - /*! * Chai dependencies. */ @@ -7586,8 +7605,6 @@ module.exports = function addChainableMethod(ctx, name, method, chainingBehavior }; },{"../../chai":2,"./addLengthGuard":10,"./flag":15,"./proxify":30,"./transferFlags":32}],10:[function(require,module,exports){ -var config = require('../config'); - var fnLengthDesc = Object.getOwnPropertyDescriptor(function () {}, 'length'); /*! @@ -7649,7 +7666,7 @@ module.exports = function addLengthGuard (fn, assertionName, isChainable) { return fn; }; -},{"../config":4}],11:[function(require,module,exports){ +},{}],11:[function(require,module,exports){ /*! * Chai - addMethod utility * Copyright(c) 2012-2014 Jake Luer @@ -7977,7 +7994,6 @@ module.exports = function getEnumerableProperties(object) { var flag = require('./flag') , getActual = require('./getActual') - , inspect = require('./inspect') , objDisplay = require('./objDisplay'); /** @@ -8017,7 +8033,7 @@ module.exports = function getMessage(obj, args) { return flagMsg ? flagMsg + ': ' + msg : msg; }; -},{"./flag":15,"./getActual":16,"./inspect":23,"./objDisplay":26}],19:[function(require,module,exports){ +},{"./flag":15,"./getActual":16,"./objDisplay":26}],19:[function(require,module,exports){ /*! * Chai - getOwnEnumerableProperties utility * Copyright(c) 2011-2016 Jake Luer @@ -8488,7 +8504,6 @@ function formatValue(ctx, value, recurseTimes) { return reduceToSingleString(output, base, braces); } - function formatPrimitive(ctx, value) { switch (typeof value) { case 'undefined': @@ -8518,12 +8533,10 @@ function formatPrimitive(ctx, value) { } } - function formatError(value) { return '[' + Error.prototype.toString.call(value) + ']'; } - function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { var output = []; for (var i = 0, l = value.length; i < l; ++i) { @@ -8626,12 +8639,8 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { return name + ': ' + str; } - function reduceToSingleString(output, base, braces) { - var numLinesEst = 0; var length = output.reduce(function(prev, cur) { - numLinesEst++; - if (cur.indexOf('\n') >= 0) numLinesEst++; return prev + cur.length + 1; }, 0); @@ -9154,10 +9163,12 @@ function stringDistance(strA, strB, memo) { if (strA.length === 0 || strB.length === 0) { memo[strA.length][strB.length] = Math.max(strA.length, strB.length); } else { + var sliceA = strA.slice(0, -1); + var sliceB = strB.slice(0, -1); memo[strA.length][strB.length] = Math.min( - stringDistance(strA.slice(0, -1), strB, memo) + 1, - stringDistance(strA, strB.slice(0, -1), memo) + 1, - stringDistance(strA.slice(0, -1), strB.slice(0, -1), memo) + + stringDistance(sliceA, strB, memo) + 1, + stringDistance(strA, sliceB, memo) + 1, + stringDistance(sliceA, sliceB, memo) + (strA.slice(-1) === strB.slice(-1) ? 0 : 1) ); } @@ -9554,7 +9565,7 @@ FakeMap.prototype = { return key[this._key]; }, set: function setMap(key, value) { - if (!Object.isFrozen(key)) { + if (Object.isExtensible(key)) { Object.defineProperty(key, this._key, { value: value, configurable: true, @@ -10704,4 +10715,4 @@ module.exports = function typeDetect(obj) { module.exports.typeDetect = module.exports; },{}]},{},[1])(1) -}); +}); \ No newline at end of file diff --git a/lib/chai/config.js b/lib/chai/config.js index ca1e30a79..412a0c806 100644 --- a/lib/chai/config.js +++ b/lib/chai/config.js @@ -90,5 +90,5 @@ module.exports = { * @api public */ - proxyExcludedKeys: ['then', 'inspect', 'toJSON'] + proxyExcludedKeys: ['then', 'catch', 'inspect', 'toJSON'] }; diff --git a/test/configuration.js b/test/configuration.js index 1f929d110..712731419 100644 --- a/test/configuration.js +++ b/test/configuration.js @@ -788,8 +788,8 @@ describe('configuration', function () { } }; - it('should have default value equal to `[\'then\', \'inspect\', \'toJSON\']`', function() { - expect(chai.config.proxyExcludedKeys).to.be.deep.equal(['then', 'inspect', 'toJSON']); + it('should have default value equal to `[\'then\', \'catch\', \'inspect\', \'toJSON\']`', function() { + expect(chai.config.proxyExcludedKeys).to.be.deep.equal(['then', 'catch', 'inspect', 'toJSON']); }); it('should not throw when accessing non-existing `then` and `inspect` in an environment with proxy support', function() {