From 4a44a6b2a3d991aedae52fbcf3ca107143dcfd61 Mon Sep 17 00:00:00 2001 From: Austin Kelleher Date: Sat, 16 Apr 2022 12:07:26 -0400 Subject: [PATCH] doc: fix documented mock request example --- docs/best-practices/mocking-request.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/docs/best-practices/mocking-request.md b/docs/best-practices/mocking-request.md index 0182d249b8e..ebcc90d24b7 100644 --- a/docs/best-practices/mocking-request.md +++ b/docs/best-practices/mocking-request.md @@ -5,17 +5,20 @@ Undici have its own mocking [utility](../api/MockAgent.md). It allow us to inter Example: ```js -// index.mjs +// bank.mjs 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) { + const { body } = await request('http://localhost:3000/bank-transfer', { method: 'POST', headers: { 'X-TOKEN-SECRET': 'SuperSecretToken', }, - body: JSON.stringify({ recepient }) + body: JSON.stringify({ + recepient, + amount + }) } ) return await body.json() @@ -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' const mockAgent = new MockAgent(); @@ -46,7 +49,7 @@ mockPool.intercept({ }, body: JSON.stringify({ recepient: '1234567890', - ammount: '100' + amount: '100' }) }).reply(200, { message: 'transaction processed' @@ -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) ```