Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: fix documented mock request example #1341

Merged
merged 1 commit into from Apr 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions docs/best-practices/mocking-request.md
Expand Up @@ -5,17 +5,20 @@ Undici have its own mocking [utility](../api/MockAgent.md). It allow us to inter
Example:

```js
// index.mjs
// bank.mjs
mcollina marked this conversation as resolved.
Show resolved Hide resolved
import { request } from 'undici'

export async function bankTransfer(recepient, ammount) {
const { body } = await request('http://localhost:3000/bank-transfer',
export async function bankTransfer(recepient, amount) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo correction ammount -> amount

const { body } = await request('http://localhost:3000/bank-transfer',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the undici .editorconfig, trimming trailing whitespace is the preferred style.

{
method: 'POST',
headers: {
'X-TOKEN-SECRET': 'SuperSecretToken',
},
body: JSON.stringify({ recepient })
body: JSON.stringify({
recepient,
amount
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the real fix. amount wasn't being included in the body and caused the first assertion to fail.

})
}
)
return await body.json()
Expand All @@ -28,7 +31,7 @@ And this is what the test file looks like:
// index.test.mjs
import { strict as assert } from 'assert'
import { MockAgent, setGlobalDispatcher, } from 'undici'
import { bankTransfer } from './undici.mjs'
import { bankTransfer } from './bank.mjs'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const mockAgent = new MockAgent();

Expand All @@ -46,7 +49,7 @@ mockPool.intercept({
},
body: JSON.stringify({
recepient: '1234567890',
ammount: '100'
amount: '100'
})
}).reply(200, {
message: 'transaction processed'
Expand Down Expand Up @@ -94,7 +97,7 @@ mockPool.intercept({

const badRequest = await bankTransfer('1234567890', '100')
// Will throw an error
// MockNotMatchedError: Mock dispatch not matched for path '/bank-transfer':
// MockNotMatchedError: Mock dispatch not matched for path '/bank-transfer':
// subsequent request to origin http://localhost:3000 was not allowed (net.connect disabled)
```

Expand Down