Repository navigation
Expand file tree
/
Copy pathrewiremock.d.ts
More file actions
266 lines (215 loc) · 7.42 KB
/
Copy pathrewiremock.d.ts
File metadata and controls
266 lines (215 loc) · 7.42 KB
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
declare module 'rewiremock' {
type Plugin = any;
type PluginNames = 'childOnly' | 'nodejs' | 'protectNodeModules' | 'relative' | 'webpackAlias' | 'toBeUsed' | 'disabledByDefault' | 'mockThroughByDefault' | 'usedByDefault' | 'alwaysMatchOrigin' | 'directChild';
type Plugins = {
[Key in PluginNames]: any
};
interface OverloadedModule {
name: String,
fileName: String,
parent: Object,
original: Object,
requireActual: Function
}
type IStubFactory = (name: string, value: any) => any;
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,
}
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;
}
interface HasDefault {
default: any
}
interface DefaultModuleMock<T extends HasDefault> extends NamedModuleMock<T> {
/**
* Setting es6 behavior for a current module and overriding default export
*/
withDefault(fn: T['default']): this;
}
interface AnyModuleMock {
/**
* 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;
}
type ModuleMock = AnyModuleMock;
type ProxyFunction = (r: ModuleMock) => Object;
type RequireFunction<T> = () => T;
type ImportFunction<T> = () => Promise<T>;
type AnyImportFunction<T> = RequireFunction<T> | ImportFunction<T>;
/**
* @name rewiremock
* @class
* Proxies imports/require in order to allow overriding dependencies during testing.
*/
interface rewiremock {
(module: string): ModuleMock;
<T extends HasDefault>(module: ImportFunction<T>): DefaultModuleMock<T>
<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>
enable(): rewiremock;
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.
*/
forceCacheClear(): 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;
export default rewiremockdefault;
export function addPlugin(plugin:Plugin):void;
export function removePlugins(plugin:Plugin):void;
export function overrideEntryPoint(module:any):void;
export function resolveExtensions(extensions: string[]):void;
export var plugins: Plugins;
}