Permalink
Browse files

Refactor global.err to test error properties

- Add feature #675
- Fix bug #648
- Allow object overload of global.err's second argument
- Combine global.err and window.err into one file and function
- Add tests for global.err
  • Loading branch information...
1 parent 9c89bf0 commit ea2c7040aba6f5123c94893eb61bd0b54156c676 @meeber meeber committed Apr 12, 2016
Showing with 142 additions and 30 deletions.
  1. +1 −1 karma.conf.js
  2. +30 −13 test/bootstrap/index.js
  3. +0 −16 test/bootstrap/karma.js
  4. +111 −0 test/globalErr.js
View
@@ -3,7 +3,7 @@ module.exports = function(config) {
frameworks: [ 'mocha' ]
, files: [
'chai.js'
- , 'test/bootstrap/karma.js'
+ , 'test/bootstrap/index.js'
, 'test/*.js'
]
, reporters: [ 'progress' ]
@@ -1,22 +1,39 @@
-/*!
- * Attach chai to global
- */
-
-global.chai = require('../..');
+if (typeof window === 'object') {
+ global = window;
+} else {
+ global.chai = require('../..');
+}
-/*!
- * Provide check for fail function.
+/**
+ * Validate that the given function throws an error. Optionally validate some
+ * additional properties of the error:
+ *
+ * If val is a string, validate val equals the error's .message
+ * If val is a regex, validate val matches the error's .message
+ * If val is an object, validate val's props are included in the error object
+ *
+ * @param {Function} function that's expected to throw an error
+ * @param {Mixed} expected properties of the expected error
*/
-global.err = function (fn, msg) {
+global.err = function (fn, val) {
+ if (chai.util.type(fn) !== 'function')
+ throw new chai.AssertionError('Invalid fn');
+
try {
fn();
- throw new chai.AssertionError({ message: 'Expected an error' });
} catch (err) {
- if ('string' === typeof msg) {
- chai.expect(err.message).to.equal(msg);
- } else {
- chai.expect(err.message).to.match(msg);
+ switch (chai.util.type(val)) {
+ case 'undefined': return;
+ case 'string': return chai.expect(err.message).to.equal(val);
+ case 'regexp': return chai.expect(err.message).to.match(val);
+ case 'object': return Object.keys(val).forEach(function (key) {
+ chai.expect(err).to.have.property(key).and.to.deep.equal(val[key]);
+ });
}
+
+ throw new chai.AssertionError('Invalid val');
}
+
+ throw new chai.AssertionError('Expected an error');
};
@@ -1,16 +0,0 @@
-/*!
- * Provide check for fail function.
- */
-
-window.err = function (fn, msg) {
- try {
- fn();
- throw new chai.AssertionError('Expected an error');
- } catch (err) {
- if ('string' === typeof msg) {
- chai.expect(err.message).to.equal(msg);
- } else {
- chai.expect(err.message).to.match(msg);
- }
- }
-};
View
@@ -0,0 +1,111 @@
+describe('globalErr', function () {
+ var noop = function () {}
+ , Err = chai.AssertionError
+ , expect = chai.expect;
+
+ it('should pass if string val equals error message', function () {
+ err(function () {
+ expect('cat').to.equal('dog')
+ }, 'expected \'cat\' to equal \'dog\'');
+ });
+
+ it('should pass if regex val matches error message', function () {
+ err(function () {
+ expect('cat').to.equal('dog')
+ }, /expected 'cat' to equal 'dog'/);
+ });
+
+ it('should pass if object val\'s props are included in error object', function () {
+ err(function () {
+ expect('cat').to.equal('dog');
+ }, {
+ message: 'expected \'cat\' to equal \'dog\''
+ , expected: 'dog'
+ , actual: 'cat'
+ });
+
+ err(function () {
+ expect({cat: 'meow'}).to.equal({dog: 'woof'});
+ }, {
+ message: 'expected { cat: \'meow\' } to equal { dog: \'woof\' }'
+ , expected: {dog: 'woof'}
+ , actual: {cat: 'meow'}
+ });
+ });
+
+ it('should throw if string val does not equal error message', function () {
+ err(function () {
+ err(function () { throw new Err('cat') }, 'dog');
+ }, {
+ message: 'expected \'cat\' to equal \'dog\''
+ , expected: 'dog'
+ , actual: 'cat'
+ });
+ });
+
+ it('should throw if regex val does not match error message', function () {
+ err(function () {
+ err(function () { throw new Err('cat') }, /dog/);
+ }, 'expected \'cat\' to match /dog/');
+ });
+
+ it('should throw if object val\'s props are not included in error object', function () {
+ err(function () {
+ err(function () { throw new Err('cat') }, {text: 'cat'});
+ }, /expected { Object \(message, showDiff(, \.\.\.)*\) } to have a property \'text\'/);
+
+ err(function () {
+ err(function () { throw new Err('cat') }, {message: 'dog'});
+ }, 'expected \'cat\' to deeply equal \'dog\'');
+ });
+
+ it('should throw if fn does not throw', function () {
+ err(function () { err(noop) }, 'Expected an error');
+ });
+
+ it('should throw if fn is invalid', function () {
+ var vals = [
+ 'cat'
+ , 42
+ , []
+ , new RegExp()
+ , new Date()
+ , null
+ , undefined
+ ];
+
+ if (typeof Symbol === 'function') vals.push(Symbol());
+ if (typeof Map === 'function') vals.push(new Map());
+ if (typeof WeakMap === 'function') vals.push(new WeakMap());
+ if (typeof Set === 'function') vals.push(new Set());
+ if (typeof WeakSet === 'function') vals.push(new WeakSet());
+ if (typeof Promise === 'function') vals.push(new Promise(noop));
+
+ vals.forEach(function (val) {
+ err(function () { err(val) }, 'Invalid fn')
+ });
+ });
+
+ it('should throw if val is invalid', function () {
+ var vals = [
+ 42
+ , []
+ , new Date()
+ , noop
+ , null
+ ];
+
+ if (typeof Symbol === 'function') vals.push(Symbol());
+ if (typeof Map === 'function') vals.push(new Map());
+ if (typeof WeakMap === 'function') vals.push(new WeakMap());
+ if (typeof Set === 'function') vals.push(new Set());
+ if (typeof WeakSet === 'function') vals.push(new WeakSet());
+ if (typeof Promise === 'function') vals.push(new Promise(noop));
+
+ vals.forEach(function (val) {
+ err(function () {
+ err(function () { throw new Err('Test error') }, val)
+ }, 'Invalid val')
+ });
+ });
+});

0 comments on commit ea2c704

Please sign in to comment.