fastify 5.2.1 → 5.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/.vscode/settings.json +22 -0
  2. package/LICENSE +1 -1
  3. package/PROJECT_CHARTER.md +7 -7
  4. package/README.md +24 -25
  5. package/SPONSORS.md +2 -0
  6. package/docs/Guides/Benchmarking.md +4 -4
  7. package/docs/Guides/Database.md +1 -1
  8. package/docs/Guides/Delay-Accepting-Requests.md +10 -10
  9. package/docs/Guides/Ecosystem.md +5 -1
  10. package/docs/Guides/Fluent-Schema.md +1 -1
  11. package/docs/Guides/Getting-Started.md +9 -5
  12. package/docs/Guides/Index.md +1 -1
  13. package/docs/Guides/Migration-Guide-V4.md +1 -1
  14. package/docs/Guides/Migration-Guide-V5.md +12 -2
  15. package/docs/Guides/Plugins-Guide.md +6 -6
  16. package/docs/Guides/Serverless.md +14 -48
  17. package/docs/Guides/Style-Guide.md +2 -2
  18. package/docs/Guides/Testing.md +2 -2
  19. package/docs/Guides/Write-Plugin.md +2 -3
  20. package/docs/Reference/ContentTypeParser.md +58 -78
  21. package/docs/Reference/Decorators.md +50 -60
  22. package/docs/Reference/Encapsulation.md +28 -33
  23. package/docs/Reference/Errors.md +50 -53
  24. package/docs/Reference/HTTP2.md +7 -7
  25. package/docs/Reference/Hooks.md +31 -30
  26. package/docs/Reference/LTS.md +10 -15
  27. package/docs/Reference/Lifecycle.md +19 -24
  28. package/docs/Reference/Logging.md +59 -56
  29. package/docs/Reference/Middleware.md +19 -19
  30. package/docs/Reference/Plugins.md +55 -71
  31. package/docs/Reference/Principles.md +25 -30
  32. package/docs/Reference/Reply.md +11 -10
  33. package/docs/Reference/Request.md +89 -98
  34. package/docs/Reference/Routes.md +108 -128
  35. package/docs/Reference/Server.md +18 -16
  36. package/docs/Reference/Type-Providers.md +19 -21
  37. package/docs/Reference/TypeScript.md +1 -18
  38. package/docs/Reference/Validation-and-Serialization.md +134 -159
  39. package/docs/Reference/Warnings.md +22 -25
  40. package/fastify.js +1 -1
  41. package/lib/contentTypeParser.js +7 -8
  42. package/lib/error-handler.js +14 -12
  43. package/lib/headRoute.js +4 -2
  44. package/lib/pluginUtils.js +4 -2
  45. package/lib/server.js +5 -0
  46. package/lib/validation.js +1 -1
  47. package/lib/warnings.js +9 -0
  48. package/lib/wrapThenable.js +8 -1
  49. package/package.json +10 -10
  50. package/test/build/error-serializer.test.js +2 -1
  51. package/test/bundler/esbuild/package.json +1 -1
  52. package/test/close.test.js +125 -108
  53. package/test/custom-parser-async.test.js +34 -36
  54. package/test/genReqId.test.js +125 -174
  55. package/test/has-route.test.js +1 -3
  56. package/test/internals/content-type-parser.test.js +1 -1
  57. package/test/issue-4959.test.js +84 -0
  58. package/test/listen.1.test.js +37 -34
  59. package/test/listen.2.test.js +47 -40
  60. package/test/listen.3.test.js +28 -32
  61. package/test/listen.4.test.js +61 -45
  62. package/test/listen.5.test.js +23 -0
  63. package/test/register.test.js +55 -50
  64. package/test/request-error.test.js +114 -94
  65. package/test/route-shorthand.test.js +36 -32
  66. package/test/server.test.js +0 -175
  67. package/test/stream.5.test.js +35 -33
  68. package/test/throw.test.js +87 -91
  69. package/test/toolkit.js +32 -0
  70. package/test/trust-proxy.test.js +23 -23
  71. package/test/types/instance.test-d.ts +1 -0
  72. package/test/upgrade.test.js +32 -30
  73. package/types/instance.d.ts +4 -0
@@ -1,14 +1,13 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
4
- const test = t.test
3
+ const { test } = require('node:test')
5
4
  const sget = require('simple-get').concat
6
5
  const Fastify = require('../fastify')
7
6
 
8
7
  process.removeAllListeners('warning')
9
8
 
10
- test('contentTypeParser should add a custom async parser', t => {
11
- t.plan(3)
9
+ test('contentTypeParser should add a custom async parser', async t => {
10
+ t.plan(2)
12
11
  const fastify = Fastify()
13
12
 
14
13
  fastify.post('/', (req, reply) => {
@@ -24,43 +23,42 @@ test('contentTypeParser should add a custom async parser', t => {
24
23
  return res
25
24
  })
26
25
 
27
- fastify.listen({ port: 0 }, err => {
28
- t.error(err)
26
+ t.after(() => fastify.close())
27
+ await fastify.listen({ port: 0 })
29
28
 
30
- t.teardown(() => fastify.close())
29
+ await t.test('in POST', (t, done) => {
30
+ t.plan(3)
31
31
 
32
- t.test('in POST', t => {
33
- t.plan(3)
34
-
35
- sget({
36
- method: 'POST',
37
- url: 'http://localhost:' + fastify.server.address().port,
38
- body: '{"hello":"world"}',
39
- headers: {
40
- 'Content-Type': 'application/jsoff'
41
- }
42
- }, (err, response, body) => {
43
- t.error(err)
44
- t.equal(response.statusCode, 200)
45
- t.same(body.toString(), JSON.stringify({ hello: 'world' }))
46
- })
32
+ sget({
33
+ method: 'POST',
34
+ url: 'http://localhost:' + fastify.server.address().port,
35
+ body: '{"hello":"world"}',
36
+ headers: {
37
+ 'Content-Type': 'application/jsoff'
38
+ }
39
+ }, (err, response, body) => {
40
+ t.assert.ifError(err)
41
+ t.assert.strictEqual(response.statusCode, 200)
42
+ t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
43
+ done()
47
44
  })
45
+ })
48
46
 
49
- t.test('in OPTIONS', t => {
50
- t.plan(3)
47
+ await t.test('in OPTIONS', (t, done) => {
48
+ t.plan(3)
51
49
 
52
- sget({
53
- method: 'OPTIONS',
54
- url: 'http://localhost:' + fastify.server.address().port,
55
- body: '{"hello":"world"}',
56
- headers: {
57
- 'Content-Type': 'application/jsoff'
58
- }
59
- }, (err, response, body) => {
60
- t.error(err)
61
- t.equal(response.statusCode, 200)
62
- t.same(body.toString(), JSON.stringify({ hello: 'world' }))
63
- })
50
+ sget({
51
+ method: 'OPTIONS',
52
+ url: 'http://localhost:' + fastify.server.address().port,
53
+ body: '{"hello":"world"}',
54
+ headers: {
55
+ 'Content-Type': 'application/jsoff'
56
+ }
57
+ }, (err, response, body) => {
58
+ t.assert.ifError(err)
59
+ t.assert.strictEqual(response.statusCode, 200)
60
+ t.assert.deepStrictEqual(body.toString(), JSON.stringify({ hello: 'world' }))
61
+ done()
64
62
  })
65
63
  })
66
64
  })
@@ -1,11 +1,11 @@
1
1
  'use strict'
2
2
 
3
3
  const { Readable } = require('node:stream')
4
- const { test } = require('tap')
4
+ const { test } = require('node:test')
5
5
  const fp = require('fastify-plugin')
6
6
  const Fastify = require('..')
7
7
 
8
- test('Should accept a custom genReqId function', t => {
8
+ test('Should accept a custom genReqId function', (t, done) => {
9
9
  t.plan(4)
10
10
 
11
11
  const fastify = Fastify({
@@ -14,62 +14,64 @@ test('Should accept a custom genReqId function', t => {
14
14
  }
15
15
  })
16
16
 
17
+ t.after(() => fastify.close())
17
18
  fastify.get('/', (req, reply) => {
18
- t.ok(req.id)
19
+ t.assert.ok(req.id)
19
20
  reply.send({ id: req.id })
20
21
  })
21
22
 
22
23
  fastify.listen({ port: 0 }, err => {
23
- t.error(err)
24
+ t.assert.ifError(err)
24
25
  fastify.inject({
25
26
  method: 'GET',
26
- url: 'http://localhost:' + fastify.server.address().port
27
+ url: `http://localhost:${fastify.server.address().port}`
27
28
  }, (err, res) => {
28
- t.error(err)
29
+ t.assert.ifError(err)
29
30
  const payload = JSON.parse(res.payload)
30
- t.equal(payload.id, 'a')
31
- fastify.close()
31
+ t.assert.strictEqual(payload.id, 'a')
32
+ done()
32
33
  })
33
34
  })
34
35
  })
35
36
 
36
- test('Custom genReqId function gets raw request as argument', t => {
37
+ test('Custom genReqId function gets raw request as argument', (t, done) => {
37
38
  t.plan(9)
38
39
 
39
40
  const REQUEST_ID = 'REQ-1234'
40
41
 
41
42
  const fastify = Fastify({
42
43
  genReqId: function (req) {
43
- t.notOk('id' in req)
44
- t.notOk('raw' in req)
45
- t.ok(req instanceof Readable)
44
+ t.assert.strictEqual('id' in req, false)
45
+ t.assert.strictEqual('raw' in req, false)
46
+ t.assert.ok(req instanceof Readable)
46
47
  // http.IncomingMessage does have `rawHeaders` property, but FastifyRequest does not
47
48
  const index = req.rawHeaders.indexOf('x-request-id')
48
49
  const xReqId = req.rawHeaders[index + 1]
49
- t.equal(xReqId, REQUEST_ID)
50
- t.equal(req.headers['x-request-id'], REQUEST_ID)
50
+ t.assert.strictEqual(xReqId, REQUEST_ID)
51
+ t.assert.strictEqual(req.headers['x-request-id'], REQUEST_ID)
51
52
  return xReqId
52
53
  }
53
54
  })
55
+ t.after(() => fastify.close())
54
56
 
55
57
  fastify.get('/', (req, reply) => {
56
- t.equal(req.id, REQUEST_ID)
58
+ t.assert.strictEqual(req.id, REQUEST_ID)
57
59
  reply.send({ id: req.id })
58
60
  })
59
61
 
60
62
  fastify.listen({ port: 0 }, err => {
61
- t.error(err)
63
+ t.assert.ifError(err)
62
64
  fastify.inject({
63
65
  method: 'GET',
64
66
  headers: {
65
67
  'x-request-id': REQUEST_ID
66
68
  },
67
- url: 'http://localhost:' + fastify.server.address().port
69
+ url: `http://localhost:${fastify.server.address().port}`
68
70
  }, (err, res) => {
69
- t.error(err)
71
+ t.assert.ifError(err)
70
72
  const payload = JSON.parse(res.payload)
71
- t.equal(payload.id, REQUEST_ID)
72
- fastify.close()
73
+ t.assert.strictEqual(payload.id, REQUEST_ID)
74
+ done()
73
75
  })
74
76
  })
75
77
  })
@@ -77,13 +79,13 @@ test('Custom genReqId function gets raw request as argument', t => {
77
79
  test('Should handle properly requestIdHeader option', t => {
78
80
  t.plan(4)
79
81
 
80
- t.equal(Fastify({ requestIdHeader: '' }).initialConfig.requestIdHeader, false)
81
- t.equal(Fastify({ requestIdHeader: false }).initialConfig.requestIdHeader, false)
82
- t.equal(Fastify({ requestIdHeader: true }).initialConfig.requestIdHeader, 'request-id')
83
- t.equal(Fastify({ requestIdHeader: 'x-request-id' }).initialConfig.requestIdHeader, 'x-request-id')
82
+ t.assert.strictEqual(Fastify({ requestIdHeader: '' }).initialConfig.requestIdHeader, false)
83
+ t.assert.strictEqual(Fastify({ requestIdHeader: false }).initialConfig.requestIdHeader, false)
84
+ t.assert.strictEqual(Fastify({ requestIdHeader: true }).initialConfig.requestIdHeader, 'request-id')
85
+ t.assert.strictEqual(Fastify({ requestIdHeader: 'x-request-id' }).initialConfig.requestIdHeader, 'x-request-id')
84
86
  })
85
87
 
86
- test('Should accept option to set genReqId with setGenReqId option', t => {
88
+ test('Should accept option to set genReqId with setGenReqId option', (t, done) => {
87
89
  t.plan(9)
88
90
 
89
91
  const fastify = Fastify({
@@ -92,12 +94,14 @@ test('Should accept option to set genReqId with setGenReqId option', t => {
92
94
  }
93
95
  })
94
96
 
97
+ t.after(() => fastify.close())
98
+
95
99
  fastify.register(function (instance, opts, next) {
96
100
  instance.setGenReqId(function (req) {
97
101
  return 'foo'
98
102
  })
99
103
  instance.get('/', (req, reply) => {
100
- t.ok(req.id)
104
+ t.assert.ok(req.id)
101
105
  reply.send({ id: req.id })
102
106
  })
103
107
  next()
@@ -108,49 +112,57 @@ test('Should accept option to set genReqId with setGenReqId option', t => {
108
112
  return 'bar'
109
113
  })
110
114
  instance.get('/', (req, reply) => {
111
- t.ok(req.id)
115
+ t.assert.ok(req.id)
112
116
  reply.send({ id: req.id })
113
117
  })
114
118
  next()
115
119
  }, { prefix: 'bar' })
116
120
 
117
121
  fastify.get('/', (req, reply) => {
118
- t.ok(req.id)
122
+ t.assert.ok(req.id)
119
123
  reply.send({ id: req.id })
120
124
  })
121
125
 
126
+ let pending = 3
127
+
128
+ function completed () {
129
+ if (--pending === 0) {
130
+ done()
131
+ }
132
+ }
133
+
122
134
  fastify.inject({
123
135
  method: 'GET',
124
136
  url: '/'
125
137
  }, (err, res) => {
126
- t.error(err)
138
+ t.assert.ifError(err)
127
139
  const payload = JSON.parse(res.payload)
128
- t.equal(payload.id, 'base')
129
- fastify.close()
140
+ t.assert.strictEqual(payload.id, 'base')
141
+ completed()
130
142
  })
131
143
 
132
144
  fastify.inject({
133
145
  method: 'GET',
134
146
  url: '/foo'
135
147
  }, (err, res) => {
136
- t.error(err)
148
+ t.assert.ifError(err)
137
149
  const payload = JSON.parse(res.payload)
138
- t.equal(payload.id, 'foo')
139
- fastify.close()
150
+ t.assert.strictEqual(payload.id, 'foo')
151
+ completed()
140
152
  })
141
153
 
142
154
  fastify.inject({
143
155
  method: 'GET',
144
156
  url: '/bar'
145
157
  }, (err, res) => {
146
- t.error(err)
158
+ t.assert.ifError(err)
147
159
  const payload = JSON.parse(res.payload)
148
- t.equal(payload.id, 'bar')
149
- fastify.close()
160
+ t.assert.strictEqual(payload.id, 'bar')
161
+ completed()
150
162
  })
151
163
  })
152
164
 
153
- test('Should encapsulate setGenReqId', t => {
165
+ test('Should encapsulate setGenReqId', (t, done) => {
154
166
  t.plan(12)
155
167
 
156
168
  const fastify = Fastify({
@@ -159,6 +171,7 @@ test('Should encapsulate setGenReqId', t => {
159
171
  }
160
172
  })
161
173
 
174
+ t.after(() => fastify.close())
162
175
  const bazInstance = function (instance, opts, next) {
163
176
  instance.register(barInstance, { prefix: 'baz' })
164
177
 
@@ -166,7 +179,7 @@ test('Should encapsulate setGenReqId', t => {
166
179
  return 'baz'
167
180
  })
168
181
  instance.get('/', (req, reply) => {
169
- t.ok(req.id)
182
+ t.assert.ok(req.id)
170
183
  reply.send({ id: req.id })
171
184
  })
172
185
  next()
@@ -177,7 +190,7 @@ test('Should encapsulate setGenReqId', t => {
177
190
  return 'bar'
178
191
  })
179
192
  instance.get('/', (req, reply) => {
180
- t.ok(req.id)
193
+ t.assert.ok(req.id)
181
194
  reply.send({ id: req.id })
182
195
  })
183
196
  next()
@@ -192,7 +205,7 @@ test('Should encapsulate setGenReqId', t => {
192
205
  })
193
206
 
194
207
  instance.get('/', (req, reply) => {
195
- t.ok(req.id)
208
+ t.assert.ok(req.id)
196
209
  reply.send({ id: req.id })
197
210
  })
198
211
  next()
@@ -201,159 +214,71 @@ test('Should encapsulate setGenReqId', t => {
201
214
  fastify.register(fooInstance, { prefix: 'foo' })
202
215
 
203
216
  fastify.get('/', (req, reply) => {
204
- t.ok(req.id)
217
+ t.assert.ok(req.id)
205
218
  reply.send({ id: req.id })
206
219
  })
207
220
 
208
- fastify.inject({
209
- method: 'GET',
210
- url: '/'
211
- }, (err, res) => {
212
- t.error(err)
213
- const payload = JSON.parse(res.payload)
214
- t.equal(payload.id, 'base')
215
- fastify.close()
216
- })
217
-
218
- fastify.inject({
219
- method: 'GET',
220
- url: '/foo'
221
- }, (err, res) => {
222
- t.error(err)
223
- const payload = JSON.parse(res.payload)
224
- t.equal(payload.id, 'foo')
225
- fastify.close()
226
- })
221
+ let pending = 4
227
222
 
228
- fastify.inject({
229
- method: 'GET',
230
- url: '/foo/bar'
231
- }, (err, res) => {
232
- t.error(err)
233
- const payload = JSON.parse(res.payload)
234
- t.equal(payload.id, 'bar')
235
- fastify.close()
236
- })
237
-
238
- fastify.inject({
239
- method: 'GET',
240
- url: '/foo/baz'
241
- }, (err, res) => {
242
- t.error(err)
243
- const payload = JSON.parse(res.payload)
244
- t.equal(payload.id, 'baz')
245
- fastify.close()
246
- })
247
- })
248
-
249
- test('Should encapsulate setGenReqId', t => {
250
- t.plan(12)
251
-
252
- const fastify = Fastify({
253
- genReqId: function (req) {
254
- return 'base'
223
+ function completed () {
224
+ if (--pending === 0) {
225
+ done()
255
226
  }
256
- })
257
-
258
- const bazInstance = function (instance, opts, next) {
259
- instance.register(barInstance, { prefix: 'baz' })
260
-
261
- instance.setGenReqId(function (req) {
262
- return 'baz'
263
- })
264
- instance.get('/', (req, reply) => {
265
- t.ok(req.id)
266
- reply.send({ id: req.id })
267
- })
268
- next()
269
- }
270
-
271
- const barInstance = function (instance, opts, next) {
272
- instance.setGenReqId(function (req) {
273
- return 'bar'
274
- })
275
- instance.get('/', (req, reply) => {
276
- t.ok(req.id)
277
- reply.send({ id: req.id })
278
- })
279
- next()
280
- }
281
-
282
- const fooInstance = function (instance, opts, next) {
283
- instance.register(bazInstance, { prefix: 'baz' })
284
- instance.register(barInstance, { prefix: 'bar' })
285
-
286
- instance.setGenReqId(function (req) {
287
- return 'foo'
288
- })
289
-
290
- instance.get('/', (req, reply) => {
291
- t.ok(req.id)
292
- reply.send({ id: req.id })
293
- })
294
- next()
295
227
  }
296
228
 
297
- fastify.register(fooInstance, { prefix: 'foo' })
298
-
299
- fastify.get('/', (req, reply) => {
300
- t.ok(req.id)
301
- reply.send({ id: req.id })
302
- })
303
-
304
229
  fastify.inject({
305
230
  method: 'GET',
306
231
  url: '/'
307
232
  }, (err, res) => {
308
- t.error(err)
233
+ t.assert.ifError(err)
309
234
  const payload = JSON.parse(res.payload)
310
- t.equal(payload.id, 'base')
311
- fastify.close()
235
+ t.assert.strictEqual(payload.id, 'base')
236
+ completed()
312
237
  })
313
238
 
314
239
  fastify.inject({
315
240
  method: 'GET',
316
241
  url: '/foo'
317
242
  }, (err, res) => {
318
- t.error(err)
243
+ t.assert.ifError(err)
319
244
  const payload = JSON.parse(res.payload)
320
- t.equal(payload.id, 'foo')
321
- fastify.close()
245
+ t.assert.strictEqual(payload.id, 'foo')
246
+ completed()
322
247
  })
323
248
 
324
249
  fastify.inject({
325
250
  method: 'GET',
326
251
  url: '/foo/bar'
327
252
  }, (err, res) => {
328
- t.error(err)
253
+ t.assert.ifError(err)
329
254
  const payload = JSON.parse(res.payload)
330
- t.equal(payload.id, 'bar')
331
- fastify.close()
255
+ t.assert.strictEqual(payload.id, 'bar')
256
+ completed()
332
257
  })
333
258
 
334
259
  fastify.inject({
335
260
  method: 'GET',
336
261
  url: '/foo/baz'
337
262
  }, (err, res) => {
338
- t.error(err)
263
+ t.assert.ifError(err)
339
264
  const payload = JSON.parse(res.payload)
340
- t.equal(payload.id, 'baz')
341
- fastify.close()
265
+ t.assert.strictEqual(payload.id, 'baz')
266
+ completed()
342
267
  })
343
268
  })
344
269
 
345
- test('Should not alter parent of genReqId', t => {
270
+ test('Should not alter parent of genReqId', (t, done) => {
346
271
  t.plan(6)
347
272
 
348
273
  const fastify = Fastify()
349
-
274
+ t.after(() => fastify.close())
350
275
  const fooInstance = function (instance, opts, next) {
351
276
  instance.setGenReqId(function (req) {
352
277
  return 'foo'
353
278
  })
354
279
 
355
280
  instance.get('/', (req, reply) => {
356
- t.ok(req.id)
281
+ t.assert.ok(req.id)
357
282
  reply.send({ id: req.id })
358
283
  })
359
284
  next()
@@ -362,32 +287,40 @@ test('Should not alter parent of genReqId', t => {
362
287
  fastify.register(fooInstance, { prefix: 'foo' })
363
288
 
364
289
  fastify.get('/', (req, reply) => {
365
- t.ok(req.id)
290
+ t.assert.ok(req.id)
366
291
  reply.send({ id: req.id })
367
292
  })
368
293
 
294
+ let pending = 2
295
+
296
+ function completed () {
297
+ if (--pending === 0) {
298
+ done()
299
+ }
300
+ }
301
+
369
302
  fastify.inject({
370
303
  method: 'GET',
371
304
  url: '/'
372
305
  }, (err, res) => {
373
- t.error(err)
306
+ t.assert.ifError(err)
374
307
  const payload = JSON.parse(res.payload)
375
- t.equal(payload.id, 'req-1')
376
- fastify.close()
308
+ t.assert.strictEqual(payload.id, 'req-1')
309
+ completed()
377
310
  })
378
311
 
379
312
  fastify.inject({
380
313
  method: 'GET',
381
314
  url: '/foo'
382
315
  }, (err, res) => {
383
- t.error(err)
316
+ t.assert.ifError(err)
384
317
  const payload = JSON.parse(res.payload)
385
- t.equal(payload.id, 'foo')
386
- fastify.close()
318
+ t.assert.strictEqual(payload.id, 'foo')
319
+ completed()
387
320
  })
388
321
  })
389
322
 
390
- test('Should have child instance user parent genReqId', t => {
323
+ test('Should have child instance user parent genReqId', (t, done) => {
391
324
  t.plan(6)
392
325
 
393
326
  const fastify = Fastify({
@@ -395,10 +328,11 @@ test('Should have child instance user parent genReqId', t => {
395
328
  return 'foo'
396
329
  }
397
330
  })
331
+ t.after(() => fastify.close())
398
332
 
399
333
  const fooInstance = function (instance, opts, next) {
400
334
  instance.get('/', (req, reply) => {
401
- t.ok(req.id)
335
+ t.assert.ok(req.id)
402
336
  reply.send({ id: req.id })
403
337
  })
404
338
  next()
@@ -407,69 +341,86 @@ test('Should have child instance user parent genReqId', t => {
407
341
  fastify.register(fooInstance, { prefix: 'foo' })
408
342
 
409
343
  fastify.get('/', (req, reply) => {
410
- t.ok(req.id)
344
+ t.assert.ok(req.id)
411
345
  reply.send({ id: req.id })
412
346
  })
413
347
 
348
+ let pending = 2
349
+
350
+ function completed () {
351
+ if (--pending === 0) {
352
+ done()
353
+ }
354
+ }
355
+
414
356
  fastify.inject({
415
357
  method: 'GET',
416
358
  url: '/'
417
359
  }, (err, res) => {
418
- t.error(err)
360
+ t.assert.ifError(err)
419
361
  const payload = JSON.parse(res.payload)
420
- t.equal(payload.id, 'foo')
421
- fastify.close()
362
+ t.assert.strictEqual(payload.id, 'foo')
363
+ completed()
422
364
  })
423
365
 
424
366
  fastify.inject({
425
367
  method: 'GET',
426
368
  url: '/foo'
427
369
  }, (err, res) => {
428
- t.error(err)
370
+ t.assert.ifError(err)
429
371
  const payload = JSON.parse(res.payload)
430
- t.equal(payload.id, 'foo')
431
- fastify.close()
372
+ t.assert.strictEqual(payload.id, 'foo')
373
+ completed()
432
374
  })
433
375
  })
434
376
 
435
- test('genReqId set on root scope when using fastify-plugin', t => {
377
+ test('genReqId set on root scope when using fastify-plugin', (t, done) => {
436
378
  t.plan(6)
437
379
 
438
380
  const fastify = Fastify()
381
+ t.after(() => fastify.close())
439
382
 
440
383
  fastify.register(fp(function (fastify, options, done) {
441
384
  fastify.setGenReqId(function (req) {
442
385
  return 'not-encapsulated'
443
386
  })
444
387
  fastify.get('/not-encapsulated-1', (req, reply) => {
445
- t.ok(req.id)
388
+ t.assert.ok(req.id)
446
389
  reply.send({ id: req.id })
447
390
  })
448
391
  done()
449
392
  }))
450
393
 
451
394
  fastify.get('/not-encapsulated-2', (req, reply) => {
452
- t.ok(req.id)
395
+ t.assert.ok(req.id)
453
396
  reply.send({ id: req.id })
454
397
  })
455
398
 
399
+ let pending = 2
400
+
401
+ function completed () {
402
+ if (--pending === 0) {
403
+ done()
404
+ }
405
+ }
406
+
456
407
  fastify.inject({
457
408
  method: 'GET',
458
409
  url: '/not-encapsulated-1'
459
410
  }, (err, res) => {
460
- t.error(err)
411
+ t.assert.ifError(err)
461
412
  const payload = JSON.parse(res.payload)
462
- t.equal(payload.id, 'not-encapsulated')
463
- fastify.close()
413
+ t.assert.strictEqual(payload.id, 'not-encapsulated')
414
+ completed()
464
415
  })
465
416
 
466
417
  fastify.inject({
467
418
  method: 'GET',
468
419
  url: '/not-encapsulated-2'
469
420
  }, (err, res) => {
470
- t.error(err)
421
+ t.assert.ifError(err)
471
422
  const payload = JSON.parse(res.payload)
472
- t.equal(payload.id, 'not-encapsulated')
473
- fastify.close()
423
+ t.assert.strictEqual(payload.id, 'not-encapsulated')
424
+ completed()
474
425
  })
475
426
  })
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { test, describe } = require('node:test')
4
- const Fastify = require('../fastify')
4
+ const Fastify = require('..')
5
5
 
6
6
  const fastify = Fastify()
7
7
 
@@ -10,9 +10,7 @@ describe('hasRoute', async t => {
10
10
  t.plan(3)
11
11
 
12
12
  t.assert.strictEqual(fastify.hasRoute({ }), false)
13
-
14
13
  t.assert.strictEqual(fastify.hasRoute({ method: 'GET' }), false)
15
-
16
14
  t.assert.strictEqual(fastify.hasRoute({ constraints: [] }), false)
17
15
  })
18
16