Skip to content

Instantly share code, notes, and snippets.

@joscha
Forked from Xiphe/bower.json
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joscha/fd31c523d8c47ef83c92 to your computer and use it in GitHub Desktop.
Save joscha/fd31c523d8c47ef83c92 to your computer and use it in GitHub Desktop.
{
"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": []
}
/* 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:
});
});
/* 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
};
}
};
}
});
});
/* 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