@@ -24,22 +24,22 @@ and magic of [proxyquire-webpack-alias](https://github.com/theKashey/proxyquire-
24
24
- be fast, to be faster.
25
25
26
26
# Goal:
27
- - give ability to mock everything
28
- - give ability to do correctly.
27
+ - give ability to mock everything - CommonJS, ES6, Webpack, anything.
28
+ - give ability to do correctly - isolation, typechecks, powerfull API
29
29
30
30
I have wrote some articles about these ideas - https://medium.com/tag/rewiremock/latest
31
31
32
32
# API
33
33
34
34
## main API
35
35
- rewiremock.enable() - wipes cache and enables interceptor.
36
- - rewiremock.disable() - wipes cache and disables interceptor.
37
- - rewiremock.inScope(callback) - place callback inside a sandbox.
38
- - rewiremock.around(loader, creator) - loads a module in an asynchronous sandbox.
39
- - rewiremock.proxy(file, stubs) - _ proxyquire_ like mocking api, where file is file name, and stubs are an object or a function.
40
- - rewiremock.module(loader, stubs) - async version of proxy, where loader is a function.
36
+ - rewiremock.disable() - wipes cache and disables interceptor.
37
+ - rewiremock.around(loader, creator): Promise < T > - loads a module in an ** asynchronous** sandbox.
38
+ - rewiremock.proxy(file, stubs): T - _ proxyquire_ like mocking api, where file is file name, and stubs are an object or a function.
39
+ - rewiremock.module(loader, stubs): Promise < T > - async version of proxy, where loader is a function.
41
40
## mocking API
42
- - rewiremock(moduleName: string) - set name of overloading module
41
+ - rewiremock(moduleName: string) - fabric for a moduleNames's mock
42
+ - rewiremock(moduleImport: loader) - async fabric for a module import function.
43
43
- .enable/disable() - to enable or disable mock (enabled by default).
44
44
- .with(stubs: function | Object) - overloads module with a value
45
45
- .withDefault(stub: function | Object) - overload ` default ` es6 export
@@ -52,20 +52,23 @@ I have wrote some articles about these ideas - https://medium.com/tag/rewiremock
52
52
- rewiremock.isolation() - enables isolation
53
53
- rewiremock.withoutIsolation() - disables isolation
54
54
- rewiremock.passBy(pattern or function) - enables some modules to pass thought isolation.
55
+ ## sandboxing
56
+ - rewiremock.inScope(callback) - place synchronous callback inside a sandbox.
55
57
56
58
# Which one?
57
59
Yep - there is 4 top level ways to activate a mock - inScope, around, proxy or just enable.
58
60
59
61
Which one to choose? Any! It just depends:
60
- - If everything is simply - use __ proxy__ .
61
- - If you have issues with name resolve - use __ module__ and resolve names by yourself.
62
- - If you need scope isolation - use __ around__ . inScope is just creating a scope and can be used in all cases.
62
+ - If everything is simply - use __ rewiremock.proxy__ .
63
+ - If you have issues with name resolve - use __ rewiremock.module__ and resolve names by yourself.
64
+ - If you need scope isolation - use __ rewiremock.around__ , or inScope.
65
+ - If you advanced syntax and type checking - use __ rewiremock.around__ .
63
66
- If you need full control - you will always have it.
64
- - As long all internal API will call __ .enable/.disable__ - I would not recommend using them directly .
67
+ - You always can just use __ .enable/.disable__ .
65
68
66
69
#Usage
67
70
``` js
68
- // 1. proxy will load a file by it's own ( name resolution is a hard thing)
71
+ // 1. proxy will load a file by it's own ( keep in mind - name resolution is a complex thing)
69
72
const mock = rewiremock .proxy (' somemodule' , (r ) => ({
70
73
' dep1' : { name: ' override' },
71
74
' dep2' : r .with ({name: ' override' }).toBeUsed ().directChildOnly () // use all `mocking API`
@@ -78,6 +81,7 @@ const mock = rewiremock.proxy(() => require('somemodule'), {
78
81
}));
79
82
80
83
// 3. or use es6 import (not for node.js mjs `real` es6 modules)
84
+ // PS: module is an async version of proxy, so you can use imports
81
85
const mock = await rewiremock .module (() => import (' somemodule' ), {
82
86
' dep1' : { name: ' override' },
83
87
' dep2' : { onlyDump: ' stubs' }
@@ -95,6 +99,49 @@ const mock = await rewiremock.around(() => import('somemodule'), () => {
95
99
rewiremock .disable ();
96
100
```
97
101
102
+ # Type safety
103
+ Rewiremock is able to provide a type-safe mocks. To enable type-safety follow these steps:
104
+ 1 . Use TypeScript or Flow.
105
+ 2 . Use dynamic import syntax.
106
+ 3 . Use rewiremock.around or rewiremock.module to perform a mock.
107
+ 4 . Use async form of rewiremock mock declaration.
108
+
109
+ ``` js
110
+ // @flow
111
+
112
+ import rewiremock from ' rewiremock' ;
113
+
114
+ rewiremock .around (
115
+ () => import (' ./a.js' ),
116
+ mock => {
117
+ mock (() => import (' ./b.js' ))
118
+ .withDefault (() => " 4" )
119
+ .with ({testB : () => 10 })
120
+ .nonStrict () // turn off type system
121
+ .with ({ absolutely: " anything" })
122
+ }
123
+ );
124
+ ```
125
+ If default export is not exists on module 'b', or there is no named export testB, or types do not match - type system will throw.
126
+
127
+ If you will declare an async mock, you it will not be resolved by the time of execution - Rewiremock will throw on Error.
128
+
129
+ If you have async imports inside mocked file, follow this syntax
130
+ ``` js
131
+ rewiremock .around (
132
+ () => import (' ./a.js' ),
133
+ mock => {
134
+ // just before loader function rewiremock enabled itself
135
+ mock (() => import (' ./b.js' ).then (mock => mock)) // mocks `live` one `tick` more
136
+ // just after loader function resolved rewiremock disables itself
137
+ mock => {
138
+ ... .
139
+ }
140
+ }
141
+ );
142
+ ```
143
+
144
+
98
145
# Setup
99
146
100
147
## To run with node.js
0 commit comments