Last active
August 16, 2017 16:50
-
-
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
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
/* 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