Last active
August 29, 2015 14:04
-
-
Save Xiphe/8549144a6969317efa11 to your computer and use it in GitHub Desktop.
Moar Jasmine 2.0 matchers
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
{ | |
"name": "jasmine-moar-matchers", | |
"authors": [ | |
"Hannes Diercks" | |
], | |
"description": "Some additional Jasmine 2.0 Matchers.", | |
"main": ["toBeInstanceOf.js", "toBeTypeOf.js", "promises.js"], | |
"keywords": [ | |
"jasmine", | |
"matcher" | |
], | |
"license": "MIT", | |
"ignore": [] | |
} |
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
/* globals jasmine */ | |
beforeEach(function() { | |
'use strict'; | |
jasmine.addMatchers({ | |
toBeAPromise: function() { | |
return { | |
compare: function(actual) { | |
var pass = actual.then instanceof Function; | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'a promise.' | |
}; | |
} | |
}; | |
}, | |
toBeAFullPromise: function() { | |
return { | |
compare: function(actual) { | |
var pass = ( | |
actual.then instanceof Function && | |
actual['catch'] instanceof Function && | |
actual['finally'] instanceof Function | |
); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'a full promise.' | |
}; | |
} | |
}; | |
}, | |
toBeAQPromise: function() { | |
return { | |
compare: function(actual) { | |
var pass = ( | |
actual.then instanceof Function && | |
actual['catch'] instanceof Function && | |
actual.progress instanceof Function && | |
actual['finally'] instanceof Function && | |
actual.done instanceof Function | |
); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'a promise matching the Q API.' | |
}; | |
} | |
}; | |
}, | |
toBeADeferred: function() { | |
return { | |
compare: function(actual) { | |
var pass = ( | |
actual.resolve instanceof Function && | |
actual.reject instanceof Function | |
); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'a deferred object.' | |
}; | |
} | |
}; | |
}, | |
toBeAQDeferred: function() { | |
return { | |
compare: function(actual) { | |
var pass = ( | |
actual.promise instanceof Object && | |
actual.resolve instanceof Function && | |
actual.notify instanceof Function && | |
actual.reject instanceof Function | |
); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'a deferred object matching the Q API.' | |
}; | |
} | |
}; | |
}, | |
toBeRejected: function() { | |
return { | |
compare: function(actual) { | |
if (typeof actual.isRejected !== 'function') { | |
throw new Error('Cant check if ' + jasmine.pp(actual) + ' is rejected due to missing isRejected method.'); | |
} | |
var pass = actual.isRejected(); | |
return { | |
pass: pass, | |
message: 'Promise has ' + (!pass ? 'not ' : '') + 'been rejected.' | |
}; | |
} | |
}; | |
}, | |
toBeResolved: function() { | |
return { | |
compare: function(actual) { | |
if (typeof actual.isResolved !== 'function') { | |
throw new Error('Cant check if ' + jasmine.pp(actual) + ' is resolved due to missing isResolved method.'); | |
} | |
var pass = actual.isResolved(); | |
return { | |
pass: pass, | |
message: 'Promise has ' + (!pass ? 'not ' : '') + 'been resolved.' | |
}; | |
} | |
}; | |
}, | |
toBePending: function() { | |
return { | |
compare: function(actual) { | |
if (typeof actual.isPending !== 'function') { | |
throw new Error('Cant check if ' + jasmine.pp(actual) + ' is pending due to missing isPending method.'); | |
} | |
var pass = actual.isPending(); | |
return { | |
pass: pass, | |
message: 'Promise has ' + (!pass ? 'not ' : '') + 'been pending.' | |
}; | |
} | |
}; | |
}, | |
// TODO: toBeResolvedWith: | |
// TODO: toBeRejectedWith: | |
}); | |
}); |
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
/* globals jasmine, beforeEach */ | |
(function(angular) { | |
'use strict'; | |
beforeEach(function() { | |
jasmine.addMatchers({ | |
toAngularEqual: function() { | |
return { | |
compare: function(actual, expected) { | |
if (!angular || typeof angular.equals === 'undefined') { | |
return { | |
pass: false, | |
message: 'Could not check equality with angular, since angular does not exist' | |
}; | |
} | |
var pass = angular.equals(actual, expected); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' does ' + (!pass ? 'not ' : '') + ' angular-equal ' + jasmine.pp(expected) | |
}; | |
} | |
}; | |
} | |
}); | |
}); | |
})(window.angular); |
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
/* globals jasmine */ | |
beforeEach(function() { | |
'use strict'; | |
jasmine.addMatchers({ | |
toBeInstanceOf: function() { | |
return { | |
compare: function(actual, expected) { | |
var pass = actual instanceof expected; | |
var name; | |
try { | |
name = actual.constructor.name; | |
} catch (e) { | |
name = typeof actual; | |
} | |
return { | |
pass: pass, | |
message: name + ' is ' + (!pass ? 'not ' : '') + 'an instance of ' + expected.name | |
}; | |
} | |
}; | |
} | |
}); | |
}); |
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
/* globals jasmine */ | |
beforeEach(function() { | |
'use strict'; | |
jasmine.addMatchers({ | |
toBeTypeOf: function() { | |
return { | |
compare: function(actual, expected) { | |
if (typeof expected !== 'string') { | |
expected = expected.constructor.name; | |
} | |
var pass = typeof actual === expected.toLowerCase(); | |
return { | |
pass: pass, | |
message: jasmine.pp(actual) + ' is ' + (!pass ? 'not ' : '') + 'of type ' + expected | |
}; | |
} | |
}; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment