( actual: unknown, expectedType: T, msg = "", )
| 26 | * @param msg The optional message to display if the assertion fails. |
| 27 | */ |
| 28 | export function assertInstanceOf< |
| 29 | // deno-lint-ignore no-explicit-any |
| 30 | T extends abstract new (...args: any[]) => any, |
| 31 | >( |
| 32 | actual: unknown, |
| 33 | expectedType: T, |
| 34 | msg = "", |
| 35 | ): asserts actual is InstanceType<T> { |
| 36 | if (actual instanceof expectedType) return; |
| 37 | |
| 38 | const msgSuffix = msg ? `: ${msg}` : "."; |
| 39 | const expectedTypeStr = expectedType.name; |
| 40 | |
| 41 | let actualTypeStr = ""; |
| 42 | if (actual === null) { |
| 43 | actualTypeStr = "null"; |
| 44 | } else if (actual === undefined) { |
| 45 | actualTypeStr = "undefined"; |
| 46 | } else if (typeof actual === "object") { |
| 47 | actualTypeStr = actual.constructor?.name ?? "Object"; |
| 48 | } else { |
| 49 | actualTypeStr = typeof actual; |
| 50 | } |
| 51 | |
| 52 | if (expectedTypeStr === actualTypeStr) { |
| 53 | msg = |
| 54 | `Expected object to be an instance of "${expectedTypeStr}"${msgSuffix}`; |
| 55 | } else if (actualTypeStr === "function") { |
| 56 | msg = |
| 57 | `Expected object to be an instance of "${expectedTypeStr}" but was not an instanced object${msgSuffix}`; |
| 58 | } else { |
| 59 | msg = |
| 60 | `Expected object to be an instance of "${expectedTypeStr}" but was "${actualTypeStr}"${msgSuffix}`; |
| 61 | } |
| 62 | |
| 63 | throw new AssertionError(msg); |
| 64 | } |
no outgoing calls
no test coverage detected