deno.land / x / expect@v0.4.2 / mock.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
const MOCK_SYMBOL = Symbol.for("@MOCK");
export type MockCall = { args: any[]; returned?: any; thrown?: any; timestamp: number; returns: boolean; throws: boolean;};
export function fn(...stubs: Function[]) { const calls: MockCall[] = [];
const f = (...args: any[]) => { const stub = stubs.length === 1 ? // keep reusing the first stubs[0] : // pick the exact mock for the current call stubs[calls.length];
try { const returned = stub ? stub(...args) : undefined; calls.push({ args, returned, timestamp: Date.now(), returns: true, throws: false, }); return returned; } catch (err) { calls.push({ args, timestamp: Date.now(), returns: false, thrown: err, throws: true, }); throw err; } };
Object.defineProperty(f, MOCK_SYMBOL, { value: { calls }, writable: false, });
return f;}
export function calls(f: Function): MockCall[] { const mockInfo = (f as any)[MOCK_SYMBOL]; if (!mockInfo) throw new Error("callCount only available on mock functions");
return [...mockInfo.calls];}
expect

Version Info

Tagged at
6 months ago