mirror of
https://github.com/chaijs/chai
synced 2024-11-14 15:57:10 +00:00
Refactor global.err to test error properties
- Add feature https://github.com/chaijs/chai/issues/675 - Fix bug https://github.com/chaijs/chai/issues/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
This commit is contained in:
parent
9c89bf0b60
commit
ea2c7040ab
4 changed files with 142 additions and 30 deletions
|
@ -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
|
||||
if (typeof window === 'object') {
|
||||
global = window;
|
||||
} else {
|
||||
global.chai = require('../..');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.chai = require('../..');
|
||||
global.err = function (fn, val) {
|
||||
if (chai.util.type(fn) !== 'function')
|
||||
throw new chai.AssertionError('Invalid fn');
|
||||
|
||||
/*!
|
||||
* Provide check for fail function.
|
||||
*/
|
||||
|
||||
global.err = function (fn, msg) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
111
test/globalErr.js
Normal file
111
test/globalErr.js
Normal file
|
@ -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')
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue