deno.land / std@0.224.0 / expect / expect.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
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.// Copyright 2019 Allain Lalonde. All rights reserved. ISC License.
import type { Expected, ExtendMatchResult, Matcher, MatcherContext, MatcherKey, Matchers,} from "./_types.ts";import { AssertionError } from "../assert/assertion_error.ts";import { addCustomEqualityTesters, getCustomEqualityTesters,} from "./_custom_equality_tester.ts";import { equal } from "./_equal.ts";import { getExtendMatchers, setExtendMatchers } from "./_extend.ts";import { toBe, toBeCloseTo, toBeDefined, toBeFalsy, toBeGreaterThan, toBeGreaterThanOrEqual, toBeInstanceOf, toBeLessThan, toBeLessThanOrEqual, toBeNaN, toBeNull, toBeTruthy, toBeUndefined, toContain, toContainEqual, toEqual, toHaveBeenCalled, toHaveBeenCalledTimes, toHaveBeenCalledWith, toHaveBeenLastCalledWith, toHaveBeenNthCalledWith, toHaveLastReturnedWith, toHaveLength, toHaveNthReturnedWith, toHaveProperty, toHaveReturned, toHaveReturnedTimes, toHaveReturnedWith, toMatch, toMatchObject, toStrictEqual, toThrow,} from "./_matchers.ts";import { addSerializer } from "./_snapshot_serializer.ts";import { isPromiseLike } from "./_utils.ts";import { any, anything, arrayContaining, closeTo, stringContaining, stringMatching,} from "./_asymmetric_matchers.ts";
const matchers: Record<MatcherKey, Matcher> = { lastCalledWith: toHaveBeenLastCalledWith, lastReturnedWith: toHaveLastReturnedWith, nthCalledWith: toHaveBeenNthCalledWith, nthReturnedWith: toHaveNthReturnedWith, toBeCalled: toHaveBeenCalled, toBeCalledTimes: toHaveBeenCalledTimes, toBeCalledWith: toHaveBeenCalledWith, toBeCloseTo, toBeDefined, toBeFalsy, toBeGreaterThanOrEqual, toBeGreaterThan, toBeInstanceOf, toBeLessThanOrEqual, toBeLessThan, toBeNaN, toBeNull, toBeTruthy, toBeUndefined, toBe, toContainEqual, toContain, toEqual, toHaveBeenCalledTimes, toHaveBeenCalledWith, toHaveBeenCalled, toHaveBeenLastCalledWith, toHaveBeenNthCalledWith, toHaveLength, toHaveLastReturnedWith, toHaveNthReturnedWith, toHaveProperty, toHaveReturnedTimes, toHaveReturnedWith, toHaveReturned, toMatchObject, toMatch, toReturn: toHaveReturned, toReturnTimes: toHaveReturnedTimes, toReturnWith: toHaveReturnedWith, toStrictEqual, toThrow,};
export function expect(value: unknown, customMessage?: string): Expected { let isNot = false; let isPromised = false; const self: Expected = new Proxy<Expected>( <Expected> {}, { get(_, name) { if (name === "not") { isNot = !isNot; return self; }
if (name === "resolves") { if (!isPromiseLike(value)) { throw new AssertionError("expected value must be Promiselike"); }
isPromised = true; return self; }
if (name === "rejects") { if (!isPromiseLike(value)) { throw new AssertionError("expected value must be a PromiseLike"); }
value = value.then( (value) => { throw new AssertionError( `Promise did not reject. resolved to ${value}`, ); }, (err) => err, ); isPromised = true; return self; }
const extendMatchers: Matchers = getExtendMatchers(); const allMatchers = { ...extendMatchers, ...matchers, }; const matcher = allMatchers[name as MatcherKey] as Matcher; if (!matcher) { throw new TypeError( typeof name === "string" ? `matcher not found: ${name}` : "matcher not found", ); }
return (...args: unknown[]) => { function applyMatcher(value: unknown, args: unknown[]) { const context: MatcherContext = { value, equal, isNot: false, customMessage, customTesters: getCustomEqualityTesters(), }; if (isNot) { context.isNot = true; } if (name in extendMatchers) { const result = matcher(context, ...args) as ExtendMatchResult; if (context.isNot) { if (result.pass) { throw new AssertionError(result.message()); } } else if (!result.pass) { throw new AssertionError(result.message()); } } else { matcher(context, ...args); } }
return isPromised ? (value as Promise<unknown>).then((value: unknown) => applyMatcher(value, args) ) : applyMatcher(value, args); }; }, }, );
return self;}
expect.addEqualityTesters = addCustomEqualityTesters;/** * @deprecated (will be removed in 0.226.0) Use {@linkcode expect.addSnapshotSerializer} instead. */expect.addSnapshotSerializers = addSerializer;expect.addSnapshotSerializer = addSerializer;expect.extend = setExtendMatchers;
expect.anything = anything;expect.any = any;expect.arrayContaining = arrayContaining;expect.closeTo = closeTo;expect.stringContaining = stringContaining;expect.stringMatching = stringMatching;
std

Version Info

Tagged at
3 weeks ago