UNPKG

6.18 kBTypeScriptView Raw
1//
2// types and interfaces
3// ----------------------------------------------------------------------------
4
5export type DoubledObject < T > = T
6
7export type DoubledObjectWithKey < T extends string > = { [K in T] : any }
8
9export type TestDouble < T > = T
10
11export type TestDoubleConstructor < T > = Constructor<T>
12
13interface Call {
14 context: {};
15 args: any[];
16}
17
18interface Constructor<T> {
19 new (...args: any[]): T
20}
21
22export interface Captor {
23 capture(): any;
24 value?: any;
25 values?: any[];
26}
27
28export interface Explanation {
29 callCount: number;
30 calls: Call[];
31 description: string;
32 isTestDouble: boolean;
33}
34
35export interface Matchers {
36 anything(): any;
37 isA(type: Function): any;
38 contains(a: string | any[] | {}): any;
39 argThat(matcher: Function): any;
40 not(v: any): any;
41 captor(): Captor
42}
43
44export const matchers: Matchers
45
46export interface Stubber {
47 thenReturn<T>(...args: any[]): TestDouble<T>;
48 thenDo<T>(f: Function): TestDouble<T>;
49 thenThrow<T>(e: Error): TestDouble<T>;
50 thenResolve<T>(...args: any[]): TestDouble<T>;
51 thenReject<T>(e: Error): TestDouble<T>;
52 thenCallback<T>(...args: any[]): TestDouble<T>;
53}
54
55export interface TestdoubleConfig {
56 promiseConstructor?: any;
57 ignoreWarnings?: boolean;
58 suppressErrors?: boolean;
59}
60
61export interface VerificationConfig {
62 ignoreExtraArgs?: boolean;
63 times?: number;
64}
65
66//
67// general
68// ----------------------------------------------------------------------------
69
70/**
71 * Update the configuration. Configuration will perist through the lifetime of
72 * of the entire test. If you need to change a configuration property for a
73 * single test, you'll need to manage undoing the change yourself (e.g. in
74 * beforeEach and afterEach hooks).
75 *
76 * @export
77 * @param {TestdoubleConfig} config
78 */
79export function config(config: TestdoubleConfig): void;
80
81/**
82 * Reset the state.
83 *
84 * @export
85 */
86export function reset(): void;
87
88/**
89 * Takes a test double function as an argument and will describe the current
90 * configuration and state of the test double.
91 *
92 * @export
93 * @template T
94 * @param {TestDouble<T>} f
95 * @returns {Explanation}
96 */
97export function explain<T>(f: TestDouble<T>): Explanation;
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 */
111export 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 */
123declare function functionDouble(name?: string): TestDouble<Function>;
124
125/**
126 * Create a fake function. Typed when type is provided.
127 * @example td.func<MyType>();
128 * @template T
129 * @param {T} [name] Name of function to copy.
130 * @returns {TestDouble<T>}
131 */
132declare function functionDouble<T>(name?: T): TestDouble<T>;
133
134export { functionDouble as function }
135export { 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 */
149export 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 */
159export 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 */
169export function object<T>(object: string): DoubledObject<T>;
170
171/**
172 * Create a fake empty object using a Proxy object that is cast as the generic of a passed interface.
173 *
174 * @export
175 * @template T
176 * @returns {DoubledObject<T>}
177 */
178export function object<T>(): DoubledObject<T>;
179
180/**
181 * Create a fake object that is deep copy of the given object.
182 *
183 * @export
184 * @template T
185 * @param {T} object Object to copy.
186 * @returns {DoubledObject<T>}
187 */
188export function object<T>(object: T): DoubledObject<T>;
189
190// fake: imitations
191
192/**
193 * Create a fake object constructor for the given class.
194 *
195 * @export
196 * @template T
197 * @param {{ new (...args: any[]): T }} constructor
198 * @param {string} [name]
199 * @returns {TestDoubleConstructor<T>}
200 */
201export function imitate<T>(constructor: Constructor<T>, name?: string): TestDoubleConstructor<T>;
202
203/**
204 * Create a fake object or function.
205 *
206 * @export
207 * @template T
208 * @param {T} original
209 * @param {string} [name]
210 * @returns {TestDouble<T>}
211 */
212export function imitate<T>(original: T, name?: string): TestDouble<T>;
213
214//
215// stubbing
216// ----------------------------------------------------------------------------
217
218/**
219 * Callback marker.
220 *
221 * @export
222 * @param {...any[]} args
223 */
224export function callback(...args: any[]): void;
225
226/**
227 * Swap out real dependenencies with fake one. Intercept calls to `require`
228 * that dependency module and ensure your subject is handed a fake instead.
229 *
230 * @export
231 * @param {string} path
232 * @param {*} [f]
233 * @returns {*}
234 */
235export function replace(path: string, f?: any): any;
236
237/**
238 * Swap out real dependenencies with fake one. Reference to the property will
239 * be replace it during your test.
240 *
241 * @export
242 * @param {{}} path
243 * @param {string} property
244 * @param {*} [f]
245 * @returns {*}
246 */
247export function replace(path: {}, property: string, f?: any): any;
248
249/**
250 * Stub a specific function call.
251 *
252 * @export
253 * @param {...any[]} args
254 * @returns {Stubber}
255 */
256export function when(...args: any[]): Stubber;
257
258/**
259 * Verify a specific function call to a stubbed function.
260 *
261 * @export
262 * @param {...any[]} args
263 * @returns {Stubber}
264 */
265export function verify(a: any, check?: VerificationConfig): void;