fastify 5.0.0 → 5.2.0

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 (201) hide show
  1. package/.borp.yaml +3 -0
  2. package/.vscode/settings.json +22 -0
  3. package/README.md +12 -7
  4. package/docs/Guides/Database.md +15 -15
  5. package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
  6. package/docs/Guides/Ecosystem.md +14 -15
  7. package/docs/Guides/Index.md +1 -1
  8. package/docs/Guides/Migration-Guide-V4.md +11 -11
  9. package/docs/Guides/Migration-Guide-V5.md +133 -9
  10. package/docs/Guides/Plugins-Guide.md +1 -1
  11. package/docs/Guides/Prototype-Poisoning.md +3 -3
  12. package/docs/Guides/Recommendations.md +9 -9
  13. package/docs/Guides/Serverless.md +5 -5
  14. package/docs/Guides/Testing.md +58 -57
  15. package/docs/Guides/Write-Plugin.md +2 -2
  16. package/docs/Guides/Write-Type-Provider.md +3 -3
  17. package/docs/Reference/ContentTypeParser.md +4 -4
  18. package/docs/Reference/Decorators.md +2 -2
  19. package/docs/Reference/Errors.md +3 -3
  20. package/docs/Reference/Hooks.md +7 -7
  21. package/docs/Reference/LTS.md +8 -0
  22. package/docs/Reference/Logging.md +5 -4
  23. package/docs/Reference/Reply.md +55 -58
  24. package/docs/Reference/Request.md +49 -42
  25. package/docs/Reference/Routes.md +16 -13
  26. package/docs/Reference/Server.md +32 -28
  27. package/docs/Reference/TypeScript.md +9 -9
  28. package/docs/Reference/Validation-and-Serialization.md +5 -5
  29. package/examples/typescript-server.ts +1 -1
  30. package/fastify.d.ts +14 -5
  31. package/fastify.js +8 -6
  32. package/lib/contentTypeParser.js +9 -7
  33. package/lib/context.js +1 -2
  34. package/lib/error-handler.js +9 -9
  35. package/lib/errors.js +1 -1
  36. package/lib/fourOhFour.js +1 -1
  37. package/lib/hooks.js +4 -1
  38. package/lib/{logger.js → logger-factory.js} +70 -122
  39. package/lib/logger-pino.js +68 -0
  40. package/lib/pluginOverride.js +1 -1
  41. package/lib/pluginUtils.js +2 -2
  42. package/lib/reply.js +4 -5
  43. package/lib/request.js +16 -9
  44. package/lib/route.js +23 -22
  45. package/lib/validation.js +2 -2
  46. package/package.json +13 -15
  47. package/test/404s.test.js +675 -629
  48. package/test/500s.test.js +72 -63
  49. package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
  50. package/test/als.test.js +48 -45
  51. package/test/async-await.test.js +148 -134
  52. package/test/async-dispose.test.js +4 -5
  53. package/test/async_hooks.test.js +30 -28
  54. package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
  55. package/test/buffer.test.js +9 -10
  56. package/test/build/error-serializer.test.js +3 -4
  57. package/test/build/version.test.js +2 -3
  58. package/test/build-certificate.js +1 -1
  59. package/test/bundler/README.md +5 -5
  60. package/test/bundler/esbuild/bundler-test.js +10 -9
  61. package/test/bundler/webpack/bundler-test.js +10 -9
  62. package/test/case-insensitive.test.js +31 -28
  63. package/test/chainable.test.js +4 -5
  64. package/test/check.test.js +8 -10
  65. package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
  66. package/test/client-timeout.test.js +5 -5
  67. package/test/close-pipelining.test.js +6 -8
  68. package/test/conditional-pino.test.js +47 -0
  69. package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
  70. package/test/constrained-routes.test.js +243 -236
  71. package/test/content-length.test.js +53 -68
  72. package/test/content-parser.test.js +186 -158
  73. package/test/content-type.test.js +8 -9
  74. package/test/context-config.test.js +44 -54
  75. package/test/custom-http-server.test.js +16 -20
  76. package/test/custom-parser.5.test.js +32 -32
  77. package/test/diagnostics-channel/404.test.js +15 -15
  78. package/test/diagnostics-channel/async-delay-request.test.js +25 -25
  79. package/test/diagnostics-channel/async-request.test.js +24 -24
  80. package/test/diagnostics-channel/error-before-handler.test.js +4 -5
  81. package/test/diagnostics-channel/error-request.test.js +19 -19
  82. package/test/diagnostics-channel/error-status.test.js +8 -8
  83. package/test/diagnostics-channel/init.test.js +6 -7
  84. package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
  85. package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
  86. package/test/diagnostics-channel/sync-request.test.js +19 -19
  87. package/test/encapsulated-child-logger-factory.test.js +8 -8
  88. package/test/encapsulated-error-handler.test.js +20 -20
  89. package/test/esm/errorCodes.test.mjs +5 -5
  90. package/test/esm/esm.test.mjs +3 -3
  91. package/test/esm/named-exports.mjs +3 -3
  92. package/test/esm/other.mjs +2 -2
  93. package/test/fastify-instance.test.js +33 -34
  94. package/test/{findRoute.test.js → find-route.test.js} +11 -10
  95. package/test/fluent-schema.test.js +33 -36
  96. package/test/handler-context.test.js +11 -11
  97. package/test/has-route.test.js +12 -15
  98. package/test/header-overflow.test.js +13 -12
  99. package/test/hooks.on-ready.test.js +2 -2
  100. package/test/hooks.test.js +25 -25
  101. package/test/http-methods/copy.test.js +22 -24
  102. package/test/http-methods/custom-http-methods.test.js +24 -21
  103. package/test/http-methods/get.test.js +97 -84
  104. package/test/http-methods/head.test.js +63 -57
  105. package/test/http-methods/lock.test.js +21 -20
  106. package/test/http-methods/mkcalendar.test.js +31 -27
  107. package/test/http-methods/mkcol.test.js +10 -10
  108. package/test/http-methods/move.test.js +11 -11
  109. package/test/http-methods/propfind.test.js +32 -27
  110. package/test/http-methods/proppatch.test.js +21 -19
  111. package/test/http-methods/report.test.js +32 -27
  112. package/test/http-methods/search.test.js +52 -47
  113. package/test/http-methods/trace.test.js +3 -4
  114. package/test/http-methods/unlock.test.js +10 -10
  115. package/test/http2/closing.test.js +50 -58
  116. package/test/http2/constraint.test.js +47 -50
  117. package/test/http2/head.test.js +18 -19
  118. package/test/http2/missing-http2-module.test.js +4 -5
  119. package/test/http2/plain.test.js +31 -31
  120. package/test/http2/secure-with-fallback.test.js +61 -61
  121. package/test/http2/secure.test.js +28 -31
  122. package/test/http2/unknown-http-method.test.js +13 -14
  123. package/test/https/custom-https-server.test.js +6 -7
  124. package/test/https/https.test.js +78 -78
  125. package/test/imports.test.js +5 -6
  126. package/test/internals/all.test.js +8 -11
  127. package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
  128. package/test/internals/context.test.js +9 -11
  129. package/test/internals/decorator.test.js +20 -21
  130. package/test/internals/errors.test.js +427 -427
  131. package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
  132. package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
  133. package/test/internals/hooks.test.js +31 -35
  134. package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
  135. package/test/internals/logger.test.js +28 -28
  136. package/test/internals/plugin.test.js +17 -18
  137. package/test/internals/reply-serialize.test.js +106 -106
  138. package/test/internals/reply.test.js +620 -585
  139. package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
  140. package/test/internals/request-validate.test.js +218 -221
  141. package/test/internals/request.test.js +225 -107
  142. package/test/internals/server.test.js +15 -12
  143. package/test/internals/validation.test.js +35 -36
  144. package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
  145. package/test/listen.5.test.js +9 -9
  146. package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
  147. package/test/middleware.test.js +4 -5
  148. package/test/noop-set.test.js +5 -5
  149. package/test/post-empty-body.test.js +18 -11
  150. package/test/pretty-print.test.js +59 -49
  151. package/test/proto-poisoning.test.js +42 -37
  152. package/test/reply-code.test.js +34 -32
  153. package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
  154. package/test/request-error.test.js +122 -0
  155. package/test/request-header-host.test.js +339 -0
  156. package/test/request-id.test.js +31 -25
  157. package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
  158. package/test/route.1.test.js +79 -72
  159. package/test/route.2.test.js +17 -16
  160. package/test/route.3.test.js +32 -27
  161. package/test/route.4.test.js +21 -25
  162. package/test/route.5.test.js +45 -64
  163. package/test/route.6.test.js +70 -89
  164. package/test/route.7.test.js +61 -65
  165. package/test/route.8.test.js +80 -18
  166. package/test/router-options.test.js +80 -77
  167. package/test/same-shape.test.js +5 -5
  168. package/test/schema-examples.test.js +72 -38
  169. package/test/serialize-response.test.js +9 -10
  170. package/test/server.test.js +75 -78
  171. package/test/set-error-handler.test.js +2 -3
  172. package/test/stream-serializers.test.js +10 -7
  173. package/test/sync-routes.test.js +18 -18
  174. package/test/test-reporter.mjs +68 -0
  175. package/test/trust-proxy.test.js +51 -45
  176. package/test/type-provider.test.js +8 -6
  177. package/test/types/content-type-parser.test-d.ts +1 -1
  178. package/test/types/fastify.test-d.ts +16 -4
  179. package/test/types/hooks.test-d.ts +2 -1
  180. package/test/types/instance.test-d.ts +13 -13
  181. package/test/types/logger.test-d.ts +2 -2
  182. package/test/types/plugin.test-d.ts +17 -9
  183. package/test/types/register.test-d.ts +22 -6
  184. package/test/types/reply.test-d.ts +1 -1
  185. package/test/types/route.test-d.ts +34 -4
  186. package/test/types/serverFactory.test-d.ts +1 -1
  187. package/test/types/type-provider.test-d.ts +1 -1
  188. package/test/url-rewriting.test.js +35 -38
  189. package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
  190. package/test/validation-error-handling.test.js +259 -285
  191. package/test/versioned-routes.test.js +126 -113
  192. package/test/web-api.test.js +48 -37
  193. package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
  194. package/types/hooks.d.ts +2 -1
  195. package/types/instance.d.ts +9 -2
  196. package/types/register.d.ts +12 -3
  197. package/types/reply.d.ts +1 -1
  198. package/types/request.d.ts +2 -6
  199. package/types/serverFactory.d.ts +3 -3
  200. package/types/utils.d.ts +13 -5
  201. package/test/types/import.js +0 -2
@@ -1,7 +1,6 @@
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('..')
7
6
  const {
@@ -10,15 +9,14 @@ const {
10
9
  } = require('../lib/errors')
11
10
  const { getServerUrl } = require('./helper')
12
11
 
13
- test('route', t => {
12
+ test('route', async t => {
14
13
  t.plan(10)
15
- const test = t.test
16
14
 
17
- test('route - get', t => {
15
+ await t.test('route - get', (t, done) => {
18
16
  t.plan(4)
19
17
 
20
18
  const fastify = Fastify()
21
- t.doesNotThrow(() =>
19
+ t.assert.doesNotThrow(() =>
22
20
  fastify.route({
23
21
  method: 'GET',
24
22
  url: '/',
@@ -41,24 +39,25 @@ test('route', t => {
41
39
  )
42
40
 
43
41
  fastify.listen({ port: 0 }, function (err) {
44
- if (err) t.error(err)
45
- t.teardown(() => { fastify.close() })
42
+ if (err) t.assert.ifError(err)
43
+ t.after(() => { fastify.close() })
46
44
  sget({
47
45
  method: 'GET',
48
46
  url: getServerUrl(fastify) + '/'
49
47
  }, (err, response, body) => {
50
- t.error(err)
51
- t.equal(response.statusCode, 200)
52
- t.same(JSON.parse(body), { hello: 'world' })
48
+ t.assert.ifError(err)
49
+ t.assert.strictEqual(response.statusCode, 200)
50
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
51
+ done()
53
52
  })
54
53
  })
55
54
  })
56
55
 
57
- test('missing schema - route', t => {
56
+ await t.test('missing schema - route', (t, done) => {
58
57
  t.plan(4)
59
58
 
60
59
  const fastify = Fastify()
61
- t.doesNotThrow(() =>
60
+ t.assert.doesNotThrow(() =>
62
61
  fastify.route({
63
62
  method: 'GET',
64
63
  url: '/missing',
@@ -69,31 +68,32 @@ test('route', t => {
69
68
  )
70
69
 
71
70
  fastify.listen({ port: 0 }, function (err) {
72
- if (err) t.error(err)
73
- t.teardown(() => { fastify.close() })
71
+ if (err) t.assert.ifError(err)
72
+ t.after(() => { fastify.close() })
74
73
  sget({
75
74
  method: 'GET',
76
75
  url: getServerUrl(fastify) + '/missing'
77
76
  }, (err, response, body) => {
78
- t.error(err)
79
- t.equal(response.statusCode, 200)
80
- t.same(JSON.parse(body), { hello: 'world' })
77
+ t.assert.ifError(err)
78
+ t.assert.strictEqual(response.statusCode, 200)
79
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
80
+ done()
81
81
  })
82
82
  })
83
83
  })
84
84
 
85
- test('invalid handler attribute - route', t => {
85
+ await t.test('invalid handler attribute - route', t => {
86
86
  t.plan(1)
87
87
 
88
88
  const fastify = Fastify()
89
- t.throws(() => fastify.get('/', { handler: 'not a function' }, () => { }))
89
+ t.assert.throws(() => fastify.get('/', { handler: 'not a function' }, () => { }))
90
90
  })
91
91
 
92
- test('Add Multiple methods per route all uppercase', t => {
92
+ await t.test('Add Multiple methods per route all uppercase', (t, done) => {
93
93
  t.plan(7)
94
94
 
95
95
  const fastify = Fastify()
96
- t.doesNotThrow(() =>
96
+ t.assert.doesNotThrow(() =>
97
97
  fastify.route({
98
98
  method: ['GET', 'DELETE'],
99
99
  url: '/multiple',
@@ -103,33 +103,34 @@ test('route', t => {
103
103
  }))
104
104
 
105
105
  fastify.listen({ port: 0 }, function (err) {
106
- if (err) t.error(err)
107
- t.teardown(() => { fastify.close() })
106
+ if (err) t.assert.ifError(err)
107
+ t.after(() => { fastify.close() })
108
108
  sget({
109
109
  method: 'GET',
110
110
  url: getServerUrl(fastify) + '/multiple'
111
111
  }, (err, response, body) => {
112
- t.error(err)
113
- t.equal(response.statusCode, 200)
114
- t.same(JSON.parse(body), { hello: 'world' })
112
+ t.assert.ifError(err)
113
+ t.assert.strictEqual(response.statusCode, 200)
114
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
115
115
  })
116
116
 
117
117
  sget({
118
118
  method: 'DELETE',
119
119
  url: getServerUrl(fastify) + '/multiple'
120
120
  }, (err, response, body) => {
121
- t.error(err)
122
- t.equal(response.statusCode, 200)
123
- t.same(JSON.parse(body), { hello: 'world' })
121
+ t.assert.ifError(err)
122
+ t.assert.strictEqual(response.statusCode, 200)
123
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
124
+ done()
124
125
  })
125
126
  })
126
127
  })
127
128
 
128
- test('Add Multiple methods per route all lowercase', t => {
129
+ await t.test('Add Multiple methods per route all lowercase', (t, done) => {
129
130
  t.plan(7)
130
131
 
131
132
  const fastify = Fastify()
132
- t.doesNotThrow(() =>
133
+ t.assert.doesNotThrow(() =>
133
134
  fastify.route({
134
135
  method: ['get', 'delete'],
135
136
  url: '/multiple',
@@ -139,33 +140,34 @@ test('route', t => {
139
140
  }))
140
141
 
141
142
  fastify.listen({ port: 0 }, function (err) {
142
- if (err) t.error(err)
143
- t.teardown(() => { fastify.close() })
143
+ if (err) t.assert.ifError(err)
144
+ t.after(() => { fastify.close() })
144
145
  sget({
145
146
  method: 'GET',
146
147
  url: getServerUrl(fastify) + '/multiple'
147
148
  }, (err, response, body) => {
148
- t.error(err)
149
- t.equal(response.statusCode, 200)
150
- t.same(JSON.parse(body), { hello: 'world' })
149
+ t.assert.ifError(err)
150
+ t.assert.strictEqual(response.statusCode, 200)
151
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
151
152
  })
152
153
 
153
154
  sget({
154
155
  method: 'DELETE',
155
156
  url: getServerUrl(fastify) + '/multiple'
156
157
  }, (err, response, body) => {
157
- t.error(err)
158
- t.equal(response.statusCode, 200)
159
- t.same(JSON.parse(body), { hello: 'world' })
158
+ t.assert.ifError(err)
159
+ t.assert.strictEqual(response.statusCode, 200)
160
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
161
+ done()
160
162
  })
161
163
  })
162
164
  })
163
165
 
164
- test('Add Multiple methods per route mixed uppercase and lowercase', t => {
166
+ await t.test('Add Multiple methods per route mixed uppercase and lowercase', (t, done) => {
165
167
  t.plan(7)
166
168
 
167
169
  const fastify = Fastify()
168
- t.doesNotThrow(() =>
170
+ t.assert.doesNotThrow(() =>
169
171
  fastify.route({
170
172
  method: ['GET', 'delete'],
171
173
  url: '/multiple',
@@ -175,33 +177,34 @@ test('route', t => {
175
177
  }))
176
178
 
177
179
  fastify.listen({ port: 0 }, function (err) {
178
- if (err) t.error(err)
179
- t.teardown(() => { fastify.close() })
180
+ if (err) t.assert.ifError(err)
181
+ t.after(() => { fastify.close() })
180
182
  sget({
181
183
  method: 'GET',
182
184
  url: getServerUrl(fastify) + '/multiple'
183
185
  }, (err, response, body) => {
184
- t.error(err)
185
- t.equal(response.statusCode, 200)
186
- t.same(JSON.parse(body), { hello: 'world' })
186
+ t.assert.ifError(err)
187
+ t.assert.strictEqual(response.statusCode, 200)
188
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
187
189
  })
188
190
 
189
191
  sget({
190
192
  method: 'DELETE',
191
193
  url: getServerUrl(fastify) + '/multiple'
192
194
  }, (err, response, body) => {
193
- t.error(err)
194
- t.equal(response.statusCode, 200)
195
- t.same(JSON.parse(body), { hello: 'world' })
195
+ t.assert.ifError(err)
196
+ t.assert.strictEqual(response.statusCode, 200)
197
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
198
+ done()
196
199
  })
197
200
  })
198
201
  })
199
202
 
200
- test('Add invalid Multiple methods per route', t => {
203
+ t.test('Add invalid Multiple methods per route', t => {
201
204
  t.plan(1)
202
205
 
203
206
  const fastify = Fastify()
204
- t.throws(() =>
207
+ t.assert.throws(() =>
205
208
  fastify.route({
206
209
  method: ['GET', 1],
207
210
  url: '/invalid-method',
@@ -211,11 +214,11 @@ test('route', t => {
211
214
  }), new FST_ERR_ROUTE_METHOD_INVALID())
212
215
  })
213
216
 
214
- test('Add method', t => {
217
+ await t.test('Add method', t => {
215
218
  t.plan(1)
216
219
 
217
220
  const fastify = Fastify()
218
- t.throws(() =>
221
+ t.assert.throws(() =>
219
222
  fastify.route({
220
223
  method: 1,
221
224
  url: '/invalid-method',
@@ -225,11 +228,11 @@ test('route', t => {
225
228
  }), new FST_ERR_ROUTE_METHOD_INVALID())
226
229
  })
227
230
 
228
- test('Add additional multiple methods to existing route', t => {
231
+ await t.test('Add additional multiple methods to existing route', (t, done) => {
229
232
  t.plan(7)
230
233
 
231
234
  const fastify = Fastify()
232
- t.doesNotThrow(() => {
235
+ t.assert.doesNotThrow(() => {
233
236
  fastify.get('/add-multiple', function (req, reply) {
234
237
  reply.send({ hello: 'Bob!' })
235
238
  })
@@ -243,49 +246,52 @@ test('route', t => {
243
246
  })
244
247
 
245
248
  fastify.listen({ port: 0 }, function (err) {
246
- if (err) t.error(err)
247
- t.teardown(() => { fastify.close() })
249
+ if (err) t.assert.ifError(err)
250
+ t.after(() => { fastify.close() })
248
251
  sget({
249
252
  method: 'PUT',
250
253
  url: getServerUrl(fastify) + '/add-multiple'
251
254
  }, (err, response, body) => {
252
- t.error(err)
253
- t.equal(response.statusCode, 200)
254
- t.same(JSON.parse(body), { hello: 'world' })
255
+ t.assert.ifError(err)
256
+ t.assert.strictEqual(response.statusCode, 200)
257
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
255
258
  })
256
259
 
257
260
  sget({
258
261
  method: 'DELETE',
259
262
  url: getServerUrl(fastify) + '/add-multiple'
260
263
  }, (err, response, body) => {
261
- t.error(err)
262
- t.equal(response.statusCode, 200)
263
- t.same(JSON.parse(body), { hello: 'world' })
264
+ t.assert.ifError(err)
265
+ t.assert.strictEqual(response.statusCode, 200)
266
+ t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
267
+ done()
264
268
  })
265
269
  })
266
270
  })
267
271
 
268
- test('cannot add another route after binding', t => {
272
+ await t.test('cannot add another route after binding', (t, done) => {
269
273
  t.plan(1)
270
274
 
271
275
  const fastify = Fastify()
272
276
 
273
277
  fastify.listen({ port: 0 }, function (err) {
274
- if (err) t.error(err)
275
- t.teardown(() => { fastify.close() })
278
+ if (err) t.assert.ifError(err)
279
+ t.after(() => { fastify.close() })
276
280
 
277
- t.throws(() => fastify.route({
281
+ t.assert.throws(() => fastify.route({
278
282
  method: 'GET',
279
283
  url: '/another-get-route',
280
284
  handler: function (req, reply) {
281
285
  reply.send({ hello: 'world' })
282
286
  }
283
287
  }), new FST_ERR_INSTANCE_ALREADY_LISTENING('Cannot add route!'))
288
+
289
+ done()
284
290
  })
285
291
  })
286
292
  })
287
293
 
288
- test('invalid schema - route', t => {
294
+ test('invalid schema - route', (t, done) => {
289
295
  t.plan(3)
290
296
 
291
297
  const fastify = Fastify()
@@ -300,10 +306,11 @@ test('invalid schema - route', t => {
300
306
  }
301
307
  })
302
308
  fastify.after(err => {
303
- t.notOk(err, 'the error is throw on preReady')
309
+ t.assert.ok(!err, 'the error is throw on preReady')
304
310
  })
305
311
  fastify.ready(err => {
306
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
307
- t.match(err.message, /Failed building the validation schema for GET: \/invalid/)
312
+ t.assert.strictEqual(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
313
+ t.assert.match(err.message, /Failed building the validation schema for GET: \/invalid/)
314
+ done()
308
315
  })
309
316
  })
@@ -1,7 +1,6 @@
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 Fastify = require('../fastify')
6
5
 
7
6
  test('same route definition object on multiple prefixes', async t => {
@@ -17,13 +16,13 @@ test('same route definition object on multiple prefixes', async t => {
17
16
 
18
17
  fastify.register(async function (f) {
19
18
  f.addHook('onRoute', (routeOptions) => {
20
- t.equal(routeOptions.url, '/v1/simple')
19
+ t.assert.strictEqual(routeOptions.url, '/v1/simple')
21
20
  })
22
21
  f.route(routeObject)
23
22
  }, { prefix: '/v1' })
24
23
  fastify.register(async function (f) {
25
24
  f.addHook('onRoute', (routeOptions) => {
26
- t.equal(routeOptions.url, '/v2/simple')
25
+ t.assert.strictEqual(routeOptions.url, '/v2/simple')
27
26
  })
28
27
  f.route(routeObject)
29
28
  }, { prefix: '/v2' })
@@ -31,7 +30,7 @@ test('same route definition object on multiple prefixes', async t => {
31
30
  await fastify.ready()
32
31
  })
33
32
 
34
- test('path can be specified in place of uri', t => {
33
+ test('path can be specified in place of uri', (t, done) => {
35
34
  t.plan(3)
36
35
  const fastify = Fastify()
37
36
 
@@ -49,9 +48,10 @@ test('path can be specified in place of uri', t => {
49
48
  }
50
49
 
51
50
  fastify.inject(reqOpts, (err, res) => {
52
- t.error(err)
53
- t.equal(res.statusCode, 200)
54
- t.same(JSON.parse(res.payload), { hello: 'world' })
51
+ t.assert.ifError(err)
52
+ t.assert.strictEqual(res.statusCode, 200)
53
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
54
+ done()
55
55
  })
56
56
  })
57
57
 
@@ -65,20 +65,20 @@ test('invalid bodyLimit option - route', t => {
65
65
  method: 'PUT',
66
66
  handler: () => null
67
67
  })
68
- t.fail('bodyLimit must be an integer')
68
+ t.assert.fail('bodyLimit must be an integer')
69
69
  } catch (err) {
70
- t.equal(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")
70
+ t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")
71
71
  }
72
72
 
73
73
  try {
74
74
  fastify.post('/url', { bodyLimit: 10000.1 }, () => null)
75
- t.fail('bodyLimit must be an integer')
75
+ t.assert.fail('bodyLimit must be an integer')
76
76
  } catch (err) {
77
- t.equal(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")
77
+ t.assert.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")
78
78
  }
79
79
  })
80
80
 
81
- test('handler function in options of shorthand route should works correctly', t => {
81
+ test('handler function in options of shorthand route should works correctly', (t, done) => {
82
82
  t.plan(3)
83
83
 
84
84
  const fastify = Fastify()
@@ -92,8 +92,9 @@ test('handler function in options of shorthand route should works correctly', t
92
92
  method: 'GET',
93
93
  url: '/foo'
94
94
  }, (err, res) => {
95
- t.error(err)
96
- t.equal(res.statusCode, 200)
97
- t.same(JSON.parse(res.payload), { hello: 'world' })
95
+ t.assert.ifError(err)
96
+ t.assert.strictEqual(res.statusCode, 200)
97
+ t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
98
+ done()
98
99
  })
99
100
  })
@@ -1,12 +1,11 @@
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 joi = require('joi')
6
5
  const Fastify = require('..')
7
6
 
8
- test('does not mutate joi schemas', t => {
9
- t.plan(4)
7
+ test('does not mutate joi schemas', (t, done) => {
8
+ t.plan(5)
10
9
 
11
10
  const fastify = Fastify()
12
11
  function validatorCompiler ({ schema, method, url, httpPart }) {
@@ -32,7 +31,8 @@ test('does not mutate joi schemas', t => {
32
31
  params: { an_id: joi.number() }
33
32
  },
34
33
  handler (req, res) {
35
- t.same(req.params, { an_id: 42 })
34
+ t.assert.strictEqual(Object.keys(req.params).length, 1)
35
+ t.assert.strictEqual(req.params.an_id, '42')
36
36
  res.send({ hello: 'world' })
37
37
  }
38
38
  })
@@ -40,14 +40,15 @@ test('does not mutate joi schemas', t => {
40
40
  fastify.inject({
41
41
  method: 'GET',
42
42
  url: '/foo/42'
43
- }, (err, result) => {
44
- t.error(err)
45
- t.equal(result.statusCode, 200)
46
- t.same(JSON.parse(result.payload), { hello: 'world' })
43
+ }, (err, res) => {
44
+ t.assert.ifError(err)
45
+ t.assert.strictEqual(res.statusCode, 200)
46
+ t.assert.deepStrictEqual(res.json(), { hello: 'world' })
47
+ done()
47
48
  })
48
49
  })
49
50
 
50
- test('multiple routes with one schema', t => {
51
+ test('multiple routes with one schema', (t, done) => {
51
52
  t.plan(2)
52
53
 
53
54
  const fastify = Fastify()
@@ -80,18 +81,19 @@ test('multiple routes with one schema', t => {
80
81
  })
81
82
 
82
83
  fastify.ready(error => {
83
- t.error(error)
84
- t.same(schema, schema)
84
+ t.assert.ifError(error)
85
+ t.assert.deepStrictEqual(schema, schema)
86
+ done()
85
87
  })
86
88
  })
87
89
 
88
- test('route error handler overrides default error handler', t => {
90
+ test('route error handler overrides default error handler', (t, done) => {
89
91
  t.plan(4)
90
92
 
91
93
  const fastify = Fastify()
92
94
 
93
95
  const customRouteErrorHandler = (error, request, reply) => {
94
- t.equal(error.message, 'Wrong Pot Error')
96
+ t.assert.strictEqual(error.message, 'Wrong Pot Error')
95
97
 
96
98
  reply.code(418).send({
97
99
  message: 'Make a brew',
@@ -113,23 +115,24 @@ test('route error handler overrides default error handler', t => {
113
115
  method: 'GET',
114
116
  url: '/coffee'
115
117
  }, (error, res) => {
116
- t.error(error)
117
- t.equal(res.statusCode, 418)
118
- t.same(JSON.parse(res.payload), {
118
+ t.assert.ifError(error)
119
+ t.assert.strictEqual(res.statusCode, 418)
120
+ t.assert.deepStrictEqual(res.json(), {
119
121
  message: 'Make a brew',
120
122
  statusCode: 418,
121
123
  error: 'Wrong Pot Error'
122
124
  })
125
+ done()
123
126
  })
124
127
  })
125
128
 
126
- test('route error handler does not affect other routes', t => {
129
+ test('route error handler does not affect other routes', (t, done) => {
127
130
  t.plan(3)
128
131
 
129
132
  const fastify = Fastify()
130
133
 
131
134
  const customRouteErrorHandler = (error, request, reply) => {
132
- t.equal(error.message, 'Wrong Pot Error')
135
+ t.assert.strictEqual(error.message, 'Wrong Pot Error')
133
136
 
134
137
  reply.code(418).send({
135
138
  message: 'Make a brew',
@@ -159,23 +162,24 @@ test('route error handler does not affect other routes', t => {
159
162
  method: 'GET',
160
163
  url: '/tea'
161
164
  }, (error, res) => {
162
- t.error(error)
163
- t.equal(res.statusCode, 500)
164
- t.same(JSON.parse(res.payload), {
165
+ t.assert.ifError(error)
166
+ t.assert.strictEqual(res.statusCode, 500)
167
+ t.assert.deepStrictEqual(res.json(), {
165
168
  message: 'No tea today',
166
169
  statusCode: 500,
167
170
  error: 'Internal Server Error'
168
171
  })
172
+ done()
169
173
  })
170
174
  })
171
175
 
172
- test('async error handler for a route', t => {
176
+ test('async error handler for a route', (t, done) => {
173
177
  t.plan(4)
174
178
 
175
179
  const fastify = Fastify()
176
180
 
177
181
  const customRouteErrorHandler = async (error, request, reply) => {
178
- t.equal(error.message, 'Delayed Pot Error')
182
+ t.assert.strictEqual(error.message, 'Delayed Pot Error')
179
183
  reply.code(418)
180
184
  return {
181
185
  message: 'Make a brew sometime later',
@@ -197,12 +201,13 @@ test('async error handler for a route', t => {
197
201
  method: 'GET',
198
202
  url: '/late-coffee'
199
203
  }, (error, res) => {
200
- t.error(error)
201
- t.equal(res.statusCode, 418)
202
- t.same(JSON.parse(res.payload), {
204
+ t.assert.ifError(error)
205
+ t.assert.strictEqual(res.statusCode, 418)
206
+ t.assert.deepStrictEqual(res.json(), {
203
207
  message: 'Make a brew sometime later',
204
208
  statusCode: 418,
205
209
  error: 'Delayed Pot Error'
206
210
  })
211
+ done()
207
212
  })
208
213
  })
@@ -1,21 +1,20 @@
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 Fastify = require('..')
6
5
 
7
- test('route error handler overrides global custom error handler', t => {
8
- t.plan(4)
6
+ test('route error handler overrides global custom error handler', async t => {
7
+ t.plan(3)
9
8
 
10
9
  const fastify = Fastify()
11
10
 
12
11
  const customGlobalErrorHandler = (error, request, reply) => {
13
- t.error(error)
12
+ t.assert.ifError(error)
14
13
  reply.code(429).send({ message: 'Too much coffee' })
15
14
  }
16
15
 
17
16
  const customRouteErrorHandler = (error, request, reply) => {
18
- t.equal(error.message, 'Wrong Pot Error')
17
+ t.assert.strictEqual(error.message, 'Wrong Pot Error')
19
18
  reply.code(418).send({
20
19
  message: 'Make a brew',
21
20
  statusCode: 418,
@@ -34,17 +33,15 @@ test('route error handler overrides global custom error handler', t => {
34
33
  errorHandler: customRouteErrorHandler
35
34
  })
36
35
 
37
- fastify.inject({
36
+ const res = await fastify.inject({
38
37
  method: 'GET',
39
38
  url: '/more-coffee'
40
- }, (error, res) => {
41
- t.error(error)
42
- t.equal(res.statusCode, 418)
43
- t.same(JSON.parse(res.payload), {
44
- message: 'Make a brew',
45
- statusCode: 418,
46
- error: 'Wrong Pot Error'
47
- })
39
+ })
40
+ t.assert.strictEqual(res.statusCode, 418)
41
+ t.assert.deepStrictEqual(JSON.parse(res.payload), {
42
+ message: 'Make a brew',
43
+ statusCode: 418,
44
+ error: 'Wrong Pot Error'
48
45
  })
49
46
  })
50
47
 
@@ -61,7 +58,7 @@ test('throws when route with empty url', async t => {
61
58
  }
62
59
  })
63
60
  } catch (err) {
64
- t.equal(err.message, 'The path could not be empty')
61
+ t.assert.strictEqual(err.message, 'The path could not be empty')
65
62
  }
66
63
  })
67
64
 
@@ -75,7 +72,7 @@ test('throws when route with empty url in shorthand declaration', async t => {
75
72
  async function handler () { return {} }
76
73
  )
77
74
  } catch (err) {
78
- t.equal(err.message, 'The path could not be empty')
75
+ t.assert.strictEqual(err.message, 'The path could not be empty')
79
76
  }
80
77
  })
81
78
 
@@ -94,19 +91,19 @@ test('throws when route-level error handler is not a function', t => {
94
91
  errorHandler: 'teapot'
95
92
  })
96
93
  } catch (err) {
97
- t.equal(err.message, 'Error Handler for GET:/tea route, if defined, must be a function')
94
+ t.assert.strictEqual(err.message, 'Error Handler for GET:/tea route, if defined, must be a function')
98
95
  }
99
96
  })
100
97
 
101
- test('route child logger factory overrides default child logger factory', t => {
102
- t.plan(3)
98
+ test('route child logger factory overrides default child logger factory', async t => {
99
+ t.plan(2)
103
100
 
104
101
  const fastify = Fastify()
105
102
 
106
103
  const customRouteChildLogger = (logger, bindings, opts, req) => {
107
104
  const child = logger.child(bindings, opts)
108
105
  child.customLog = function (message) {
109
- t.equal(message, 'custom')
106
+ t.assert.strictEqual(message, 'custom')
110
107
  }
111
108
  return child
112
109
  }
@@ -121,11 +118,10 @@ test('route child logger factory overrides default child logger factory', t => {
121
118
  childLoggerFactory: customRouteChildLogger
122
119
  })
123
120
 
124
- fastify.inject({
121
+ const res = await fastify.inject({
125
122
  method: 'GET',
126
123
  url: '/coffee'
127
- }, (error, res) => {
128
- t.error(error)
129
- t.equal(res.statusCode, 200)
130
124
  })
125
+
126
+ t.assert.strictEqual(res.statusCode, 200)
131
127
  })