Skip to content

Commit d478907

Browse files
p16Fdawgs
andauthored
docs: Add documentation for reply accepting a function as the data parameter (nodejs#1456)
* Add a small paragraph documenting reply accept a function as the data parameter * Update docs/best-practices/mocking-request.md Co-authored-by: Frazer Smith <[email protected]> Co-authored-by: Frazer Smith <[email protected]>
1 parent c4e6775 commit d478907

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/best-practices/mocking-request.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,36 @@ const badRequest = await bankTransfer('1234567890', '100')
101101
// subsequent request to origin http://localhost:3000 was not allowed (net.connect disabled)
102102
```
103103

104+
## Reply with data based on request
104105

106+
If the mocked response needs to be dynamically derived from the request parameters, you can provide a function instead of an object to `reply`
107+
108+
```js
109+
mockPool.intercept({
110+
path: '/bank-transfer',
111+
method: 'POST',
112+
headers: {
113+
'X-TOKEN-SECRET': 'SuperSecretToken',
114+
},
115+
body: JSON.stringify({
116+
recepient: '1234567890',
117+
amount: '100'
118+
})
119+
}).reply(200, (opts) => {
120+
// do something with opts
121+
122+
return { message: 'transaction processed' }
123+
})
124+
```
125+
126+
in this case opts will be
127+
128+
```
129+
{
130+
method: 'POST',
131+
headers: { 'X-TOKEN-SECRET': 'SuperSecretToken' },
132+
body: '{"recepient":"1234567890","amount":"100"}',
133+
origin: 'http://localhost:3000',
134+
path: '/bank-transfer'
135+
}
136+
```

0 commit comments

Comments
 (0)