deno.land / std@0.107.0 / testing

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Testing
This module provides a few basic utilities to make testing easier and consistentin Deno.
## Usage
`testing/asserts.ts` module provides range of assertion helpers. If theassertion is false an `AssertionError` will be thrown which will result inpretty-printed diff of failing assertion.
- `equal()` - Deep comparison function, where `actual` and `expected` are compared deeply, and if they vary, `equal` returns `false`.- `assert()` - Expects a boolean value, throws if the value is `false`.- `assertEquals()` - Uses the `equal` comparison and throws if the `actual` and `expected` are not equal.- `assertNotEquals()` - Uses the `equal` comparison and throws if the `actual` and `expected` are equal.- `assertStrictEquals()` - Compares `actual` and `expected` strictly, therefore for non-primitives the values must reference the same instance.- `assertStringIncludes()` - Make an assertion that `actual` includes `expected`.- `assertMatch()` - Make an assertion that `actual` match RegExp `expected`.- `assertNotMatch()` - Make an assertion that `actual` not match RegExp `expected`.- `assertArrayIncludes()` - Make an assertion that `actual` array includes the `expected` values.- `assertObjectMatch()` - Make an assertion that `actual` object match `expected` subset object- `assertThrows()` - Expects the passed `fn` to throw. If `fn` does not throw, this function does. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string.- `assertRejects()` - Expects the passed `fn` to be async and throw (or return a `Promise` that rejects). If the `fn` does not throw or reject, this function will throw asynchronously _(⚠️ assertion should be awaited or be the return value of the test function to avoid any uncaught rejections which could result in unexpected process exit)_. Also compares any errors thrown to an optional expected `Error` class and checks that the error `.message` includes an optional string.- `assertThrowsAsync()` - Deprecated. Use `assertRejects`.- `unimplemented()` - Use this to stub out methods that will throw when invoked.- `unreachable()` - Used to assert unreachable code.
Basic usage:
```tsimport { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test({ name: "testing example", fn(): void { assertEquals("world", "world"); assertEquals({ hello: "world" }, { hello: "world" }); },});```
Short syntax (named function instead of object):
```tsimport { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test("example", function (): void { assertEquals("world", "world"); assertEquals({ hello: "world" }, { hello: "world" });});```
Using `assertStrictEquals()`:
```tsimport { assertStrictEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test("isStrictlyEqual", function (): void { const a = {}; const b = a; assertStrictEquals(a, b);});
// This test failsDeno.test("isNotStrictlyEqual", function (): void { const a = {}; const b = {}; assertStrictEquals(a, b);});```
Using `assertThrows()`:
```tsimport { assertThrows } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test("doesThrow", function (): void { assertThrows((): void => { throw new TypeError("hello world!"); }); assertThrows((): void => { throw new TypeError("hello world!"); }, TypeError); assertThrows( (): void => { throw new TypeError("hello world!"); }, TypeError, "hello", );});
// This test will not pass.Deno.test("fails", function (): void { assertThrows((): void => { console.log("Hello world"); });});```
Using `assertThrowsAsync()`:
```tsimport { assertThrowsAsync } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";
Deno.test("doesThrow", async function () { await assertThrowsAsync( async () => { throw new TypeError("hello world!"); }, ); await assertThrowsAsync(async () => { throw new TypeError("hello world!"); }, TypeError); await assertThrowsAsync( async () => { throw new TypeError("hello world!"); }, TypeError, "hello", ); await assertThrowsAsync( async () => { return Promise.reject(new Error()); }, );});
// This test will not pass.Deno.test("fails", async function () { await assertThrowsAsync( async () => { console.log("Hello world"); }, );});```
## Benching
With this module you can benchmark your code and get information on how is itperforming.
### Basic usage:
Benchmarks can be registered using the `bench` function, where you can define acode, that should be benchmarked. `b.start()` has to be called at the start ofthe part you want to benchmark and `b.stop()` at the end of it, otherwise anerror will be thrown.
After that simply calling `runBenchmarks()` will benchmark all registeredbenchmarks and log the results in the commandline.
```tsimport { bench, runBenchmarks,} from "https://deno.land/std@$STD_VERSION/testing/bench.ts";
bench(function forIncrementX1e9(b): void { b.start(); for (let i = 0; i < 1e9; i++); b.stop();});
runBenchmarks();```
Averaging execution time over multiple runs:
```tsimport { bench } from "https://deno.land/std@$STD_VERSION/testing/bench.ts";
bench({ name: "runs100ForIncrementX1e6", runs: 100, func(b): void { b.start(); for (let i = 0; i < 1e6; i++); b.stop(); },});```
Running specific benchmarks using regular expressions:
```tsimport { runBenchmarks,} from "https://deno.land/std@$STD_VERSION/testing/bench.ts";
runBenchmarks({ only: /desired/, skip: /exceptions/ });```
### Processing benchmark results
`runBenchmarks()` returns a `Promise<BenchmarkRunResult>`, so you can processthe benchmarking results yourself. It contains detailed results of eachbenchmark's run as `BenchmarkResult` s.
```tsimport { BenchmarkRunResult, runBenchmarks,} from "https://deno.land/std@$STD_VERSION/testing/bench.ts";
runBenchmarks() .then((results: BenchmarkRunResult) => { console.log(results); }) .catch((error: Error) => { // ... errors if benchmark was badly constructed. });```
### Processing benchmarking progress
`runBenchmarks()` accepts an optional progress handler callback function, so youcan get information on the progress of the running benchmarking.
Using `{ silent: true }` means you wont see the default progression logs in thecommandline.
```tsimport { BenchmarkRunProgress, ProgressState, runBenchmarks,} from "https://deno.land/std@$STD_VERSION/testing/bench.ts";
runBenchmarks({ silent: true }, (p: BenchmarkRunProgress) => { // initial progress data. if (p.state === ProgressState.BenchmarkingStart) { console.log( `Starting benchmarking. Queued: ${ p.queued!.length }, filtered: ${p.filtered}`, ); } // ...});```
#### Benching API
##### `bench(benchmark: BenchmarkDefinition | BenchmarkFunction): void`
Registers a benchmark that will be run once `runBenchmarks` is called.
##### `runBenchmarks(opts?: BenchmarkRunOptions, progressCb?: (p: BenchmarkRunProgress) => void | Promise<void>): Promise<BenchmarkRunResult>`
Runs all registered benchmarks serially. Filtering can be applied by setting`BenchmarkRunOptions.only` and/or `BenchmarkRunOptions.skip` to regularexpressions matching benchmark names. Default progression logs can be turned offwith the `BenchmarkRunOptions.silent` flag.
##### `clearBenchmarks(opts?: BenchmarkClearOptions): void`
Clears all registered benchmarks, so calling `runBenchmarks()` after it wont runthem. Filtering can be applied by setting `BenchmarkRunOptions.only` and/or`BenchmarkRunOptions.skip` to regular expressions matching benchmark names.
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2121

Version Info

Tagged at
8 months ago