/
Mocked.test.ts
85 lines (60 loc) · 2.1 KB
/
Mocked.test.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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {expectAssignable, expectType} from 'tsd-lite';
import type {MockInstance, Mocked} from 'jest-mock';
/// mocks class
class ExampleClass {
constructor(c: string, d?: boolean) {}
// _propertyB: false;
methodA() {
return true;
}
methodB(a: string, b: number) {
return;
}
methodC(e: any) {
throw new Error();
}
// propertyA: 'abc',
}
const MockExampleClass = ExampleClass as Mocked<typeof ExampleClass>;
const xx = MockExampleClass.mock.calls[0];
const yy = MockExampleClass.prototype.methodB.mock.calls[0];
const ww = MockExampleClass.mock.instances[0].methodB.mock.calls[0];
const mockExample = new MockExampleClass('c') as Mocked<
InstanceType<typeof MockExampleClass>
>;
const zz = mockExample.methodB.mock.calls[0];
/// mocks function
function someFunction(a: number, b?: string): boolean {
return true;
}
async function someAsyncFunction(a: Array<boolean>): Promise<string> {
return 'true';
}
const mockFunction = someFunction as Mocked<typeof someFunction>;
expectType<number>(mockFunction.mock.calls[0][0]);
expectType<string | undefined>(mockFunction.mock.calls[0][1]);
const mockFunctionResult = mockFunction.mock.results[0];
if (mockFunctionResult.type === 'return') {
expectType<boolean>(mockFunctionResult.value);
}
const mockAsyncFunction = someAsyncFunction as Mocked<typeof someAsyncFunction>;
expectType<Array<boolean>>(mockAsyncFunction.mock.calls[0][0]);
// expectError(mockAsyncFunction.mock.calls[0][1]);
const mockAsyncFunctionResult = mockAsyncFunction.mock.results[0];
if (mockAsyncFunctionResult.type === 'return') {
expectType<Promise<string>>(mockAsyncFunctionResult.value);
}
// mocks object
const mockConsole = console as Mocked<typeof console>;
expectAssignable<typeof console.log>(
mockConsole.log.mockImplementation(() => {}),
);
expectAssignable<MockInstance<typeof console.log>>(
mockConsole.log.mockImplementation(() => {}),
);