deno.land / std@0.166.0 / node / assertion_error_test.ts

assertion_error_test.ts
نووسراو ببینە
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { stripColor } from "../fmt/colors.ts";import { assert, assertEquals, assertNotStrictEquals, assertStrictEquals,} from "../testing/asserts.ts";import { AssertionError, copyError, createErrDiff, inspectValue,} from "./assertion_error.ts";
Deno.test({ name: "copyError()", fn() { class TestError extends Error {} const err = new TestError("this is a test"); const copy = copyError(err);
assert(copy instanceof Error, "Copy should inherit from Error."); assert(copy instanceof TestError, "Copy should inherit from TestError."); assertEquals(copy, err, "Copy should be equal to the original error."); assertNotStrictEquals( copy, err, "Copy should not be strictly equal to the original error.", ); },});
Deno.test({ name: "inspectValue()", fn() { const obj = { a: 1, b: [2] }; Object.defineProperty(obj, "c", { value: 3, enumerable: false }); assertStrictEquals( stripColor(inspectValue(obj)), "{ a: 1, b: [ 2 ] }", ); },});
Deno.test({ name: "createErrDiff()", fn() { assertStrictEquals( stripColor( createErrDiff({ a: 1, b: 2 }, { a: 2, b: 2 }, "strictEqual"), ), stripColor( 'Expected "actual" to be reference-equal to "expected":' + "\n" + "+ actual - expected" + "\n" + "\n" + "+ { a: 1, b: 2 }" + "\n" + "- { a: 2, b: 2 }", ), ); },});
Deno.test({ name: "construct AssertionError() with given message", fn() { const err = new AssertionError( { message: "answer", actual: "42", expected: "42", operator: "notStrictEqual", }, ); assertStrictEquals(err.name, "AssertionError"); assertStrictEquals(err.message, "answer"); assertStrictEquals(err.generatedMessage, false); assertStrictEquals(err.code, "ERR_ASSERTION"); assertStrictEquals(err.actual, "42"); assertStrictEquals(err.expected, "42"); assertStrictEquals(err.operator, "notStrictEqual"); },});
Deno.test({ name: "construct AssertionError() with generated message", fn() { const err = new AssertionError( { actual: 1, expected: 2, operator: "equal" }, ); assertStrictEquals(err.name, "AssertionError"); assertStrictEquals(stripColor(err.message), "1 equal 2"); assertStrictEquals(err.generatedMessage, true); assertStrictEquals(err.code, "ERR_ASSERTION"); assertStrictEquals(err.actual, 1); assertStrictEquals(err.expected, 2); assertStrictEquals(err.operator, "equal"); },});
Deno.test({ name: "construct AssertionError() with stackStartFn", fn: function stackStartFn() { const expected = /node/; const err = new AssertionError({ actual: "deno", expected, operator: "match", stackStartFn, }); assertStrictEquals(err.name, "AssertionError"); assertStrictEquals(stripColor(err.message), `'deno' match /node/`); assertStrictEquals(err.generatedMessage, true); assertStrictEquals(err.code, "ERR_ASSERTION"); assertStrictEquals(err.actual, "deno"); assertStrictEquals(err.expected, expected); assertStrictEquals(err.operator, "match"); assert(err.stack, "error should have a stack"); assert( !err.stack?.includes("stackStartFn"), "stackStartFn() should not present in stack trace", ); },});
Deno.test({ name: "error details", fn() { const stack0 = new Error(); const stack1 = new Error(); const err = new AssertionError({ message: "Function(s) were not called the expected number of times", details: [ { message: "Expected the calls function to be executed 2 time(s) but was executed 3 time(s).", actual: 3, expected: 2, operator: "calls", stack: stack0, }, { message: "Expected the fn function to be executed 1 time(s) but was executed 0 time(s).", actual: 0, expected: 1, operator: "fn", stack: stack1, }, ], });
assertStrictEquals( err.message, "Function(s) were not called the expected number of times", );
assertStrictEquals( err["message 0"], "Expected the calls function to be executed 2 time(s) but was executed 3 time(s).", ); assertStrictEquals(err["actual 0"], 3); assertStrictEquals(err["expected 0"], 2); assertStrictEquals(err["operator 0"], "calls"); assertStrictEquals(err["stack trace 0"], stack0);
assertStrictEquals( err["message 1"], "Expected the fn function to be executed 1 time(s) but was executed 0 time(s).", ); assertStrictEquals(err["actual 1"], 0); assertStrictEquals(err["expected 1"], 1); assertStrictEquals(err["operator 1"], "fn"); assertStrictEquals(err["stack trace 1"], stack1); },});
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2447

Version Info

Tagged at
2 months ago