Skip to content

Instantly share code, notes, and snippets.

@RomkeVdMeulen
Last active August 16, 2017 16:50
Show Gist options
  • Save RomkeVdMeulen/a670e4cbb649edf36caa18e8859c2412 to your computer and use it in GitHub Desktop.
Save RomkeVdMeulen/a670e4cbb649edf36caa18e8859c2412 to your computer and use it in GitHub Desktop.
Custom matcher for Jasmine > 2.0 to verify if a given value is an instance of the expected type
/* Run this during setup */
beforeAll(() => {
jasmine.addMatchers({
toBeInstanceOf: function() {
return {
compare: function(actual: any, expected: {new (...args: any[]): any}) {
const pass = actual instanceof expected;
return {
pass,
message: `Expected instance of ${actual.constructor.name} ${pass ? 'not ' : ''}to be an instance of ${expected.name}`,
};
},
};
},
});
});
/* Declare the matcher so TypeScript doesn't complain */
declare module jasmine {
interface Matchers {
toBeInstanceOf(expected: {new (...args: any[]): any}): boolean;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment