Introduce matchers#toBeInstanceOf

This commit is contained in:
Elliot Nelson
2019-05-15 11:51:46 -04:00
parent 3dde56bbd8
commit 0fded24d35
3 changed files with 281 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
'toBe',
'toBeCloseTo',
'toBeDefined',
'toBeInstanceOf',
'toBeFalse',
'toBeFalsy',
'toBeGreaterThan',

View File

@@ -0,0 +1,45 @@
getJasmineRequireObj().toBeInstanceOf = function(j$) {
var usageError = j$.formatErrorMsg('<toBeInstanceOf>', 'expect(value).toBeInstanceOf(<ConstructorFunction>)');
/**
* {@link expect} the actual to be an instance of the expected class
* @function
* @name matchers#toBeInstanceOf
* @param {Object} expected - The class or constructor function to check for
* @example
* expect('foo').toBeInstanceOf(String);
* expect(3).toBeInstanceOf(Number);
* expect(new Error()).toBeInstanceOf(Error);
*/
function toBeInstanceOf(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var actualType = actual && actual.constructor ? j$.fnNameFor(actual.constructor) : j$.pp(actual),
expectedType = expected ? j$.fnNameFor(expected) : j$.pp(expected),
expectedMatcher,
pass;
try {
expectedMatcher = new j$.Any(expected);
pass = expectedMatcher.asymmetricMatch(actual);
} catch (error) {
throw new Error(usageError('Expected value is not a constructor function'));
}
if (pass) {
return {
pass: true,
message: 'Expected instance of ' + actualType + ' not to be an instance of ' + expectedType
};
} else {
return {
pass: false,
message: 'Expected instance of ' + actualType + ' to be an instance of ' + expectedType
};
}
}
};
}
return toBeInstanceOf;
};