MCPcopy Create free account
hub / github.com/denoland/std / assertInstanceOf

Function assertInstanceOf

assert/instance_of.ts:28–64  ·  view source on GitHub ↗
(
  actual: unknown,
  expectedType: T,
  msg = "",
)

Source from the content-addressed store, hash-verified

26 * @param msg The optional message to display if the assertion fails.
27 */
28export 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}

Callers 13

crypto_test.tsFile · 0.90
channel_test.tsFile · 0.90
types_test.tsFile · 0.90
parse_test.tsFile · 0.90
fnFunction · 0.90
typeScriptTestsFunction · 0.90
testFailedAssertionFunction · 0.90
snapshot_test.tsFile · 0.90
time_test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected