-
Notifications
You must be signed in to change notification settings - Fork 30
/
rewiremock.d.ts
331 lines (274 loc) · 9.12 KB
/
rewiremock.d.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
declare module 'rewiremock' {
export type Plugin = any;
export type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'mockThroughByDefault' | 'usedByDefault' | 'alwaysMatchOrigin' | 'directChild';
export type Plugins = {
[Key in PluginNames]: any
};
export interface OverloadedModule {
name: String,
fileName: String,
parent: Object,
original: Object,
requireActual: Function
}
export type IStubFactory = (name: string, value: any) => any;
/**
* Base non-strict module export interface
*/
export interface BaseMock {
/**
* Enabled call thought original module, making all the original methods accessible.
* @example
* mock.callThrough();
*/
callThrough(): this,
/**
* Mimic the original file, replacing all the original methods by mocks.
* @param {IStubFactory} [stubFactory] - stub factory function
* @example
* mock.mockThrough();
* mock.mockThrough( () => sinon.stub() );
* mock.mockThrough( (name, value) => typeof value === 'function' ? sinon.stub() : value );
*/
mockThrough(stubFactory?: IStubFactory): this,
/**
* enables hot mock updates
* @return {this}
*/
dynamic(): this,
/**
* Setting es6 behaviour for a module
*/
es6(): this,
/**
* Overriding export of one module by another
* @example
* mock.by('otherModuleName');
*/
by(module: string): BaseMock,
/**
* Overriding export of one module by something generated by a function
* @example
* mock.by( originalModule => cache || cache = originalModule.requireActual('./nestedDep'));
*/
by(module: (module: OverloadedModule) => Object): BaseMock,
enable(): this,
disable(): this,
/**
* will mock this only first (directly nested) children.
*/
directChildOnly(): this;
/**
* will mocks this regardless of position
*/
atAnyPlace(): this;
/**
* mocks only if parent were mocked
*/
calledFromMock(): this;
calledFromAnywhere(): this;
/**
* Force mock to be used, or throw an error otherwise
*/
toBeUsed(): this,
notToBeUsed(): this,
/**
* checks mocks against implementation
* @return {this}
*/
toMatchOrigin(): this,
/**
* Bypass shouldMock and always mock
*/
always(): this,
}
/**
* Typed mock export interface
*/
export interface NamedModuleMock<T> extends BaseMock {
/**
* Overriding export of a module
*/
with(keys: {[P in keyof T]?: T[P]}): this;
/**
* Append overrides
*/
append(keys: {[P in keyof T]?: T[P]}): this;
/**
* Washes away the types
*/
nonStrict(): AnyModuleMock;
}
export interface HasDefault {
default: any
}
/**
* Module with default export mock export interface
*/
export interface DefaultModuleMock<T extends HasDefault> extends NamedModuleMock<T> {
/**
* Setting es6 behavior for a current module and overriding default export
*/
withDefault(fn: T['default']): this;
}
/**
* Non-strict mock export interface
*/
export interface AnyModuleMock extends BaseMock {
/**
* Setting es6 behavior for a current module and overriding default export
*/
withDefault(stubs: any): this;
/**
* Overriding export of a module
*/
with(stubs: any): this;
/**
* Append overrides
*/
append(stubs: any): this;
}
export type ModuleMock = AnyModuleMock;
export type ProxyFunction = (r: ModuleMock) => Object;
export type RequireFunction<T> = () => T;
export type ImportFunction<T> = () => Promise<T>;
export type AnyImportFunction<T> = RequireFunction<T> | ImportFunction<T>;
/**
* @name rewiremock
* @class
* Proxies imports/require in order to allow overriding dependencies during testing.
*/
export interface Rewiremock {
/**
* Define an overload for a given module
* @param {String} module
*/
(module: string): ModuleMock;
/**
* Define an overload for a given module with default export
* @param {Function} module
*/
<T extends HasDefault>(module: ImportFunction<T>): DefaultModuleMock<T>
/**
* Define an overload for a given module using import or require function
* @param {Function} module
*/
<T>(module: ImportFunction<T>): NamedModuleMock<T>
/**
* returns existing mock
* @return {"rewiremock".ModuleMock}
*/
getMock(module: string): ModuleMock;
getMock<T extends HasDefault>(module: ImportFunction<T>): DefaultModuleMock<T>
getMock<T>(module: ImportFunction<T>): NamedModuleMock<T>
/**
* Enables rewiremock and prepares module system (cleans cache)
*/
enable(): Rewiremock;
/**
* Disables rewiremock and cleans cache
*/
disable(): Rewiremock;
/**
* executes module in a sandbox
* @param {Function} loader - loader of target module. You can use import or require. May return a Promise
* @param {Function} [creator] - mock creator. You may add any mocks inside.
*/
around<T>(loader: AnyImportFunction<T>, creator?: (r: Rewiremock) => any): Promise<T>;
inScope(callback: Function): Rewiremock;
/**
* Loads a file and hooks deps in a `proxyquire` way
* @param {String|Function} fileName
* @param {Object|Function} overrides, with key==filename, and value==data
*/
proxy<T>(fileName: String | RequireFunction<T>, overrides?: Object | ProxyFunction): T;
/**
* Loads a file and hooks deps in a `proxyquire` way
* @param {Function} fileLoader. Require or Import desired module
* @param {Object} overrides, with key==filename, and value==data
*/
module<T>(fileLoader: ImportFunction<T>, overrides?: Object | ProxyFunction): Promise<T>;
flush(): void;
clear(): void;
/**
* Define stub factory for mockThrough command
* @param {IStubFactory} stubFactory
*/
stubFactory(stubFactory: IStubFactory): void;
/**
* converts module name
* @param module
*/
resolve(module: string): string,
/**
* Activates module isolation
* @param {Boolean} [options.noAutoPassBy] excludes mocked modules to a isolation scope. Use it with mock.callThrough.
* @param {Boolean} [options.noParentPassBy] disable allowing any module, with allowed parent
*/
isolation(options?: { noAutoPassBy?: boolean, noParentPassBy?: boolean}): Rewiremock;
/**
* Deactivates isolation
*/
withoutIsolation(): Rewiremock;
/**
* set aggressive politics to cache operation, restoring to the the previous values on end.
* false: (default) removes all new elements from the cache. Old data from "old" cache is transferred to a new one. New modules are kept.
* true: removes mocked modules from the cache. New modules are kept
* 'nocache': completely restores old modules
*/
forceCacheClear(mode?: boolean | 'nocache'): Rewiremock;
setCacheControl(enable: boolean): Rewiremock;
/**
* Adding new isolationpassby record
*/
passBy(pattern: any): Rewiremock;
/**
* Adds a plugin
*/
addPlugin(plugin: any): Rewiremock;
/**
* low-level require
*/
requireActual(fileName: string): any;
/**
* low-level import
*/
importActual(fileName: string): any;
/**
* low-level API override
*/
overrideEntryPoint(module:any): void;
}
var rewiremockdefault: Rewiremock;
/**
* rewiremock main export
* @example
* rewiremock('module').with({});
* rewiremock.enable();
*/
export default rewiremockdefault;
/**
* Adds a plugin
* @param plugin
*/
export function addPlugin(plugin:Plugin):void;
/**
* Removes a plugin
* @param plugin
*/
export function removePlugins(plugin:Plugin):void;
/**
* Sets given module as a top level parent
* @param module
*/
export function overrideEntryPoint(module:any):void;
/**
* Configures extensions to handle
* @param extensions
*/
export function resolveExtensions(extensions: string[]):void;
/**
* List of available plugins
*/
export var plugins: Plugins;
}