deno.land / std@0.224.0 / expect / _to_throw_test.ts

_to_throw_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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { expect } from "./expect.ts";import { AssertionError, assertThrows } from "../assert/mod.ts";
Deno.test("expect().toThrow()", () => { expect(() => { throw new Error("hello world"); }).toThrow();
expect(() => {}).not.toThrow();
assertThrows(() => { expect(() => {}).toThrow(); }, AssertionError);
assertThrows(() => { expect(() => { throw new Error("hello world"); }).not.toThrow(); }, AssertionError);
// Passes when type is correct expect(() => { throw new AssertionError("hello world"); }).toThrow(AssertionError);
// Throws when type is incorrect assertThrows( () => { expect(() => { throw new Error("hello world"); }).toThrow(AssertionError); }, AssertionError, 'Expected error to be instance of "AssertionError", but was "Error".', );
// Passes when error instance type is correct expect(() => { throw new AssertionError("hello world"); }).toThrow(new AssertionError("hello world"));
// Throws when error instance type is incorrect assertThrows( () => { expect(() => { throw new Error("hello world"); }).toThrow(new AssertionError("hello world")); }, AssertionError, 'Expected error to be instance of "AssertionError", but was "Error".', );
// Passes when literal string is in error expect(() => { throw new AssertionError("hello world"); }).toThrow("hello");
// Throws when literal string is not in error assertThrows( () => { expect(() => { throw new AssertionError("hello world"); }).toThrow("goodbye"); }, AssertionError, 'Expected error message to include "goodbye", but got "hello world".', );
// Passes when error instance string is in error expect(() => { throw new AssertionError("hello world"); }).toThrow(new AssertionError("hello world"));
// Throws when error instance string is not in error assertThrows( () => { expect(() => { throw new AssertionError("hello world"); }).toThrow(new AssertionError("goodbye")); }, AssertionError, 'Expected error message to include "goodbye", but got "hello world".', );
// Passes when regex does match error expect(() => { throw new AssertionError("hello world"); }).toThrow(/\w/);
// Throws when regex does not match error assertThrows( () => { expect(() => { throw new Error("hello world"); }).toThrow(/\d/); }, AssertionError, 'Expected error message to include /\\d/, but got "hello world".', );});
std

Version Info

Tagged at
6 months ago