deno.land / std@0.224.0 / assert / assert_instance_of_test.ts

assert_instance_of_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertInstanceOf, AssertionError, assertThrows } from "./mod.ts";
Deno.test({ name: "assertInstanceOf", fn() { class TestClass1 {} class TestClass2 {} class TestClass3 {}
// Regular types assertInstanceOf(new Date(), Date); assertInstanceOf(new Number(), Number); assertInstanceOf(Promise.resolve(), Promise); assertInstanceOf(new TestClass1(), TestClass1);
// Throwing cases assertThrows( () => assertInstanceOf(new Date(), RegExp), AssertionError, `Expected object to be an instance of "RegExp" but was "Date".`, ); assertThrows( () => assertInstanceOf(5, Date), AssertionError, `Expected object to be an instance of "Date" but was "number".`, ); assertThrows( () => assertInstanceOf(new TestClass1(), TestClass2), AssertionError, `Expected object to be an instance of "TestClass2" but was "TestClass1".`, );
// Custom message assertThrows( () => assertInstanceOf(new Date(), RegExp, "Custom message"), AssertionError, "Custom message", );
// Edge cases assertThrows( () => assertInstanceOf(5, Number), AssertionError, `Expected object to be an instance of "Number" but was "number".`, );
let TestClassWithSameName: new () => unknown; { class TestClass3 {} TestClassWithSameName = TestClass3; } assertThrows( () => assertInstanceOf(new TestClassWithSameName(), TestClass3), AssertionError, `Expected object to be an instance of "TestClass3".`, );
assertThrows( () => assertInstanceOf(TestClass1, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was not an instanced object.`, ); assertThrows( () => assertInstanceOf(() => {}, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was not an instanced object.`, ); assertThrows( () => assertInstanceOf(null, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "null".`, ); assertThrows( () => assertInstanceOf(undefined, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "undefined".`, ); assertThrows( () => assertInstanceOf({}, TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "Object".`, ); assertThrows( () => assertInstanceOf(Object.create(null), TestClass1), AssertionError, `Expected object to be an instance of "TestClass1" but was "Object".`, );
// Test TypeScript types functionality, wrapped in a function that never runs // deno-lint-ignore no-unused-vars function typeScriptTests() { class ClassWithProperty { property = "prop1"; } const testInstance = new ClassWithProperty() as unknown;
// @ts-expect-error: `testInstance` is `unknown` so setting its property before `assertInstanceOf` should give a type error. testInstance.property = "prop2";
assertInstanceOf(testInstance, ClassWithProperty);
// Now `testInstance` should be of type `ClassWithProperty` testInstance.property = "prop3";
let x = 5 as unknown;
// @ts-expect-error: `x` is `unknown` so adding to it shouldn't work x += 5; assertInstanceOf(x, Number);
// @ts-expect-error: `x` is now `Number` rather than `number`, so this should still give a type error. x += 5; } },});
std

Version Info

Tagged at
6 months ago