Skip to content

Commit

Permalink
docs: add test examples with undici and fetch (#4300)
Browse files Browse the repository at this point in the history
Co-authored-by: Cristian Teodorescu <87984709+CristianTeodorescu@users.noreply.github.com>
  • Loading branch information
Cristian Teodorescu and CristianTeodorescu committed Oct 6, 2022
1 parent a4bb8ad commit 62d2c94
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions docs/Guides/Testing.md
Expand Up @@ -243,33 +243,66 @@ after initializing routes and plugins with `fastify.ready()`.

Uses **app.js** from the previous example.

**test-listen.js** (testing with
[`Request`](https://www.npmjs.com/package/request))
**test-listen.js** (testing with [`undici`](https://www.npmjs.com/package/undici))
```js
const tap = require('tap')
const request = require('request')
const { Client } = require('undici')
const buildFastify = require('./app')

tap.test('GET `/` route', t => {
t.plan(5)
tap.test('should work with undici', async t => {
t.plan(2)

const fastify = buildFastify()

t.teardown(() => fastify.close())
await fastify.listen()

fastify.listen({ port: 0 }, (err) => {
t.error(err)
const client = new Client(
'http://localhost:' + fastify.server.address().port, {
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10
}
)

request({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
t.same(JSON.parse(body), { hello: 'world' })
})
t.teardown(() => {
fastify.close()
client.close()
})

const response = await client.request({ method: 'GET', path: '/' })

t.equal(await response.body.text(), '{"hello":"world"}')
t.equal(response.statusCode, 200)
})
```

Alternatively, starting with Node.js 18,
[`fetch`](https://nodejs.org/docs/latest-v18.x/api/globals.html#fetch)
may be used without requiring any extra dependencies:

**test-listen.js**
```js
const tap = require('tap')
const buildFastify = require('./app')

tap.test('should work with fetch', async t => {
t.plan(3)

const fastify = buildFastify()

t.teardown(() => fastify.close())

await fastify.listen()

const response = await fetch(
'http://localhost:' + fastify.server.address().port
)

t.equal(response.status, 200)
t.equal(
response.headers.get('content-type'),
'application/json; charset=utf-8'
)
t.has(await response.json(), { hello: 'world' })
})
```

Expand Down

0 comments on commit 62d2c94

Please sign in to comment.