Skip to content

Commit 37f5ab6

Browse files
feat: resetMock, more exposed internals, TS generics
1 parent 64be1bd commit 37f5ab6

26 files changed

+846
-503
lines changed

README.md

+42-5
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121
- [`hasPathBeenCalledWith(proxy, path, args) => boolean`](#haspathbeencalledwithproxy-path-args--boolean)
2222
- [`getVisitedPathData(proxy, path) => ProxyData[] | null`](#getvisitedpathdataproxy-path--proxydata--null)
2323
- [ProxyData](#proxydata)
24+
- [`resetMock(proxy)`](#resetmockproxy)
2425
- [`replayProxy(proxy, target)`](#replayproxyproxy-target)
2526
- [`listAllProxyOperations(proxy) => ProxyData[]`](#listallproxyoperationsproxy--proxydata)
27+
- [`listAllProxyPaths(proxy) => ProxyPath[]`](#listallproxypathsproxy--proxypath)
2628
- [ProxyPath](#proxypath)
2729
- [Caveats](#caveats)
2830
- [More examples](#more-examples)
2931
- [Mock HTML5 Canvas in JSDOM](#mock-html5-canvas-in-jsdom)
32+
- [Mock a dependency using Jest](#mock-a-dependency-using-jest)
3033
- [Mock an entire library](#mock-an-entire-library)
3134
- [Mock complex objects](#mock-complex-objects)
3235
- [Browser/Node Support](#browsernode-support)
3336
- [Performance & Size](#performance--size)
3437

3538
## About
3639

37-
Have you ever wanted to mock something that has lots of nested properties and functions? You don't need all of those to be implemented. You just want them to exist so the code doesn't crash. This is the solution.
40+
This is an easy-to-use library which enables you to instantly mock anything. Any properties, functions, classes, etc will be instantly mocked with one line. Useful when you need to provide a mock, but don't care about the implementation. With this library you can then do many other advanced things such as: overriding certain operations, inspect what operations occurred, replay all operations onto another object, and more!
3841

3942
Recursive Proxy Mock is a [JavaScript Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) that can handle literally anything. This is best explained with examples. Read on!
4043

@@ -46,9 +49,9 @@ npm install --save-dev recursive-proxy-mock
4649

4750
```js
4851
import { recursiveProxyMock } from "recursive-proxy-mock";
49-
```
5052

51-
```js
53+
// OR
54+
5255
const { recursiveProxyMock } = require("recursive-proxy-mock");
5356
```
5457

@@ -253,6 +256,12 @@ A `ProxyData` object contains any relevant details about the operation. For exam
253256
- All other handlers:
254257
- No useful additional information is available
255258

259+
### `resetMock(proxy)`
260+
261+
Resets the internally tracked proxy operations. See the [Mock a dependency using Jest](#mock-a-dependency-using-jest) example for a common usage of this method.
262+
263+
- `proxy` - the root proxy object that was returned from `recursiveProxyMock`
264+
256265
### `replayProxy(proxy, target)`
257266

258267
Replay every operation performed on a proxy mock object onto a target object. This can effectively let you time travel to queue up any actions and replay them as many times as you would like. Every property accessor, every function call, etc will be replayed onto the target.
@@ -269,6 +278,13 @@ This is exposed primarily for debugging or curiosity and shouldn't be relied on.
269278
- `proxy` - the root proxy object that was returned from `recursiveProxyMock`
270279
- Returns: Array of [ProxyData](#proxydata) objects for every operation that was performed on the mock.
271280

281+
### `listAllProxyPaths(proxy) => ProxyPath[]`
282+
283+
A debug function which lists every path and sub-path that was visited on the mock. This is an array of [ProxyPath](#proxypath) arrays which is useful to manually inspect what operations took place and find the correct paths to use for the other APIs.
284+
285+
- `proxy` - the root proxy object that was returned from `recursiveProxyMock`
286+
- Returns: Array of [ProxyPath](#proxypath) arrays with one entry for every path or sub-path that was visited.
287+
272288
### ProxyPath
273289

274290
Whenever a method accepts a `path` it is an array of properties and symbols to define a request path on the mock object. (See [ProxySymbol](#proxysymbol) for more details.)
@@ -291,13 +307,34 @@ Whenever a method accepts a `path` it is an array of properties and symbols to d
291307
JSDOM doesn't implement the Canvas element so if you are testing code that is drawing on a canvas, it'll crash as soon as it tries to interact with the canvas context. You can use `recursiveProxyMock` to mock every method/property on the context for both 2d and WebGL. None of them will do anything, but the code will no longer crash and you can assert that the required functions were called.
292308

293309
```ts
294-
import { recursiveProxyMock } from "recursive-proxy-mock";
310+
import { recursiveProxyMock, hasPathBeenVisited, ProxySymbol } from "recursive-proxy-mock";
311+
312+
const mock = recursiveProxyMock();
295313

296-
global.HTMLCanvasElement.prototype.getContext = recursiveProxyMock();
314+
global.HTMLCanvasElement.prototype.getContext = () => mock;
297315

298316
const canvas = document.createElement("canvas");
299317
const context = canvas.getContext("webgl"); // JSDOM doesn't implement this
300318
context.clear(context.COLOR_BUFFER_BIT); // This would normally crash
319+
320+
// Check that `context.clear` has been called
321+
hasPathBeenVisited(mock, ["clear", ProxySymbol.APPLY]);
322+
```
323+
324+
### Mock a dependency using Jest
325+
326+
```ts
327+
import { recursiveProxyMock, resetMock } from "recursive-proxy-mock";
328+
329+
const mockInstance = recursiveProxyMock();
330+
331+
beforeEach(() => {
332+
resetMock(mockInstance);
333+
});
334+
335+
jest.doMock("my-dependency", () => {
336+
return mockInstance;
337+
});
301338
```
302339

303340
### Mock an entire library

cspell.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
{
22
"version": "0.1",
3-
"words": ["bundlephobia", "chainable", "commitlint", "corejs", "eqeqeq", "iife", "lcov", "ttypescript", "webgl"],
3+
"words": [
4+
"bundlephobia",
5+
"chainable",
6+
"commitlint",
7+
"corejs",
8+
"eqeqeq",
9+
"filesize",
10+
"iife",
11+
"lcov",
12+
"ttypescript",
13+
"webgl"
14+
],
415
"ignoreRegExpList": ["\\(#.*\\)"],
516
"ignorePaths": ["/coverage", "/dist", "package-lock.json", "package.json"]
617
}

0 commit comments

Comments
 (0)