Skip to content

Commit 996c437

Browse files
committed
tests: fix
1 parent a834118 commit 996c437

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

test/index.test.js

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { promisify } = require('util')
33

44
const tap = require('tap')
55
const fastify = require('fastify')
6-
const { request } = require('undici')
6+
const { request, Client } = require('undici')
77

88
const plugin = require('../.')
99
const { Errors } = require('../lib')
@@ -85,8 +85,6 @@ tap.test('fastify-racing#decoration', subtest => {
8585
)
8686
})
8787

88-
// TODO: find what's hanging the tests
89-
// TODO: remove "only" once done
9088
tap.test('fastify-racing#promise', { only: true }, subtest => {
9189
subtest.plan(4)
9290

@@ -113,7 +111,7 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
113111

114112
app
115113
.ready()
116-
.then(() => app.listen())
114+
.then(() => app.listen({ port: 0 }))
117115
.then(async () => {
118116
request(
119117
`http://localhost:${app.server.address().port}`,
@@ -196,7 +194,7 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
196194

197195
app
198196
.ready()
199-
.then(() => app.listen())
197+
.then(() => app.listen({ port: 0 }))
200198
.then(async () => {
201199
request(
202200
`http://localhost:${app.server.address().port}`,
@@ -220,9 +218,14 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
220218
subtest.test(
221219
'Should reuse AbortController for the single request',
222220
async t => {
223-
let first
221+
// eslint-disable-next-line
222+
let first, client
224223
const app = fastify()
225224

225+
t.teardown(async () => {
226+
await client.destroy()
227+
await app.close()
228+
})
226229
t.plan(5)
227230

228231
app.register(plugin)
@@ -243,79 +246,81 @@ tap.test('fastify-racing#promise', { only: true }, subtest => {
243246
t.notOk(second.aborted)
244247
t.equal(second, first, 'Should reuse the initial controller')
245248

246-
return 'Hello World'
249+
_reply.send('Hello World')
247250
}
248251
)
249252

250-
t.teardown(() => app.close())
253+
await app.listen({ port: 0 })
251254

252-
await app.listen()
255+
client = new Client(`http://localhost:${app.server.address().port}`)
253256

254-
const response = await request(
255-
`http://localhost:${app.server.address().port}`,
256-
{
257-
method: 'GET',
258-
path: '/'
259-
}
260-
)
257+
const response = await client.request({
258+
method: 'GET',
259+
path: '/'
260+
})
261+
262+
const responseBody = await response.body.text()
261263

262264
t.equal(response.statusCode, 200)
263-
t.equal(await response.body.text(), 'Hello World')
264-
t.end()
265+
t.equal(responseBody, 'Hello World')
265266
}
266267
)
267268

268269
// TODO: Find how to close the socket after request finished
269-
subtest.test(
270-
'Should throw on already closed request',
271-
async t => {
272-
let first
273-
const app = fastify()
270+
subtest.test('Should throw on already closed request', async t => {
271+
// eslint-disable-next-line
272+
let first, client
273+
const app = fastify()
274274

275-
t.plan(7)
275+
t.teardown(async () => {
276+
await client.destroy()
277+
await app.close()
278+
})
276279

277-
app.register(plugin)
280+
t.plan(7)
278281

279-
app.get(
280-
'/',
281-
{
282-
onResponse: async (req, _reply, done) => {
283-
req.raw.destroy()
282+
app.register(plugin)
284283

285-
try {
286-
first = await req.race()
287-
} catch (err) {
288-
t.ok(err)
289-
t.ok(err instanceof Errors.SOCKET_CLOSED)
290-
t.equal(err.code, 'FST_PLUGIN_RACE_SOCKET_CLOSED')
291-
t.equal(err.statusCode, 500)
292-
}
284+
app.get(
285+
'/',
286+
{
287+
onResponse: async (req, _reply, done) => {
288+
req.raw.destroy()
293289

294-
t.notOk(first)
295-
done()
290+
try {
291+
first = await req.race()
292+
} catch (err) {
293+
t.ok(err)
294+
t.ok(err instanceof Errors.SOCKET_CLOSED)
295+
t.equal(err.code, 'FST_PLUGIN_RACE_SOCKET_CLOSED')
296+
t.equal(err.statusCode, 500)
296297
}
297-
},
298-
(req, _reply) => {
299-
return 'Hello World'
298+
299+
t.notOk(first)
300+
done()
300301
}
301-
)
302+
},
303+
(req, _reply) => {
304+
return 'Hello World'
305+
}
306+
)
302307

303-
t.teardown(() => app.close())
308+
t.teardown(() => app.close())
304309

305-
await app.listen()
310+
await app.listen({ port: 0 })
306311

307-
const response = await request(
308-
`http://localhost:${app.server.address().port}`,
309-
{
310-
method: 'GET',
311-
path: '/'
312-
}
313-
)
312+
client = new Client(`http://localhost:${app.server.address().port}`)
314313

315-
t.equal(response.statusCode, 200)
316-
t.equal(await response.body.text(), 'Hello World')
317-
}
318-
)
314+
const response = await client.request(
315+
{
316+
method: 'GET',
317+
path: '/'
318+
}
319+
)
320+
321+
t.equal(response.statusCode, 200)
322+
t.equal(await response.body.text(), 'Hello World')
323+
})
319324

320325
async function dummy (signal, ms = 3000) {
321326
await sleep(ms, null, { signal, ref: false })

0 commit comments

Comments
 (0)