@slimlib/smart-mock 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Yet another proxy mock (YAPM?). Still in a very early state (EXPECT BUGS!).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A mock that records operations for code generation later. The idea is somewhat similar to `prepack`, but instead of interpreting code by other JS code, we run it in a JS VM and later use the mock to repeat the same operations. Ideally combined with a terser-like optimizer. Please check the example in `pkgbld` to see how it is used to eject config.
|
|
6
6
|
|
|
7
7
|
[Changelog](./CHANGELOG.md)
|
|
8
8
|
|
|
@@ -10,11 +10,11 @@ Mock that records operations for code generation later. Idea is somewhat similar
|
|
|
10
10
|
|
|
11
11
|
### default `() => { createMock, generateGlobals, generate }`
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
The default export function is a factory that creates 3 other functions with shared state.
|
|
14
14
|
|
|
15
15
|
### `createMock<T extends object>(object: T, name: string): T`
|
|
16
16
|
|
|
17
|
-
Function to create mock wrapper around object and
|
|
17
|
+
Function to create a mock wrapper around an object and define a global name for later usage. `object` can be a real original object or a pure mock object with the same behavior for the specific situation. All operations on this object will be recorded by the mock.
|
|
18
18
|
|
|
19
19
|
### `generate(object: unknown): string`
|
|
20
20
|
|
|
@@ -22,19 +22,24 @@ Function to generate code for some export point (exit point). It will try to aut
|
|
|
22
22
|
|
|
23
23
|
### `generateGlobals(): string`
|
|
24
24
|
|
|
25
|
-
Function to generate global code that cannot be inlined to exit point.
|
|
25
|
+
Function to generate global code that cannot be inlined to an exit point.
|
|
26
26
|
|
|
27
27
|
### Example
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
|
-
import createMockProvider from
|
|
30
|
+
import createMockProvider from "@slimlib/smart-mock";
|
|
31
31
|
const { createMock, generate, generateGlobals } = createMockProvider();
|
|
32
|
-
const mock = createMock(
|
|
33
|
-
|
|
32
|
+
const mock = createMock(
|
|
33
|
+
{
|
|
34
|
+
fly() {
|
|
35
|
+
return { status: "flying" };
|
|
36
|
+
},
|
|
34
37
|
land() {},
|
|
35
|
-
name:
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
+
name: "",
|
|
39
|
+
},
|
|
40
|
+
"fly"
|
|
41
|
+
);
|
|
42
|
+
mock.name = "Moth";
|
|
38
43
|
const status = mock.fly();
|
|
39
44
|
mock.land();
|
|
40
45
|
```
|
|
@@ -42,12 +47,12 @@ mock.land();
|
|
|
42
47
|
At this point `generate(mock)` will result in `mock`, `generate(mock.name)` in `'Moth'` and `generate(status)` in `fly.fly()`. And if you afterwards call `generateGlobals()` you get something like:
|
|
43
48
|
|
|
44
49
|
```javascript
|
|
45
|
-
fly.name = "Moth"
|
|
46
|
-
const tmp_0 = fly.land
|
|
47
|
-
tmp_0()
|
|
50
|
+
fly.name = "Moth";
|
|
51
|
+
const tmp_0 = fly.land;
|
|
52
|
+
tmp_0();
|
|
48
53
|
```
|
|
49
54
|
|
|
50
|
-
Each `generate` call updates counters / flags in mock so `generateGlobals` only emits what was not generated at the time of call.
|
|
55
|
+
Each `generate` call updates counters / flags in the mock so `generateGlobals` only emits what was not generated at the time of the call.
|
|
51
56
|
|
|
52
57
|
# License
|
|
53
58
|
|