deno.land / std@0.224.0 / testing / asserts.ts

asserts.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/** * A library of assertion functions. * If the assertion is false an `AssertionError` will be thrown which will * result in pretty-printed diff of failing assertion. * * This module is browser compatible, but do not rely on good formatting of * values for AssertionError messages in browsers. * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/mod.ts} instead. * * @module */import * as asserts from "../assert/mod.ts";
/** * Make an assertion that `actual` and `expected` are almost equal numbers * through a given tolerance. It can be used to take into account IEEE-754 * double-precision floating-point representation limitations. If the values * are not almost equal then throw. * * @example * ```ts * import { assertAlmostEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw * assertAlmostEquals(0.01, 0.02); // Throws * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw * assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_almost_equals.ts} instead. */export function assertAlmostEquals( actual: number, expected: number, tolerance = 1e-7, msg?: string,) { asserts.assertAlmostEquals(actual, expected, tolerance, msg);}
/** * An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string. * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_array_includes.ts} instead. */export type ArrayLikeArg<T> = ArrayLike<T> & object;
/** * Make an assertion that `actual` includes the `expected` values. If not then * an error will be thrown. * * Type parameter can be specified to ensure values under comparison have the * same type. * * @example * ```ts * import { assertArrayIncludes } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertArrayIncludes([1, 2], [2]); // Doesn't throw * assertArrayIncludes([1, 2], [3]); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_array_includes.ts} instead. */export function assertArrayIncludes<T>( actual: ArrayLikeArg<T>, expected: ArrayLikeArg<T>, msg?: string,) { asserts.assertArrayIncludes<T>(actual, expected, msg);}
/** * Make an assertion that `actual` and `expected` are equal, deeply. If not * deeply equal, then throw. * * Type parameter can be specified to ensure values under comparison have the * same type. * * @example * ```ts * import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertEquals("world", "world"); // Doesn't throw * assertEquals("hello", "world"); // Throws * ``` * * Note: formatter option is experimental and may be removed in the future. * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_equals.ts} instead. */export function assertEquals<T>( actual: T, expected: T, msg?: string, options: { formatter?: (value: unknown) => string } = {},) { asserts.assertEquals<T>(actual, expected, msg, options);}
/** * Make an assertion that actual is not null or undefined. * If not then throw. * * @example * ```ts * import { assertExists } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertExists("something"); // Doesn't throw * assertExists(undefined); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_exists.ts} instead. */export function assertExists<T>( actual: T, msg?: string,): asserts actual is NonNullable<T> { asserts.assertExists<T>(actual, msg);}
/** * Assertion condition for {@linkcode assertFalse}. * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_false.ts} instead. */export type Falsy = false | 0 | 0n | "" | null | undefined;
/** * Make an assertion, error will be thrown if `expr` have truthy value. * * @example * ```ts * import { assertFalse } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertFalse(false); // Doesn't throw * assertFalse(true); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_false.ts} instead. */export function assertFalse(expr: unknown, msg = ""): asserts expr is Falsy { asserts.assertFalse(expr, msg);}
/** * Make an assertion that `actual` is greater than or equal to `expected`. * If not then throw. * * @example * ```ts * import { assertGreaterOrEqual } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertGreaterOrEqual(2, 1); // Doesn't throw * assertGreaterOrEqual(1, 1); // Doesn't throw * assertGreaterOrEqual(0, 1); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_greater_or_equal.ts} instead. */export function assertGreaterOrEqual<T>( actual: T, expected: T, msg?: string,) { asserts.assertGreaterOrEqual<T>(actual, expected, msg);}
/** * Make an assertion that `actual` is greater than `expected`. * If not then throw. * * @example * ```ts * import { assertGreater } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertGreater(2, 1); // Doesn't throw * assertGreater(1, 1); // Throws * assertGreater(0, 1); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_greater.ts} instead. */export function assertGreater<T>(actual: T, expected: T, msg?: string) { asserts.assertGreater<T>(actual, expected, msg);}
/** * Any constructor * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_instance_of.ts} instead. */// deno-lint-ignore no-explicit-anyexport type AnyConstructor = new (...args: any[]) => any;/** Gets constructor type * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_instance_of.ts} instead. */export type GetConstructorType<T extends AnyConstructor> = T extends // deno-lint-ignore no-explicit-anynew (...args: any) => infer C ? C : never;
/** * Make an assertion that `obj` is an instance of `type`. * If not then throw. * * @example * ```ts * import { assertInstanceOf } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertInstanceOf(new Date(), Date); // Doesn't throw * assertInstanceOf(new Date(), Number); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_instance_of.ts} instead. */export function assertInstanceOf<T extends AnyConstructor>( actual: unknown, expectedType: T, msg = "",): asserts actual is GetConstructorType<T> { asserts.assertInstanceOf<T>(actual, expectedType, msg);}
/** * Make an assertion that `error` is an `Error`. * If not then an error will be thrown. * An error class and a string that should be included in the * error message can also be asserted. * * @example * ```ts * import { assertIsError } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertIsError(null); // Throws * assertIsError(new RangeError("Out of range")); // Doesn't throw * assertIsError(new RangeError("Out of range"), SyntaxError); // Throws * assertIsError(new RangeError("Out of range"), SyntaxError, "Out of range"); // Doesn't throw * assertIsError(new RangeError("Out of range"), SyntaxError, "Within range"); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_is_error.ts} instead. */export function assertIsError<E extends Error = Error>( error: unknown, // deno-lint-ignore no-explicit-any ErrorClass?: new (...args: any[]) => E, msgMatches?: string | RegExp, msg?: string,): asserts error is E { asserts.assertIsError<E>(error, ErrorClass, msgMatches, msg);}
/** * Make an assertion that `actual` is less than or equal to `expected`. * If not then throw. * * @example * ```ts * import { assertLessOrEqual } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertLessOrEqual(1, 2); // Doesn't throw * assertLessOrEqual(1, 1); // Doesn't throw * assertLessOrEqual(1, 0); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_less_or_equal.ts} instead. */export function assertLessOrEqual<T>( actual: T, expected: T, msg?: string,) { asserts.assertLessOrEqual<T>(actual, expected, msg);}
/** * Make an assertion that `actual` is less than `expected`. * If not then throw. * * @example * ```ts * import { assertLess } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertLess(1, 2); // Doesn't throw * assertLess(2, 1); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_less.ts} instead. */export function assertLess<T>(actual: T, expected: T, msg?: string) { asserts.assertLess<T>(actual, expected, msg);}
/** * Make an assertion that `actual` match RegExp `expected`. If not * then throw. * * @example * ```ts * import { assertMatch } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertMatch("Raptor", RegExp(/Raptor/)); // Doesn't throw * assertMatch("Denosaurus", RegExp(/Raptor/)); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_match.ts} instead. */export function assertMatch( actual: string, expected: RegExp, msg?: string,) { asserts.assertMatch(actual, expected, msg);}
/** * Make an assertion that `actual` and `expected` are not equal, deeply. * If not then throw. * * Type parameter can be specified to ensure values under comparison have the same type. * * @example * ```ts * import { assertNotEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertNotEquals(1, 2); // Doesn't throw * assertNotEquals(1, 1); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_not_equals.ts} instead. */export function assertNotEquals<T>(actual: T, expected: T, msg?: string) { asserts.assertNotEquals<T>(actual, expected, msg);}
/** * Make an assertion that `obj` is not an instance of `type`. * If so, then throw. * * @example * ```ts * import { assertNotInstanceOf } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertNotInstanceOf(new Date(), Number); // Doesn't throw * assertNotInstanceOf(new Date(), Date); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_not_instance_of.ts} instead. */export function assertNotInstanceOf<A, T>( actual: A, // deno-lint-ignore no-explicit-any unexpectedType: new (...args: any[]) => T, msg?: string,): asserts actual is Exclude<A, T> { asserts.assertNotInstanceOf<A, T>(actual, unexpectedType, msg);}
/** * Make an assertion that `actual` not match RegExp `expected`. If match * then throw. * * @example * ```ts * import { assertNotMatch } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertNotMatch("Denosaurus", RegExp(/Raptor/)); // Doesn't throw * assertNotMatch("Raptor", RegExp(/Raptor/)); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_not_match.ts} instead. */export function assertNotMatch( actual: string, expected: RegExp, msg?: string,) { asserts.assertNotMatch(actual, expected, msg);}
/** * Make an assertion that `actual` and `expected` are not strictly equal. * If the values are strictly equal then throw. * * @example * ```ts * import { assertNotStrictEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertNotStrictEquals(1, 1); // Doesn't throw * assertNotStrictEquals(1, 2); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_not_strict_equals.ts} instead. */export function assertNotStrictEquals<T>( actual: T, expected: T, msg?: string,) { asserts.assertNotStrictEquals(actual, expected, msg);}
/** * Make an assertion that `actual` object is a subset of `expected` object, * deeply. If not, then throw. * * @example * ```ts * import { assertObjectMatch } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertObjectMatch({ foo: "bar" }, { foo: "bar" }); // Doesn't throw * assertObjectMatch({ foo: "bar" }, { foo: "baz" }); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_object_match.ts} instead. */export function assertObjectMatch( // deno-lint-ignore no-explicit-any actual: Record<PropertyKey, any>, expected: Record<PropertyKey, unknown>, msg?: string,) { asserts.assertObjectMatch(actual, expected, msg);}
/** * Executes a function which returns a promise, expecting it to reject. * * @example * ```ts * import { assertRejects } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * await assertRejects(async () => Promise.reject(new Error())); // Doesn't throw * await assertRejects(async () => console.log("Hello world")); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_rejects.ts} instead. */export function assertRejects( fn: () => PromiseLike<unknown>, msg?: string,): Promise<unknown>;/** * Executes a function which returns a promise, expecting it to reject. * If it does not, then it throws. An error class and a string that should be * included in the error message can also be asserted. * * @example * ```ts * import { assertRejects } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * await assertRejects(async () => Promise.reject(new Error()), Error); // Doesn't throw * await assertRejects(async () => Promise.reject(new Error()), SyntaxError); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_rejects.ts} instead. */export function assertRejects<E extends Error = Error>( fn: () => PromiseLike<unknown>, // deno-lint-ignore no-explicit-any ErrorClass: new (...args: any[]) => E, msgIncludes?: string, msg?: string,): Promise<E>;export async function assertRejects<E extends Error = Error>( fn: () => PromiseLike<unknown>, errorClassOrMsg?: // deno-lint-ignore no-explicit-any | (new (...args: any[]) => E) | string, msgIncludesOrMsg?: string, msg?: string,): Promise<E | Error | unknown> { return await asserts.assertRejects<E>( fn, // deno-lint-ignore no-explicit-any errorClassOrMsg as new (...args: any[]) => E, // Cast errorClassOrMsg to the correct type msgIncludesOrMsg, msg, );}
/** * Make an assertion that `actual` and `expected` are strictly equal. If * not then throw. * * @example * ```ts * import { assertStrictEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const a = {}; * const b = a; * assertStrictEquals(a, b); // Doesn't throw * * const c = {}; * const d = {}; * assertStrictEquals(c, d); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_strict_equals.ts} instead. */export function assertStrictEquals<T>( actual: unknown, expected: T, msg?: string,): asserts actual is T { asserts.assertStrictEquals<T>(actual, expected, msg);}
/** * Make an assertion that actual includes expected. If not * then throw. * * @example * ```ts * import { assertStringIncludes } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertStringIncludes("Hello", "ello"); // Doesn't throw * assertStringIncludes("Hello", "world"); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_string_includes.ts} instead. */export function assertStringIncludes( actual: string, expected: string, msg?: string,) { asserts.assertStringIncludes(actual, expected, msg);}
/** * Executes a function, expecting it to throw. If it does not, then it * throws. * * @example * ```ts * import { assertThrows } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertThrows(() => { throw new TypeError("hello world!"); }); // Doesn't throw * assertThrows(() => console.log("hello world!")); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_throws.ts} instead. */export function assertThrows( fn: () => unknown, msg?: string,): unknown;/** * Executes a function, expecting it to throw. If it does not, then it * throws. An error class and a string that should be included in the * error message can also be asserted. * * @example * ```ts * import { assertThrows } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assertThrows(() => { throw new TypeError("hello world!"); }, TypeError); // Doesn't throw * assertThrows(() => { throw new TypeError("hello world!"); }, RangeError); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert_throws.ts} instead. */export function assertThrows<E extends Error = Error>( fn: () => unknown, // deno-lint-ignore no-explicit-any ErrorClass: new (...args: any[]) => E, msgIncludes?: string, msg?: string,): E;export function assertThrows<E extends Error = Error>( fn: () => unknown, errorClassOrMsg?: // deno-lint-ignore no-explicit-any | (new (...args: any[]) => E) | string, msgIncludesOrMsg?: string, msg?: string,): E | Error | unknown { return asserts.assertThrows<E>( fn, // deno-lint-ignore no-explicit-any errorClassOrMsg as new (...args: any[]) => E, // Cast errorClassOrMsg to the correct type msgIncludesOrMsg, msg, );}
/** * Make an assertion, error will be thrown if `expr` does not have truthy value. * * @example * ```ts * import { assert } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * assert("hello".includes("ello")); // Doesn't throw * assert("hello".includes("world")); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assert.ts} instead. */export function assert(expr: unknown, msg = ""): asserts expr { asserts.assert(expr, msg);}
/** * Error thrown when an assertion fails. * * @example * ```ts * import { AssertionError } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * throw new AssertionError("Assertion failed"); * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/assertion_error.ts} instead. */export class AssertionError extends Error { /** Constructs a new instance. */ constructor(message: string) { super(message); this.name = "AssertionError"; }}
/** * Deep equality comparison used in assertions * @param c actual value * @param d expected value * * @example * ```ts * import { equal } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * equal({ foo: "bar" }, { foo: "bar" }); // Returns `true` * equal({ foo: "bar" }, { foo: "baz" }); // Returns `false * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/equal.ts} instead. */export function equal(c: unknown, d: unknown): boolean { return asserts.equal(c, d);}
/** * Forcefully throws a failed assertion. * * @example * ```ts * import { fail } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * fail("Deliberately failed!"); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/fail.ts} instead. */export function fail(msg?: string): never { asserts.fail(msg);}
/** * Use this to stub out methods that will throw when invoked. * * @example * ```ts * import { unimplemented } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * unimplemented(); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/unimplemented.ts} instead. */export function unimplemented(msg?: string): never { asserts.unimplemented(msg);}
/** * Use this to assert unreachable code. * * @example * ```ts * import { unreachable } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * unreachable(); // Throws * ``` * * @deprecated This will be removed in 1.0.0. Import from {@link https://deno.land/std/assert/unreachable.ts} instead. */export function unreachable(): never { asserts.unreachable();}
std

Version Info

Tagged at
6 months ago