a 4.0.2 → 4.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.d.ts +94 -81
  2. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -1,33 +1,44 @@
1
1
  type AnyFunction = (...args: any[]) => any;
2
2
  type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
3
-
4
- interface RepeatControl {
5
- repeat(times: number): RepeatControl;
6
- repeatAny(): RepeatControl;
7
- }
8
-
9
- interface ExpectationTerminal<R, TArgs extends any[]> {
10
- return(value?: R): RepeatControl;
11
- whenCalled(callback: (...args: TArgs) => void): ExpectationTerminal<R, TArgs>;
12
- repeat(times: number): RepeatControl;
13
- repeatAny(): RepeatControl;
14
- throw(error: unknown): ExpectationTerminal<R, TArgs>;
15
- resolve(value: any): RepeatControl;
16
- reject(value: any): RepeatControl;
17
- }
18
-
19
- interface ExpectationChain<TArgs extends any[], R>
20
- extends ExpectationTerminal<R, TArgs> {
21
- expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
22
- expectAnything(): ExpectationChain<Tail<TArgs>, R>;
23
- ignore(): ExpectationChain<Tail<TArgs>, R>;
24
- expectArray(
25
- value: TArgs[0] extends any[] ? TArgs[0] : any[]
26
- ): ExpectationChain<Tail<TArgs>, R>;
27
- }
28
-
3
+ type FunctionProps<T> = T extends (...args: any[]) => any
4
+ ? { [K in Exclude<keyof T, keyof Function>]: Mocked<T[K]> }
5
+ : {};
6
+ type MockedFunction<T extends AnyFunction> = MockFunction<
7
+ Parameters<T>,
8
+ ReturnType<T>
9
+ > &
10
+ T &
11
+ FunctionProps<T>;
12
+
13
+ interface RepeatControl {
14
+ repeat(times: number): RepeatControl;
15
+ repeatAny(): RepeatControl;
16
+ }
17
+
18
+ interface ExpectationTerminal<R, TArgs extends any[]> {
19
+ return(value?: R): RepeatControl;
20
+ whenCalled(callback: (...args: TArgs) => void): ExpectationTerminal<R, TArgs>;
21
+ repeat(times: number): RepeatControl;
22
+ repeatAny(): RepeatControl;
23
+ throw(error: unknown): ExpectationTerminal<R, TArgs>;
24
+ resolve(value: any): RepeatControl;
25
+ reject(value: any): RepeatControl;
26
+ }
27
+
28
+ interface ExpectationChain<TArgs extends any[], R>
29
+ extends ExpectationTerminal<R, TArgs> {
30
+ expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
31
+ expectAnything(): ExpectationChain<Tail<TArgs>, R>;
32
+ ignore(): ExpectationChain<Tail<TArgs>, R>;
33
+ expectArray(
34
+ value: TArgs[0] extends any[] ? TArgs[0] : any[]
35
+ ): ExpectationChain<Tail<TArgs>, R>;
36
+ }
37
+
29
38
  interface MockFunction<TArgs extends any[], R> {
30
39
  (...args: TArgs): R;
40
+ // Allow any-args calls in addition to the original signature.
41
+ (...args: any[]): any;
31
42
  expect(): ExpectationTerminal<R, TArgs>;
32
43
  expect(...args: TArgs): ExpectationTerminal<R, TArgs>;
33
44
  expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
@@ -36,61 +47,63 @@ interface MockFunction<TArgs extends any[], R> {
36
47
  expectArray(
37
48
  value: TArgs[0] extends any[] ? TArgs[0] : any[]
38
49
  ): ExpectationChain<Tail<TArgs>, R>;
50
+ // Allow any-args expectations in addition to typed expectations.
51
+ expect(...args: any[]): ExpectationTerminal<any, any[]>;
39
52
  verify(): true;
40
53
  reset(): void;
41
54
  }
42
-
43
- type Mocked<T> = T extends (...args: infer A) => infer R
44
- ? MockFunction<A, R>
45
- : T extends object
46
- ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
47
- : T;
48
-
49
- interface RequireExpectation {
50
- return(value: any): RequireExpectation;
51
- repeat(times: number): RequireExpectation;
52
- repeatAny(): RequireExpectation;
53
- whenCalled(callback: (...args: any[]) => void): RequireExpectation;
54
- }
55
-
56
- interface ThenableMock<T = any> {
57
- (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
58
- then<U = T>(
59
- success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
60
- fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
61
- ): ThenableMock<U>;
62
- resolve(value?: T): ThenableMock<T>;
63
- reject(error: any): ThenableMock<never>;
64
- }
65
-
66
- declare function mock<TArgs extends any[] = any[], R = any>(): MockFunction<
67
- TArgs,
68
- R
69
- >;
70
- declare function mock<T extends AnyFunction>(
71
- original: T
72
- ): MockFunction<Parameters<T>, ReturnType<T>>;
73
- declare function mock<T extends object>(subject: T): Mocked<T>;
74
-
75
- declare function expectRequire(moduleName: string): RequireExpectation;
76
- declare namespace expectRequire {
77
- function reset(): void;
78
- }
79
-
80
- declare function requireMock<TArgs extends any[] = any[], R = any>(
81
- moduleName: string
82
- ): MockFunction<TArgs, R>;
83
- declare namespace requireMock {
84
- function reset(): void;
85
- }
86
-
87
- declare function promise<T = any>(): ThenableMock<T>;
88
-
89
- declare const aMock: {
90
- mock: typeof mock;
91
- expectRequire: typeof expectRequire;
92
- requireMock: typeof requireMock;
93
- promise: typeof promise;
94
- };
95
-
96
- export = aMock;
55
+
56
+ type Mocked<T> = T extends AnyFunction
57
+ ? MockedFunction<T>
58
+ : T extends object
59
+ ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
60
+ : T;
61
+
62
+ interface RequireExpectation {
63
+ return(value: any): RequireExpectation;
64
+ repeat(times: number): RequireExpectation;
65
+ repeatAny(): RequireExpectation;
66
+ whenCalled(callback: (...args: any[]) => void): RequireExpectation;
67
+ }
68
+
69
+ interface ThenableMock<T = any> {
70
+ (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
71
+ then<U = T>(
72
+ success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
73
+ fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
74
+ ): ThenableMock<U>;
75
+ resolve(value?: T): ThenableMock<T>;
76
+ reject(error: any): ThenableMock<never>;
77
+ }
78
+
79
+ declare function mock<TArgs extends any[] = any[], R = any>(): MockFunction<
80
+ TArgs,
81
+ R
82
+ >;
83
+ declare function mock<T extends AnyFunction>(
84
+ original: T
85
+ ): MockedFunction<T>;
86
+ declare function mock<T extends object>(subject: T): Mocked<T>;
87
+
88
+ declare function expectRequire(moduleName: string): RequireExpectation;
89
+ declare namespace expectRequire {
90
+ function reset(): void;
91
+ }
92
+
93
+ declare function requireMock<TArgs extends any[] = any[], R = any>(
94
+ moduleName: string
95
+ ): MockFunction<TArgs, R>;
96
+ declare namespace requireMock {
97
+ function reset(): void;
98
+ }
99
+
100
+ declare function promise<T = any>(): ThenableMock<T>;
101
+
102
+ declare const aMock: {
103
+ mock: typeof mock;
104
+ expectRequire: typeof expectRequire;
105
+ requireMock: typeof requireMock;
106
+ promise: typeof promise;
107
+ };
108
+
109
+ export = aMock;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "a",
3
- "version": "4.0.2",
4
- "main": "index.js",
5
- "types": "index.d.ts",
6
- "title": "a",
3
+ "version": "4.0.4",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "title": "a",
7
7
  "description": "Mocking framework",
8
8
  "keywords": [
9
9
  "mock",