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

Function fn

assert/instance_of_test.ts:6–115  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4Deno.test({
5 name: "assertInstanceOf",
6 fn() {
7 class TestClass1 {}
8 class TestClass2 {}
9 class TestClass3 {}
10
11 // Regular types
12 assertInstanceOf(new Date(), Date);
13 assertInstanceOf(new Number(), Number);
14 assertInstanceOf(Promise.resolve(), Promise);
15 assertInstanceOf(new TestClass1(), TestClass1);
16
17 // Throwing cases
18 assertThrows(
19 () => assertInstanceOf(new Date(), RegExp),
20 AssertionError,
21 `Expected object to be an instance of "RegExp" but was "Date".`,
22 );
23 assertThrows(
24 () => assertInstanceOf(5, Date),
25 AssertionError,
26 `Expected object to be an instance of "Date" but was "number".`,
27 );
28 assertThrows(
29 () => assertInstanceOf(new TestClass1(), TestClass2),
30 AssertionError,
31 `Expected object to be an instance of "TestClass2" but was "TestClass1".`,
32 );
33
34 // Custom message
35 assertThrows(
36 () => assertInstanceOf(new Date(), RegExp, "CUSTOM MESSAGE"),
37 AssertionError,
38 "CUSTOM MESSAGE",
39 );
40
41 // Edge cases
42 assertThrows(
43 () => assertInstanceOf(5, Number),
44 AssertionError,
45 `Expected object to be an instance of "Number" but was "number".`,
46 );
47
48 let TestClassWithSameName: new () => unknown;
49 {
50 class TestClass3 {}
51 TestClassWithSameName = TestClass3;
52 }
53 assertThrows(
54 () => assertInstanceOf(new TestClassWithSameName(), TestClass3),
55 AssertionError,
56 `Expected object to be an instance of "TestClass3".`,
57 );
58
59 assertThrows(
60 () => assertInstanceOf(TestClass1, TestClass1),
61 AssertionError,
62 `Expected object to be an instance of "TestClass1" but was not an instanced object.`,
63 );

Callers

nothing calls this directly

Calls 2

assertInstanceOfFunction · 0.90
assertThrowsFunction · 0.90

Tested by

no test coverage detected