Files
jasmine/src/core/matchers/toBeInstanceOf.js
Steve Gravrock 5ff7e7f9a1 Updated to eslint 9
This isn't officially compatible with the oldest version of Node that
Jasmine supports, but it works. If it stops working, we can always disable
linting in CI builds on older Node versions.
2025-04-07 21:39:58 -07:00

66 lines
1.8 KiB
JavaScript

getJasmineRequireObj().toBeInstanceOf = function(j$) {
const usageError = j$.formatErrorMsg(
'<toBeInstanceOf>',
'expect(value).toBeInstanceOf(<ConstructorFunction>)'
);
/**
* {@link expect} the actual to be an instance of the expected class
* @function
* @name matchers#toBeInstanceOf
* @since 3.5.0
* @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(matchersUtil) {
return {
compare: function(actual, expected) {
const actualType =
actual && actual.constructor
? j$.fnNameFor(actual.constructor)
: matchersUtil.pp(actual);
const expectedType = expected
? j$.fnNameFor(expected)
: matchersUtil.pp(expected);
let expectedMatcher;
let pass;
try {
expectedMatcher = new j$.Any(expected);
pass = expectedMatcher.asymmetricMatch(actual);
// eslint-disable-next-line no-unused-vars
} 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;
};