Skip to content

Commit

Permalink
docs: Add documentation for reply accepting a function as the data pa…
Browse files Browse the repository at this point in the history
…rameter (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 <frazer.dev@outlook.com>

Co-authored-by: Frazer Smith <frazer.dev@outlook.com>
  • Loading branch information
2 people authored and metcoder95 committed Dec 26, 2022
1 parent 3a020df commit 8b90f47
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/best-practices/mocking-request.md
Expand Up @@ -101,4 +101,36 @@ const badRequest = await bankTransfer('1234567890', '100')
// subsequent request to origin http://localhost:3000 was not allowed (net.connect disabled)
```

## Reply with data based on request

If the mocked response needs to be dynamically derived from the request parameters, you can provide a function instead of an object to `reply`

```js
mockPool.intercept({
path: '/bank-transfer',
method: 'POST',
headers: {
'X-TOKEN-SECRET': 'SuperSecretToken',
},
body: JSON.stringify({
recepient: '1234567890',
amount: '100'
})
}).reply(200, (opts) => {
// do something with opts

return { message: 'transaction processed' }
})
```

in this case opts will be

```
{
method: 'POST',
headers: { 'X-TOKEN-SECRET': 'SuperSecretToken' },
body: '{"recepient":"1234567890","amount":"100"}',
origin: 'http://localhost:3000',
path: '/bank-transfer'
}
```

0 comments on commit 8b90f47

Please sign in to comment.