a_mock 2.0.0 → 2.0.1

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 (3) hide show
  1. package/index.d.ts +96 -0
  2. package/package.json +2 -1
  3. package/util.js +7 -1
package/index.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ type AnyFunction = (...args: any[]) => any;
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
+
29
+ interface MockFunction<TArgs extends any[], R> {
30
+ (...args: TArgs): R;
31
+ expect(): ExpectationTerminal<R, TArgs>;
32
+ expect(...args: TArgs): ExpectationTerminal<R, TArgs>;
33
+ expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
34
+ expectAnything(): ExpectationChain<Tail<TArgs>, R>;
35
+ ignore(): ExpectationChain<Tail<TArgs>, R>;
36
+ expectArray(
37
+ value: TArgs[0] extends any[] ? TArgs[0] : any[]
38
+ ): ExpectationChain<Tail<TArgs>, R>;
39
+ verify(): true;
40
+ reset(): void;
41
+ }
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;
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "a_mock",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "main": "index.js",
5
+ "types": "index.d.ts",
5
6
  "title": "a_mock",
6
7
  "description": "Sub package of a. Mocking framework",
7
8
  "keywords": ["mock","mocking","partial mock","strict mock","tdd","stub","stubbing","mock require","verify"],
package/util.js CHANGED
@@ -1 +1,7 @@
1
- module.exports = require('util');
1
+ function isDate(x) {
2
+ return Object.prototype.toString.call(x) === '[object Date]';
3
+ }
4
+
5
+ module.exports = {
6
+ isDate: isDate
7
+ };