forked from chaijs/chai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chaijs#677 from meeber/test-expected-actual
Refactor global.err to test error properties
- Loading branch information
Showing
4 changed files
with
142 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}); | ||
}); | ||
}); |