From 3048c06f003d2fce0e25bd25cc385734273ec05a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 17 Mar 2015 21:57:54 -0400 Subject: [PATCH 1/2] Assert ifError now throws the error passed in, to keep it consistent with node's assert.ifError --- lib/chai/interface/assert.js | 19 ++++++++++++++++--- test/assert.js | 5 +++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/chai/interface/assert.js b/lib/chai/interface/assert.js index a4f478b4d..56a688bbb 100644 --- a/lib/chai/interface/assert.js +++ b/lib/chai/interface/assert.js @@ -1243,11 +1243,24 @@ module.exports = function (chai, util) { } /*! - * Undocumented / untested + * ### .ifError(object) + * + * Asserts if value is not a false value, and throws if it is a true value. + * This is added to allow for chai to be a drop-in replacement for Node's + * assert class. + * + * var err = new Error('I am a custom error'); + * assert.ifError(err); // Rethrows err! + * + * @name ifError + * @param {Object} object + * @api public */ - assert.ifError = function (val, msg) { - new Assertion(val, msg).to.not.be.ok; + assert.ifError = function (val) { + if (val) { + throw(val); + } }; /*! diff --git a/test/assert.js b/test/assert.js index 5ae3e477b..66c239627 100644 --- a/test/assert.js +++ b/test/assert.js @@ -558,8 +558,9 @@ describe('assert', function () { assert.ifError(undefined); err(function () { - assert.ifError('foo'); - }, "expected \'foo\' to be falsy"); + var err = new Error('This is an error message'); + assert.ifError(err); + }, 'This is an error message'); }); it('operator', function() { From 81b88f869be3fef42ef8c5890c0434367a74326f Mon Sep 17 00:00:00 2001 From: David Losert Date: Sat, 4 Apr 2015 21:58:07 +0200 Subject: [PATCH 2/2] Replaced type.js with the "chaijs/type-detect" library. --- lib/chai/core/assertions.js | 8 ++++++- lib/chai/utils/hasProperty.js | 2 +- lib/chai/utils/index.js | 2 +- lib/chai/utils/type.js | 45 ----------------------------------- package.json | 3 ++- 5 files changed, 11 insertions(+), 49 deletions(-) delete mode 100644 lib/chai/utils/type.js diff --git a/lib/chai/core/assertions.js b/lib/chai/core/assertions.js index 2ead53f69..ce1b3bf2d 100644 --- a/lib/chai/core/assertions.js +++ b/lib/chai/core/assertions.js @@ -136,6 +136,12 @@ module.exports = function (chai, _) { * expect({ foo: 'bar' }).to.be.an('object'); * expect(null).to.be.a('null'); * expect(undefined).to.be.an('undefined'); + * expect(new Promise).to.be.a('promise'); + * expect(new Float32Array()).to.be.a('float32array'); + * expect(Symbol()).to.be.a('symbol'); + * + * // es6 overrides + * expect({[Symbol.toStringTag]:()=>'foo'}).to.be.a('foo'); * * // language chain * expect(foo).to.be.an.instanceof(Foo); @@ -1269,7 +1275,7 @@ module.exports = function (chai, _) { } // next, check message - var message = 'object' === _.type(err) && "message" in err + var message = 'error' === _.type(err) && "message" in err ? err.message : '' + err; diff --git a/lib/chai/utils/hasProperty.js b/lib/chai/utils/hasProperty.js index 6ebea84b5..364f68c04 100644 --- a/lib/chai/utils/hasProperty.js +++ b/lib/chai/utils/hasProperty.js @@ -4,7 +4,7 @@ * MIT Licensed */ -var type = require('./type'); +var type = require('type-detect'); /** * ### .hasProperty(object, name) diff --git a/lib/chai/utils/index.js b/lib/chai/utils/index.js index 4ed87ed46..f5d5a771d 100644 --- a/lib/chai/utils/index.js +++ b/lib/chai/utils/index.js @@ -20,7 +20,7 @@ exports.test = require('./test'); * type utility */ -exports.type = require('./type'); +exports.type = require('type-detect'); /*! * message utility diff --git a/lib/chai/utils/type.js b/lib/chai/utils/type.js deleted file mode 100644 index 99392d0ad..000000000 --- a/lib/chai/utils/type.js +++ /dev/null @@ -1,45 +0,0 @@ -/*! - * Chai - type utility - * Copyright(c) 2012-2014 Jake Luer - * MIT Licensed - */ - -/*! - * Detectable javascript natives - */ - -var natives = { - '[object Arguments]': 'arguments' - , '[object Array]': 'array' - , '[object Date]': 'date' - , '[object Function]': 'function' - , '[object Number]': 'number' - , '[object RegExp]': 'regexp' - , '[object String]': 'string' -}; - -/** - * ### type(object) - * - * Better implementation of `typeof` detection that can - * be used cross-browser. Handles the inconsistencies of - * Array, `null`, and `undefined` detection. - * - * utils.type({}) // 'object' - * utils.type(null) // `null' - * utils.type(undefined) // `undefined` - * utils.type([]) // `array` - * - * @param {Mixed} object to detect type of - * @name type - * @api private - */ - -module.exports = function (obj) { - var str = Object.prototype.toString.call(obj); - if (natives[str]) return natives[str]; - if (obj === null) return 'null'; - if (obj === undefined) return 'undefined'; - if (obj === Object(obj)) return 'object'; - return typeof obj; -}; diff --git a/package.json b/package.json index 70cf309e3..bd8f8fb8f 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ }, "dependencies": { "assertion-error": "1.0.1", - "deep-eql": "0.1.3" + "deep-eql": "0.1.3", + "type-detect": "1.0.0" }, "devDependencies": { "browserify": "10.2.1",