All files / testing / mock.ts

100.00% Branches 0/0
100.00% Lines 76/76
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
x1
x1
 
 
 
 
 
 
 
 
 
 
 
x1
x2
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
x1
 
 
 
 
 
 
 
 
x1
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
 
 
 
 
 
 
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x1
x1
































































































































































































































































































































































































import { MockError as _class_MockError } from "jsr:@std/[email protected]/mock"
/**
 * An error related to spying on a function or instance method.
 *
 * @example Usage
 * ```ts
 * import { MockError, spy } from "@std/testing/mock";
 * import { assertThrows } from "@std/assert";
 *
 * assertThrows(() => {
 *   spy({} as any, "no-such-method");
 * }, MockError);
 * ```
 */
class MockError extends _class_MockError {}
export { MockError }

import type { SpyCall as _interface_SpyCall } from "jsr:@std/[email protected]/mock"
/**
 * Call information recorded by a spy.
 */
interface SpyCall<Self = any, Args extends unknown[] = any[], Return = any> extends _interface_SpyCall<Self, Args, Return> {}
export type { SpyCall }

import type { Spy as _interface_Spy } from "jsr:@std/[email protected]/mock"
/**
 * A function or instance method wrapper that records all calls made to it.
 */
interface Spy<Self = any, Args extends unknown[] = any[], Return = any> extends _interface_Spy<Self, Args, Return> {}
export type { Spy }

import type { MethodSpy as _interface_MethodSpy } from "jsr:@std/[email protected]/mock"
/**
 * An instance method wrapper that records all calls made to it.
 */
interface MethodSpy<Self = any, Args extends unknown[] = any[], Return = any> extends _interface_MethodSpy<Self, Args, Return> {}
export type { MethodSpy }

import { mockSession as _function_mockSession } from "jsr:@std/[email protected]/mock"
/** UNDOCUMENTED */
const mockSession = _function_mockSession as typeof _function_mockSession
export { mockSession }

import { mockSessionAsync as _function_mockSessionAsync } from "jsr:@std/[email protected]/mock"
/**
 * Creates an async session that tracks all mocks created before the promise resolves.
 *
 * @example Usage
 * ```ts
 * import { mockSessionAsync, restore, stub } from "@std/testing/mock";
 * import { assertEquals, assertNotEquals } from "@std/assert";
 *
 * const setTimeout = globalThis.setTimeout;
 * const session = mockSessionAsync(async () => {
 *   stub(globalThis, "setTimeout");
 *   assertNotEquals(globalThis.setTimeout, setTimeout);
 * });
 *
 * await session();
 *
 * assertEquals(globalThis.setTimeout, setTimeout); // stub is restored
 * ```
 * @template Self The self type of the function.
 * @template Args The arguments type of the function.
 * @template Return The return type of the function.
 * @param func The function.
 * @return The return value of the function.
 */
const mockSessionAsync = _function_mockSessionAsync as typeof _function_mockSessionAsync
export { mockSessionAsync }

import { restore as _function_restore } from "jsr:@std/[email protected]/mock"
/**
 * Restores all mocks registered in the current session that have not already been restored.
 * If an id is provided, it will restore all mocks registered in the session associed with that id that have not already been restored.
 *
 * @example Usage
 * ```ts
 * import { mockSession, restore, stub } from "@std/testing/mock";
 * import { assertEquals, assertNotEquals } from "@std/assert";
 *
 * const setTimeout = globalThis.setTimeout;
 *
 * stub(globalThis, "setTimeout");
 *
 * assertNotEquals(globalThis.setTimeout, setTimeout);
 *
 * restore();
 *
 * assertEquals(globalThis.setTimeout, setTimeout);
 * ```
 *
 * @param id The id of the session to restore. If not provided, all mocks registered in the current session are restored.
 */
const restore = _function_restore as typeof _function_restore
export { restore }

import type { ConstructorSpy as _interface_ConstructorSpy } from "jsr:@std/[email protected]/mock"
/**
 * A constructor wrapper that records all calls made to it.
 */
interface ConstructorSpy<Self = any, Args extends unknown[] = any[]> extends _interface_ConstructorSpy<Self, Args> {}
export type { ConstructorSpy }

import type { GetParametersFromProp as _typeAlias_GetParametersFromProp } from "jsr:@std/[email protected]/mock"
/**
 * Utility for extracting the arguments type from a property
 *
 * @internal
 */
type GetParametersFromProp<Self, Prop extends keyof Self> = _typeAlias_GetParametersFromProp<Self, Prop>
export type { GetParametersFromProp }

import type { GetReturnFromProp as _typeAlias_GetReturnFromProp } from "jsr:@std/[email protected]/mock"
/**
 * Utility for extracting the return type from a property
 *
 * @internal
 */
type GetReturnFromProp<Self, Prop extends keyof Self> = _typeAlias_GetReturnFromProp<Self, Prop>
export type { GetReturnFromProp }

import type { SpyLike as _typeAlias_SpyLike } from "jsr:@std/[email protected]/mock"
/**
 * SpyLink object type.
 */
type SpyLike<Self = any, Args extends unknown[] = any[], Return = any> = _typeAlias_SpyLike<Self, Args, Return>
export type { SpyLike }

import { spy as _function_spy } from "jsr:@std/[email protected]/mock"
/** UNDOCUMENTED */
const spy = _function_spy as typeof _function_spy
export { spy }

import type { Stub as _interface_Stub } from "jsr:@std/[email protected]/mock"
/**
 * An instance method replacement that records all calls made to it.
 */
interface Stub<Self = any, Args extends unknown[] = any[], Return = any> extends _interface_Stub<Self, Args, Return> {}
export type { Stub }

import { stub as _function_stub } from "jsr:@std/[email protected]/mock"
/** UNDOCUMENTED */
const stub = _function_stub as typeof _function_stub
export { stub }

import { assertSpyCalls as _function_assertSpyCalls } from "jsr:@std/[email protected]/mock"
/**
 * Asserts that a spy is called as much as expected and no more.
 *
 * @example Usage
 * ```ts
 * import { assertSpyCalls, spy } from "@std/testing/mock";
 *
 * const func = spy();
 *
 * func();
 * func();
 *
 * assertSpyCalls(func, 2);
 * ```
 *
 * @template Self The self type of the spy function.
 * @template Args The arguments type of the spy function.
 * @template Return The return type of the spy function.
 * @param spy The spy to check
 * @param expectedCalls The number of the expected calls.
 */
const assertSpyCalls = _function_assertSpyCalls as typeof _function_assertSpyCalls
export { assertSpyCalls }

import type { ExpectedSpyCall as _interface_ExpectedSpyCall } from "jsr:@std/[email protected]/mock"
/**
 * Call information recorded by a spy.
 */
interface ExpectedSpyCall<Self = any, Args extends unknown[] = any[], Return = any> extends _interface_ExpectedSpyCall<Self, Args, Return> {}
export type { ExpectedSpyCall }

import { assertSpyCall as _function_assertSpyCall } from "jsr:@std/[email protected]/mock"
/**
 * Asserts that a spy is called as expected.
 *
 * @example Usage
 * ```ts
 * import { assertSpyCall, spy } from "@std/testing/mock";
 *
 * const func = spy((a: number, b: number) => a + b);
 *
 * func(3, 4);
 * func(5, 6);
 *
 * // asserts each call made to the spy function.
 * assertSpyCall(func, 0, { args: [3, 4], returned: 7 });
 * assertSpyCall(func, 1, { args: [5, 6], returned: 11 });
 * ```
 *
 * @template Self The self type of the spy function.
 * @template Args The arguments type of the spy function.
 * @template Return The return type of the spy function.
 * @param spy The spy to check
 * @param callIndex The index of the call to check
 * @param expected The expected spy call.
 */
const assertSpyCall = _function_assertSpyCall as typeof _function_assertSpyCall
export { assertSpyCall }

import { assertSpyCallAsync as _function_assertSpyCallAsync } from "jsr:@std/[email protected]/mock"
/**
 * Asserts that an async spy is called as expected.
 *
 * @example Usage
 * ```ts
 * import { assertSpyCallAsync, spy } from "@std/testing/mock";
 *
 * const func = spy((a: number, b: number) => new Promise((resolve) => {
 *   setTimeout(() => resolve(a + b), 100)
 * }));
 *
 * await func(3, 4);
 * await func(5, 6);
 *
 * // asserts each call made to the spy function.
 * await assertSpyCallAsync(func, 0, { args: [3, 4], returned: 7 });
 * await assertSpyCallAsync(func, 1, { args: [5, 6], returned: 11 });
 * ```
 *
 * @template Self The self type of the spy function.
 * @template Args The arguments type of the spy function.
 * @template Return The return type of the spy function.
 * @param spy The spy to check
 * @param callIndex The index of the call to check
 * @param expected The expected spy call.
 */
const assertSpyCallAsync = _function_assertSpyCallAsync as typeof _function_assertSpyCallAsync
export { assertSpyCallAsync }

import { assertSpyCallArg as _function_assertSpyCallArg } from "jsr:@std/[email protected]/mock"
/**
 * Asserts that a spy is called with a specific arg as expected.
 *
 * @example Usage
 * ```ts
 * import { assertSpyCallArg, spy } from "@std/testing/mock";
 *
 * const func = spy((a: number, b: number) => a + b);
 *
 * func(3, 4);
 * func(5, 6);
 *
 * // asserts each call made to the spy function.
 * assertSpyCallArg(func, 0, 0, 3);
 * assertSpyCallArg(func, 0, 1, 4);
 * assertSpyCallArg(func, 1, 0, 5);
 * assertSpyCallArg(func, 1, 1, 6);
 * ```
 *
 * @template Self The self type of the spy function.
 * @template Args The arguments type of the spy function.
 * @template Return The return type of the spy function.
 * @template ExpectedArg The expected type of the argument for the spy to be called.
 * @param spy The spy to check.
 * @param callIndex The index of the call to check.
 * @param argIndex The index of the arguments to check.
 * @param expected The expected argument.
 * @return The actual argument.
 */
const assertSpyCallArg = _function_assertSpyCallArg as typeof _function_assertSpyCallArg
export { assertSpyCallArg }

import { assertSpyCallArgs as _function_assertSpyCallArgs } from "jsr:@std/[email protected]/mock"
/** UNDOCUMENTED */
const assertSpyCallArgs = _function_assertSpyCallArgs as typeof _function_assertSpyCallArgs
export { assertSpyCallArgs }

import { returnsThis as _function_returnsThis } from "jsr:@std/[email protected]/mock"
/**
 * Creates a function that returns the instance the method was called on.
 *
 * @example Usage
 * ```ts
 * import { returnsThis } from "@std/testing/mock";
 * import { assertEquals } from "@std/assert";
 *
 * const func = returnsThis();
 * const obj = { func };
 * assertEquals(obj.func(), obj);
 * ```
 *
 * @template Self The self type of the returned function.
 * @template Args The arguments type of the returned function.
 * @return A function that returns the instance the method was called on.
 */
const returnsThis = _function_returnsThis as typeof _function_returnsThis
export { returnsThis }

import { returnsArg as _function_returnsArg } from "jsr:@std/[email protected]/mock"
/**
 * Creates a function that returns one of its arguments.
 *
 * @example Usage
 * ```ts
 * import { returnsArg } from "@std/testing/mock";
 * import { assertEquals } from "@std/assert";
 *
 * const func = returnsArg(1);
 * assertEquals(func(1, 2, 3), 2);
 * ```
 *
 * @template Arg The type of returned argument.
 * @template Self The self type of the returned function.
 * @param idx The index of the arguments to use.
 * @return A function that returns one of its arguments.
 */
const returnsArg = _function_returnsArg as typeof _function_returnsArg
export { returnsArg }

import { returnsArgs as _function_returnsArgs } from "jsr:@std/[email protected]/mock"
/**
 * Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end.
 *
 * @example Usage
 * ```ts
 * import { returnsArgs } from "@std/testing/mock";
 * import { assertEquals } from "@std/assert";
 *
 * const func = returnsArgs();
 * assertEquals(func(1, 2, 3), [1, 2, 3]);
 * ```
 *
 * @template Args The arguments type of the returned function
 * @template Self The self type of the returned function
 * @param start The start index of the arguments to return. Default is 0.
 * @param end The end index of the arguments to return.
 * @return A function that returns its arguments or a subset of them.
 */
const returnsArgs = _function_returnsArgs as typeof _function_returnsArgs
export { returnsArgs }

import { returnsNext as _function_returnsNext } from "jsr:@std/[email protected]/mock"
/**
 * Creates a function that returns the iterable values. Any iterable values that are errors will be thrown.
 *
 * @example Usage
 * ```ts
 * import { returnsNext } from "@std/testing/mock";
 * import { assertEquals, assertThrows } from "@std/assert";
 *
 * const func = returnsNext([1, 2, new Error("foo"), 3]);
 * assertEquals(func(), 1);
 * assertEquals(func(), 2);
 * assertThrows(() => func(), Error, "foo");
 * assertEquals(func(), 3);
 * ```
 *
 * @template Return The type of each item of the iterable
 * @template Self The self type of the returned function
 * @template Args The arguments type of the returned function
 * @param values The iterable values
 * @return A function that returns the iterable values
 */
const returnsNext = _function_returnsNext as typeof _function_returnsNext
export { returnsNext }

import { resolvesNext as _function_resolvesNext } from "jsr:@std/[email protected]/mock"
/**
 * Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown.
 *
 * @example Usage
 * ```ts
 * import { resolvesNext } from "@std/testing/mock";
 * import { assertEquals, assertRejects } from "@std/assert";
 *
 * const func = resolvesNext([1, 2, new Error("foo"), 3]);
 * assertEquals(await func(), 1);
 * assertEquals(await func(), 2);
 * assertRejects(() => func(), Error, "foo");
 * assertEquals(await func(), 3);
 * ```
 *
 * @template Return The type of each item of the iterable
 * @template Self The self type of the returned function
 * @template Args The type of arguments of the returned function
 * @param iterable The iterable to use
 * @return A function that resolves the awaited iterable values
 */
const resolvesNext = _function_resolvesNext as typeof _function_resolvesNext
export { resolvesNext }