Last active
August 29, 2015 14:00
-
-
Save simonsmith/11209918 to your computer and use it in GitHub Desktop.
Jasmine type matchers (native and underscore/lodash)
This file contains hidden or 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
/** | |
* Add matchers for different types | |
* Note that Function.prototype.bind is not supported in PhantomJS | |
* Uses underscore/lodash | |
* | |
* Usage: expect('test').toBeString(); | |
*/ | |
!function() { | |
'use strict'; | |
function typeOf(obj) { | |
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
} | |
function compare(expected, actual) { | |
return { | |
pass: typeOf(actual) === expected | |
}; | |
} | |
var toBeType = {}; | |
_.each([ | |
'String', | |
'Function', | |
'RegExp', | |
'Math', | |
'Number', | |
'Date', | |
'Error', | |
'Array', | |
'Boolean', | |
'Object' | |
], function(name) { | |
toBeType['toBe' + name] = function() { | |
return { | |
compare: _.bind(compare, this, name.toLowerCase()) | |
}; | |
}; | |
}); | |
beforeEach(function() { | |
jasmine.addMatchers(toBeType); | |
}); | |
}(); |
This file contains hidden or 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
/** | |
* Add matchers for different types | |
* Note that Function.prototype.bind is not supported in PhantomJS | |
* | |
* Usage: expect('test').toBeString(); | |
*/ | |
!function() { | |
'use strict'; | |
function typeOf(obj) { | |
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); | |
} | |
function compare(expected, actual) { | |
return { | |
pass: typeOf(actual) === expected | |
}; | |
} | |
var toBeType = {}; | |
[ | |
'String', | |
'Function', | |
'RegExp', | |
'Math', | |
'Number', | |
'Date', | |
'Error', | |
'Array', | |
'Boolean', | |
'Object' | |
].forEach(function(name) { | |
toBeType['toBe' + name] = function() { | |
return { | |
compare: compare.bind(this, name.toLowerCase()) | |
}; | |
}; | |
}); | |
beforeEach(function() { | |
jasmine.addMatchers(toBeType); | |
}); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment