|
1 |
| -export type TestDouble = Function; |
| 1 | +// |
| 2 | +// types and interfaces |
| 3 | +// ---------------------------------------------------------------------------- |
2 | 4 |
|
3 |
| -export type DoubledObjectWithKey<Key extends string> = {}; |
| 5 | +export type DoubledObject<T> = T; |
4 | 6 |
|
5 |
| -export type DoubledObject<Subject> = Subject; |
| 7 | +export type DoubledObjectWithKey<T extends string> = { [K in T] }; |
6 | 8 |
|
7 |
| -export interface TestdoubleConfig { |
8 |
| - promiseConstructor?: any; |
9 |
| - ignoreWarnings?: boolean; |
10 |
| - suppressErrors?: boolean; |
11 |
| -} |
12 |
| -export function config(config: TestdoubleConfig): void; |
13 |
| - |
14 |
| -declare function functionDouble(name?: string): TestDouble; |
15 |
| -export { functionDouble as function }; |
| 9 | +export type TestDouble<T> = T; |
16 | 10 |
|
17 |
| -// When passed class or constructor function |
18 |
| -export function object<T>(constructor: { new (...args: any[]): T }): DoubledObject<T>; |
| 11 | +export type TestDoubleConstructor<T> = Constructor<T>; |
19 | 12 |
|
20 |
| -// When passed array of props |
21 |
| -export function object<Key extends string>(props: Key[]): DoubledObjectWithKey<Key>; |
22 |
| - |
23 |
| -// When passed class or constuctor function name as string value |
24 |
| -export function object<T>(object: string): DoubledObject<T>; |
25 |
| - |
26 |
| -// When passed general object |
27 |
| -export function object<T>(object: T): DoubledObject<T>; |
| 13 | +interface Call { |
| 14 | + context: {}; |
| 15 | + args: any[]; |
| 16 | +} |
28 | 17 |
|
29 |
| -export interface Stubber { |
30 |
| - thenReturn(...args: any[]): TestDouble; |
31 |
| - thenDo(f: Function): TestDouble; |
32 |
| - thenThrow(e: Error): TestDouble; |
33 |
| - thenResolve(...args: any[]): TestDouble; |
34 |
| - thenReject(e: Error): TestDouble; |
35 |
| - thenCallback(...args: any[]): TestDouble; |
| 18 | +interface Constructor<T> { |
| 19 | + new (...args: any[]): T |
36 | 20 | }
|
37 | 21 |
|
38 |
| -export function callback(...args: any[]): void; |
| 22 | +export interface Captor { |
| 23 | + capture(): any; |
| 24 | + value?: any; |
| 25 | + values?: any[]; |
| 26 | +} |
39 | 27 |
|
40 |
| -export function when(...args: any[]): Stubber; |
| 28 | +export interface Explanation { |
| 29 | + callCount: number; |
| 30 | + calls: Call[]; |
| 31 | + description: string; |
| 32 | +} |
41 | 33 |
|
42 | 34 | export interface Matchers {
|
43 | 35 | anything(): any;
|
44 | 36 | isA(type: Function): any;
|
45 |
| - contains(a: string|any[]|{}): any; |
| 37 | + contains(a: string | any[] | {}): any; |
46 | 38 | argThat(matcher: Function): any;
|
47 | 39 | not(v: any): any;
|
48 | 40 | captor(): Captor
|
49 | 41 | }
|
50 | 42 |
|
51 |
| -export interface Captor { |
52 |
| - capture(): any; |
53 |
| - value?: any; |
54 |
| - values?: any[]; |
55 |
| -} |
56 |
| - |
57 | 43 | export const matchers: Matchers;
|
58 | 44 |
|
59 |
| -export function replace(path: string, f?: any): any; |
60 |
| -export function replace(path: {}, property: string, f?: any): any; |
| 45 | +export interface Stubber { |
| 46 | + thenReturn<T>(...args: any[]): TestDouble<T>; |
| 47 | + thenDo<T>(f: Function): TestDouble<T>; |
| 48 | + thenThrow<T>(e: Error): TestDouble<T>; |
| 49 | + thenResolve<T>(...args: any[]): TestDouble<T>; |
| 50 | + thenReject<T>(e: Error): TestDouble<T>; |
| 51 | + thenCallback<T>(...args: any[]): TestDouble<T>; |
| 52 | +} |
61 | 53 |
|
62 |
| -export function reset(): void; |
| 54 | +export interface TestdoubleConfig { |
| 55 | + promiseConstructor?: any; |
| 56 | + ignoreWarnings?: boolean; |
| 57 | + suppressErrors?: boolean; |
| 58 | +} |
63 | 59 |
|
64 | 60 | export interface VerificationConfig {
|
65 | 61 | ignoreExtraArgs?: boolean;
|
66 | 62 | times?: number;
|
67 | 63 | }
|
68 | 64 |
|
69 |
| -export function verify(a: any, check?: VerificationConfig): void; |
| 65 | +// |
| 66 | +// general |
| 67 | +// ---------------------------------------------------------------------------- |
| 68 | + |
| 69 | +/** |
| 70 | + * Update the configuration. Configuration will perist through the lifetime of |
| 71 | + * of the entire test. If you need to change a configuration property for a |
| 72 | + * single test, you'll need to manage undoing the change yourself (e.g. in |
| 73 | + * beforeEach and afterEach hooks). |
| 74 | + * |
| 75 | + * @export |
| 76 | + * @param {TestdoubleConfig} config |
| 77 | + */ |
| 78 | +export function config(config: TestdoubleConfig): void; |
70 | 79 |
|
71 |
| -interface Call { |
72 |
| - context: {}; |
73 |
| - args: any[]; |
74 |
| -} |
| 80 | +/** |
| 81 | + * Reset the state. |
| 82 | + * |
| 83 | + * @export |
| 84 | + */ |
| 85 | +export function reset(): void; |
75 | 86 |
|
76 |
| -export interface Explanation { |
77 |
| - callCount: number; |
78 |
| - calls: Call[]; |
79 |
| - description: string; |
80 |
| -} |
| 87 | +/** |
| 88 | + * Takes a test double function as an argument and will describe the current |
| 89 | + * configuration and state of the test double. |
| 90 | + * |
| 91 | + * @export |
| 92 | + * @template T |
| 93 | + * @param {TestDouble<T>} f |
| 94 | + * @returns {Explanation} |
| 95 | + */ |
| 96 | +export function explain<T>(f: TestDouble<T>): Explanation; |
| 97 | + |
| 98 | + |
| 99 | +// |
| 100 | +// fake: constructors |
| 101 | +// ---------------------------------------------------------------------------- |
| 102 | + |
| 103 | +/** |
| 104 | + * Create a fake object constructor the given class. |
| 105 | + * |
| 106 | + * @export |
| 107 | + * @template T |
| 108 | + * @param {{ new (...args: any[]): T }} constructor |
| 109 | + * @returns {DoubledObject<T>} |
| 110 | + */ |
| 111 | +export function constructor<T>(constructor: Constructor<T>): TestDoubleConstructor<T>; |
| 112 | + |
| 113 | +// |
| 114 | +// fake: functions |
| 115 | +// ---------------------------------------------------------------------------- |
| 116 | + |
| 117 | +/** |
| 118 | + * Create a fake function. |
| 119 | + * |
| 120 | + * @param {string} [name] Name of function for better messages. |
| 121 | + * @returns {TestDouble<Function>} |
| 122 | + */ |
| 123 | +declare function functionDouble(name?: string): TestDouble<Function>; |
| 124 | + |
| 125 | +/** |
| 126 | + * Create a fake function. |
| 127 | + * |
| 128 | + * @template T |
| 129 | + * @param {T} [name] Name of function to copy. |
| 130 | + * @returns {TestDouble<T>} |
| 131 | + */ |
| 132 | +declare function functionDouble<T>(name?: T): TestDouble<T>; |
81 | 133 |
|
82 |
| -export function explain(f: TestDouble): Explanation; |
| 134 | +export { functionDouble as function }; |
| 135 | +export { functionDouble as func }; |
| 136 | + |
| 137 | +// |
| 138 | +// fake: objects |
| 139 | +// ---------------------------------------------------------------------------- |
| 140 | + |
| 141 | +/** |
| 142 | + * Create a fake object that is deep copy of the given object. |
| 143 | + * |
| 144 | + * @export |
| 145 | + * @template T |
| 146 | + * @param {{ new (...args: any[]): T }} constructor |
| 147 | + * @returns {DoubledObject<T>} |
| 148 | + */ |
| 149 | +export function object<T>(constructor: Constructor<T> ): DoubledObject<T>; |
| 150 | + |
| 151 | +/** |
| 152 | + * Create a fake object that has the given list of properties. |
| 153 | + * |
| 154 | + * @export |
| 155 | + * @template Key |
| 156 | + * @param {Key[]} props Array of properties. |
| 157 | + * @returns {DoubledObjectWithKey<Key>} |
| 158 | + */ |
| 159 | +export function object<T extends string>(props: T[]): DoubledObjectWithKey<T>; |
| 160 | + |
| 161 | +/** |
| 162 | + * Create a fake empty object that is cast as the generic using a Proxy object. |
| 163 | + * |
| 164 | + * @export |
| 165 | + * @template T |
| 166 | + * @param {T} object Name of object. |
| 167 | + * @returns {DoubledObject<T>} |
| 168 | + */ |
| 169 | +export function object<T>(object: string): DoubledObject<T>; |
| 170 | + |
| 171 | +/** |
| 172 | + * Create a fake object that is deep copy of the given object. |
| 173 | + * |
| 174 | + * @export |
| 175 | + * @template T |
| 176 | + * @param {T} object Object to copy. |
| 177 | + * @returns {DoubledObject<T>} |
| 178 | + */ |
| 179 | +export function object<T>(object: T): DoubledObject<T>; |
| 180 | + |
| 181 | +// |
| 182 | +// stubbing |
| 183 | +// ---------------------------------------------------------------------------- |
| 184 | + |
| 185 | + |
| 186 | +/** |
| 187 | + * Callback marker. |
| 188 | + * |
| 189 | + * @export |
| 190 | + * @param {...any[]} args |
| 191 | + */ |
| 192 | +export function callback(...args: any[]): void; |
| 193 | + |
| 194 | + |
| 195 | +/** |
| 196 | + * Swap out real dependenencies with fake one. Intercept calls to `require` |
| 197 | + * that dependency module and ensure your subject is handed a fake instead. |
| 198 | + * |
| 199 | + * @export |
| 200 | + * @param {string} path |
| 201 | + * @param {*} [f] |
| 202 | + * @returns {*} |
| 203 | + */ |
| 204 | +export function replace(path: string, f?: any): any; |
| 205 | + |
| 206 | +/** |
| 207 | + * Swap out real dependenencies with fake one. Reference to the property will |
| 208 | + * be replace it during your test. |
| 209 | + * |
| 210 | + * @export |
| 211 | + * @param {{}} path |
| 212 | + * @param {string} property |
| 213 | + * @param {*} [f] |
| 214 | + * @returns {*} |
| 215 | + */ |
| 216 | +export function replace(path: {}, property: string, f?: any): any; |
| 217 | + |
| 218 | + |
| 219 | +/** |
| 220 | + * Stub a specific function call. |
| 221 | + * |
| 222 | + * @export |
| 223 | + * @param {...any[]} args |
| 224 | + * @returns {Stubber} |
| 225 | + */ |
| 226 | +export function when(...args: any[]): Stubber; |
| 227 | + |
| 228 | +/** |
| 229 | + * Verify a specific function call to a stubbed function. |
| 230 | + * |
| 231 | + * @export |
| 232 | + * @param {...any[]} args |
| 233 | + * @returns {Stubber} |
| 234 | + */ |
| 235 | +export function verify(a: any, check?: VerificationConfig): void; |
0 commit comments