Skip to content

Instantly share code, notes, and snippets.

@simonsmith
Last active August 29, 2015 14:00
Show Gist options
  • Save simonsmith/11209918 to your computer and use it in GitHub Desktop.
Save simonsmith/11209918 to your computer and use it in GitHub Desktop.
Jasmine type matchers (native and underscore/lodash)
/**
* 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);
});
}();
/**
* 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