This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
asserts.ts
311 lines (296 loc) · 7.78 KB
/
asserts.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
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
/** This module is browser compatible. */
import {
assertEquals,
AssertionError,
assertIsError,
assertRejects,
} from "./deps.ts";
import { Spy, SpyCall } from "./mock.ts";
/**
* Asserts that a spy is called as much as expected and no more.
*/
export function assertSpyCalls<
Self,
Args extends unknown[],
Return,
>(
spy: Spy<Self, Args, Return>,
expectedCalls: number,
) {
try {
assertEquals(spy.calls.length, expectedCalls);
} catch (e) {
let message = spy.calls.length < expectedCalls
? "spy not called as much as expected:\n"
: "spy called more than expected:\n";
message += e.message.split("\n").slice(1).join("\n");
throw new AssertionError(message);
}
}
/** Call information recorded by a spy. */
export interface ExpectedSpyCall<
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
// deno-lint-ignore no-explicit-any
Return = any,
> {
/** Arguments passed to a function when called. */
args?: [...Args, ...unknown[]];
/** The instance that a method was called on. */
self?: Self;
/**
* The value that was returned by a function.
* If you expect a promise to reject, expect error instead.
*/
returned?: Return;
error?: {
/** The class for the error that was thrown by a function. */
// deno-lint-ignore no-explicit-any
Class?: new (...args: any[]) => Error;
/** Part of the message for the error that was thrown by a function. */
msgIncludes?: string;
};
}
/**
* Asserts that a spy is called as expected.
* Returns the call.
*/
export function assertSpyCall<
Self,
Args extends unknown[],
Return,
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
expected?: ExpectedSpyCall<Self, Args, Return>,
) {
if (spy.calls.length < (callIndex + 1)) {
throw new AssertionError("spy not called as much as expected");
}
const call: SpyCall = spy.calls[callIndex];
if (expected) {
if (expected.args) {
try {
assertEquals(call.args, expected.args);
} catch (e) {
throw new AssertionError(
"spy not called with expected args:\n" +
e.message.split("\n").slice(1).join("\n"),
);
}
}
if ("self" in expected) {
try {
assertEquals(call.self, expected.self);
} catch (e) {
let message = expected.self
? "spy not called as method on expected self:\n"
: "spy not expected to be called as method on object:\n";
message += e.message.split("\n").slice(1).join("\n");
throw new AssertionError(message);
}
}
if ("returned" in expected) {
if ("error" in expected) {
throw new TypeError(
"do not expect error and return, only one should be expected",
);
}
if (call.error) {
throw new AssertionError(
"spy call did not return expected value, an error was thrown.",
);
}
try {
assertEquals(call.returned, expected.returned);
} catch (e) {
throw new AssertionError(
"spy call did not return expected value:\n" +
e.message.split("\n").slice(1).join("\n"),
);
}
}
if ("error" in expected) {
if ("returned" in call) {
throw new AssertionError(
"spy call did not throw an error, a value was returned.",
);
}
assertIsError(
call.error,
expected.error?.Class,
expected.error?.msgIncludes,
);
}
}
return call;
}
/**
* Asserts that an async spy is called as expected.
* Returns the call.
*/
export async function assertSpyCallAsync<
Self,
Args extends unknown[],
Return,
>(
spy: Spy<Self, Args, Promise<Return>>,
callIndex: number,
expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>,
) {
const expectedSync = expected && { ...expected };
if (expectedSync) {
delete expectedSync.returned;
delete expectedSync.error;
}
const call: SpyCall = assertSpyCall(
spy,
callIndex,
expectedSync,
);
if (call.error) {
throw new AssertionError(
"spy call did not return a promise, an error was thrown.",
);
}
if (call.returned !== Promise.resolve(call.returned)) {
throw new AssertionError(
"spy call did not return a promise, a value was returned.",
);
}
if (expected) {
if ("returned" in expected) {
if ("error" in expected) {
throw new TypeError(
"do not expect error and return, only one should be expected",
);
}
if (call.error) {
throw new AssertionError(
"spy call did not return expected value, an error was thrown.",
);
}
let expectedResolved;
try {
expectedResolved = await expected.returned;
} catch {
throw new TypeError(
"do not expect rejected promise, expect error instead",
);
}
let resolved;
try {
resolved = await call.returned;
} catch {
throw new AssertionError("spy call returned promise was rejected");
}
try {
assertEquals(resolved, expectedResolved);
} catch (e) {
throw new AssertionError(
"spy call did not resolve to expected value:\n" +
e.message.split("\n").slice(1).join("\n"),
);
}
}
if ("error" in expected) {
await assertRejects(
() => Promise.resolve(call.returned),
expected.error?.Class ?? Error,
expected.error?.msgIncludes ?? "",
);
}
}
return call;
}
/**
* Asserts that a spy is called with a specific arg as expected.
* Returns the actual arg.
*/
export function assertSpyCallArg<
Self,
Args extends unknown[],
Return,
ExpectedArg,
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
argIndex: number,
expected: ExpectedArg,
): ExpectedArg {
const call: SpyCall = assertSpyCall(spy, callIndex);
const arg = call.args[argIndex];
assertEquals(arg, expected);
return arg as ExpectedArg;
}
/**
* Asserts that an spy is called with a specific range of args as expected.
* If a start and end index is not provided, the expected will be compared against all args.
* If a start is provided without an end index, the expected will be compared against all args from the start index to the end.
* The end index is not included in the range of args that are compared.
* Returns the actual args.
*/
export function assertSpyCallArgs<
Self,
Args extends unknown[],
Return,
ExpectedArgs extends unknown[],
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
expected: ExpectedArgs,
): ExpectedArgs;
export function assertSpyCallArgs<
Self,
Args extends unknown[],
Return,
ExpectedArgs extends unknown[],
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
argsStart: number,
expected: ExpectedArgs,
): ExpectedArgs;
export function assertSpyCallArgs<
Self,
Args extends unknown[],
Return,
ExpectedArgs extends unknown[],
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
argStart: number,
argEnd: number,
expected: ExpectedArgs,
): ExpectedArgs;
export function assertSpyCallArgs<
ExpectedArgs extends unknown[],
Args extends unknown[],
Return,
Self,
>(
spy: Spy<Self, Args, Return>,
callIndex: number,
argsStart?: number | ExpectedArgs,
argsEnd?: number | ExpectedArgs,
expected?: ExpectedArgs,
): ExpectedArgs {
const call: SpyCall = assertSpyCall(spy, callIndex);
if (!expected) {
expected = argsEnd as ExpectedArgs;
argsEnd = undefined;
}
if (!expected) {
expected = argsStart as ExpectedArgs;
argsStart = undefined;
}
const args = typeof argsEnd === "number"
? call.args.slice(argsStart as number, argsEnd)
: typeof argsStart === "number"
? call.args.slice(argsStart)
: call.args;
assertEquals(args, expected);
return args as ExpectedArgs;
}