fastify 5.2.1 → 5.3.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 (85) hide show
  1. package/LICENSE +1 -1
  2. package/PROJECT_CHARTER.md +7 -7
  3. package/README.md +24 -25
  4. package/SPONSORS.md +1 -0
  5. package/docs/Guides/Benchmarking.md +4 -4
  6. package/docs/Guides/Database.md +1 -1
  7. package/docs/Guides/Delay-Accepting-Requests.md +10 -10
  8. package/docs/Guides/Ecosystem.md +7 -1
  9. package/docs/Guides/Fluent-Schema.md +1 -1
  10. package/docs/Guides/Getting-Started.md +9 -5
  11. package/docs/Guides/Index.md +1 -1
  12. package/docs/Guides/Migration-Guide-V4.md +1 -1
  13. package/docs/Guides/Migration-Guide-V5.md +12 -2
  14. package/docs/Guides/Plugins-Guide.md +6 -6
  15. package/docs/Guides/Serverless.md +14 -48
  16. package/docs/Guides/Style-Guide.md +2 -2
  17. package/docs/Guides/Testing.md +2 -2
  18. package/docs/Guides/Write-Plugin.md +2 -3
  19. package/docs/Reference/ContentTypeParser.md +58 -78
  20. package/docs/Reference/Decorators.md +249 -60
  21. package/docs/Reference/Encapsulation.md +28 -33
  22. package/docs/Reference/Errors.md +52 -53
  23. package/docs/Reference/HTTP2.md +7 -7
  24. package/docs/Reference/Hooks.md +31 -30
  25. package/docs/Reference/LTS.md +10 -15
  26. package/docs/Reference/Lifecycle.md +19 -24
  27. package/docs/Reference/Logging.md +59 -56
  28. package/docs/Reference/Middleware.md +19 -19
  29. package/docs/Reference/Plugins.md +55 -71
  30. package/docs/Reference/Principles.md +25 -30
  31. package/docs/Reference/Reply.md +11 -10
  32. package/docs/Reference/Request.md +89 -98
  33. package/docs/Reference/Routes.md +108 -128
  34. package/docs/Reference/Server.md +18 -16
  35. package/docs/Reference/Type-Providers.md +19 -21
  36. package/docs/Reference/TypeScript.md +1 -18
  37. package/docs/Reference/Validation-and-Serialization.md +134 -159
  38. package/docs/Reference/Warnings.md +22 -25
  39. package/fastify.js +3 -2
  40. package/lib/contentTypeParser.js +7 -8
  41. package/lib/decorate.js +18 -3
  42. package/lib/error-handler.js +14 -12
  43. package/lib/errors.js +4 -0
  44. package/lib/headRoute.js +4 -2
  45. package/lib/pluginUtils.js +4 -2
  46. package/lib/reply.js +17 -2
  47. package/lib/request.js +28 -2
  48. package/lib/server.js +5 -0
  49. package/lib/validation.js +1 -1
  50. package/lib/warnings.js +9 -0
  51. package/lib/wrapThenable.js +8 -1
  52. package/package.json +12 -12
  53. package/test/bundler/esbuild/package.json +1 -1
  54. package/test/close.test.js +125 -108
  55. package/test/custom-parser-async.test.js +34 -36
  56. package/test/custom-parser.4.test.js +55 -38
  57. package/test/decorator.test.js +174 -4
  58. package/test/fastify-instance.test.js +12 -2
  59. package/test/genReqId.test.js +125 -174
  60. package/test/has-route.test.js +1 -3
  61. package/test/hooks.on-listen.test.js +17 -14
  62. package/test/internals/content-type-parser.test.js +1 -1
  63. package/test/internals/errors.test.js +14 -1
  64. package/test/issue-4959.test.js +84 -0
  65. package/test/listen.1.test.js +37 -34
  66. package/test/listen.2.test.js +50 -40
  67. package/test/listen.3.test.js +28 -32
  68. package/test/listen.4.test.js +61 -45
  69. package/test/listen.5.test.js +23 -0
  70. package/test/register.test.js +55 -50
  71. package/test/request-error.test.js +114 -94
  72. package/test/route-shorthand.test.js +36 -32
  73. package/test/stream.5.test.js +35 -33
  74. package/test/throw.test.js +87 -91
  75. package/test/toolkit.js +32 -0
  76. package/test/trust-proxy.test.js +23 -23
  77. package/test/types/instance.test-d.ts +4 -0
  78. package/test/types/reply.test-d.ts +1 -0
  79. package/test/types/request.test-d.ts +4 -0
  80. package/test/types/type-provider.test-d.ts +40 -0
  81. package/test/upgrade.test.js +32 -33
  82. package/types/instance.d.ts +6 -0
  83. package/types/reply.d.ts +1 -0
  84. package/types/request.d.ts +2 -0
  85. package/types/type-provider.d.ts +12 -3
@@ -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 proxyquire = require('proxyquire')
6
5
  const fs = require('node:fs')
7
6
  const Readable = require('node:stream').Readable
8
7
  const sget = require('simple-get').concat
9
8
  const Fastify = require('..')
10
9
 
11
- test('should destroy stream when response is ended', t => {
10
+ test('should destroy stream when response is ended', (t, done) => {
12
11
  t.plan(4)
13
12
  const stream = require('node:stream')
14
13
  const fastify = Fastify()
@@ -17,7 +16,7 @@ test('should destroy stream when response is ended', t => {
17
16
  const reallyLongStream = new stream.Readable({
18
17
  read: function () { },
19
18
  destroy: function (err, callback) {
20
- t.ok('called')
19
+ t.assert.ok('called')
21
20
  callback(err)
22
21
  }
23
22
  })
@@ -26,23 +25,25 @@ test('should destroy stream when response is ended', t => {
26
25
  })
27
26
 
28
27
  fastify.listen({ port: 0 }, err => {
29
- t.error(err)
30
- t.teardown(() => { fastify.close() })
28
+ t.assert.ifError(err)
29
+ t.after(() => fastify.close())
31
30
 
32
31
  sget(`http://localhost:${fastify.server.address().port}/error`, function (err, response) {
33
- t.error(err)
34
- t.equal(response.statusCode, 200)
32
+ t.assert.ifError(err)
33
+ t.assert.strictEqual(response.statusCode, 200)
34
+ done()
35
35
  })
36
36
  })
37
37
  })
38
38
 
39
- test('should mark reply as sent before pumping the payload stream into response for async route handler', t => {
39
+ test('should mark reply as sent before pumping the payload stream into response for async route handler', (t, done) => {
40
40
  t.plan(3)
41
+ t.after(() => fastify.close())
41
42
 
42
43
  const handleRequest = proxyquire('../lib/handleRequest', {
43
44
  './wrapThenable': (thenable, reply) => {
44
45
  thenable.then(function (payload) {
45
- t.equal(reply.sent, true)
46
+ t.assert.strictEqual(reply.sent, true)
46
47
  })
47
48
  }
48
49
  })
@@ -66,20 +67,20 @@ test('should mark reply as sent before pumping the payload stream into response
66
67
  url: '/',
67
68
  method: 'GET'
68
69
  }, (err, res) => {
69
- t.error(err)
70
- t.equal(res.payload, fs.readFileSync(__filename, 'utf8'))
71
- fastify.close()
70
+ t.assert.ifError(err)
71
+ t.assert.strictEqual(res.payload, fs.readFileSync(__filename, 'utf8'))
72
+ done()
72
73
  })
73
74
  })
74
75
 
75
- test('reply.send handles aborted requests', t => {
76
+ test('reply.send handles aborted requests', (t, done) => {
76
77
  t.plan(2)
77
78
 
78
79
  const spyLogger = {
79
80
  level: 'error',
80
81
  fatal: () => { },
81
82
  error: () => {
82
- t.fail('should not log an error')
83
+ t.assert.fail('should not log an error')
83
84
  },
84
85
  warn: () => { },
85
86
  info: () => { },
@@ -103,31 +104,31 @@ test('reply.send handles aborted requests', t => {
103
104
  })
104
105
 
105
106
  fastify.listen({ port: 0 }, err => {
106
- t.error(err)
107
- t.teardown(() => { fastify.close() })
107
+ t.assert.ifError(err)
108
+ t.after(() => fastify.close())
108
109
 
109
110
  const port = fastify.server.address().port
110
111
  const http = require('node:http')
111
112
  const req = http.get(`http://localhost:${port}`)
112
113
  .on('error', (err) => {
113
- t.equal(err.code, 'ECONNRESET')
114
- fastify.close()
114
+ t.assert.strictEqual(err.code, 'ECONNRESET')
115
+ done()
115
116
  })
116
117
 
117
118
  setTimeout(() => {
118
- req.abort()
119
+ req.destroy()
119
120
  }, 1)
120
121
  })
121
122
  })
122
123
 
123
- test('request terminated should not crash fastify', t => {
124
+ test('request terminated should not crash fastify', (t, done) => {
124
125
  t.plan(10)
125
126
 
126
127
  const spyLogger = {
127
128
  level: 'error',
128
129
  fatal: () => { },
129
130
  error: () => {
130
- t.fail('should not log an error')
131
+ t.assert.fail('should not log an error')
131
132
  },
132
133
  warn: () => { },
133
134
  info: () => { },
@@ -156,18 +157,18 @@ test('request terminated should not crash fastify', t => {
156
157
  })
157
158
 
158
159
  fastify.listen({ port: 0 }, err => {
159
- t.error(err)
160
- t.teardown(() => { fastify.close() })
160
+ t.assert.ifError(err)
161
+ t.after(() => fastify.close())
161
162
 
162
163
  const port = fastify.server.address().port
163
164
  const http = require('node:http')
164
165
  const req = http.get(`http://localhost:${port}`, function (res) {
165
166
  const { statusCode, headers } = res
166
- t.equal(statusCode, 200)
167
- t.equal(headers['content-type'], 'text/html; charset=utf-8')
168
- t.equal(headers['transfer-encoding'], 'chunked')
167
+ t.assert.strictEqual(statusCode, 200)
168
+ t.assert.strictEqual(headers['content-type'], 'text/html; charset=utf-8')
169
+ t.assert.strictEqual(headers['transfer-encoding'], 'chunked')
169
170
  res.on('data', function (chunk) {
170
- t.equal(chunk.toString(), '<h1>HTML</h1>')
171
+ t.assert.strictEqual(chunk.toString(), '<h1>HTML</h1>')
171
172
  })
172
173
 
173
174
  setTimeout(() => {
@@ -176,16 +177,17 @@ test('request terminated should not crash fastify', t => {
176
177
  // the server is not crash, we can connect it
177
178
  http.get(`http://localhost:${port}`, function (res) {
178
179
  const { statusCode, headers } = res
179
- t.equal(statusCode, 200)
180
- t.equal(headers['content-type'], 'text/html; charset=utf-8')
181
- t.equal(headers['transfer-encoding'], 'chunked')
180
+ t.assert.strictEqual(statusCode, 200)
181
+ t.assert.strictEqual(headers['content-type'], 'text/html; charset=utf-8')
182
+ t.assert.strictEqual(headers['transfer-encoding'], 'chunked')
182
183
  let payload = ''
183
184
  res.on('data', function (chunk) {
184
185
  payload += chunk.toString()
185
186
  })
186
187
  res.on('end', function () {
187
- t.equal(payload, '<h1>HTML</h1><h1>should display on second stream</h1>')
188
- t.pass('should end properly')
188
+ t.assert.strictEqual(payload, '<h1>HTML</h1><h1>should display on second stream</h1>')
189
+ t.assert.ok('should end properly')
190
+ done()
189
191
  })
190
192
  })
191
193
  }, 1)
@@ -1,20 +1,20 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('..')
5
5
 
6
- test('Fastify should throw on wrong options', t => {
6
+ test('Fastify should throw on wrong options', (t) => {
7
7
  t.plan(2)
8
8
  try {
9
9
  Fastify('lol')
10
- t.fail()
10
+ t.assert.fail()
11
11
  } catch (e) {
12
- t.equal(e.message, 'Options must be an object')
13
- t.pass()
12
+ t.assert.strictEqual(e.message, 'Options must be an object')
13
+ t.assert.ok(true)
14
14
  }
15
15
  })
16
16
 
17
- test('Fastify should throw on multiple assignment to the same route', t => {
17
+ test('Fastify should throw on multiple assignment to the same route', (t) => {
18
18
  t.plan(1)
19
19
  const fastify = Fastify()
20
20
 
@@ -22,14 +22,14 @@ test('Fastify should throw on multiple assignment to the same route', t => {
22
22
 
23
23
  try {
24
24
  fastify.get('/', () => {})
25
- t.fail('Should throw fastify duplicated route declaration')
25
+ t.assert.fail('Should throw fastify duplicated route declaration')
26
26
  } catch (error) {
27
- t.equal(error.code, 'FST_ERR_DUPLICATED_ROUTE')
27
+ t.assert.strictEqual(error.code, 'FST_ERR_DUPLICATED_ROUTE')
28
28
  }
29
29
  })
30
30
 
31
- test('Fastify should throw for an invalid schema, printing the error route - headers', t => {
32
- t.plan(2)
31
+ test('Fastify should throw for an invalid schema, printing the error route - headers', async (t) => {
32
+ t.plan(1)
33
33
 
34
34
  const badSchema = {
35
35
  type: 'object',
@@ -39,20 +39,18 @@ test('Fastify should throw for an invalid schema, printing the error route - hea
39
39
  }
40
40
  }
41
41
  }
42
-
43
42
  const fastify = Fastify()
44
43
  fastify.get('/', { schema: { headers: badSchema } }, () => {})
45
44
  fastify.get('/not-loaded', { schema: { headers: badSchema } }, () => {})
46
45
 
47
- fastify.ready(err => {
48
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
49
- t.match(err.message, /Failed building the validation schema for GET: \//)
46
+ await t.assert.rejects(fastify.ready(), {
47
+ code: 'FST_ERR_SCH_VALIDATION_BUILD',
48
+ message: /Failed building the validation schema for GET: \//
50
49
  })
51
50
  })
52
51
 
53
- test('Fastify should throw for an invalid schema, printing the error route - body', t => {
54
- t.plan(2)
55
-
52
+ test('Fastify should throw for an invalid schema, printing the error route - body', async (t) => {
53
+ t.plan(1)
56
54
  const badSchema = {
57
55
  type: 'object',
58
56
  properties: {
@@ -68,13 +66,13 @@ test('Fastify should throw for an invalid schema, printing the error route - bod
68
66
  done()
69
67
  }, { prefix: 'hello' })
70
68
 
71
- fastify.ready(err => {
72
- t.equal(err.code, 'FST_ERR_SCH_VALIDATION_BUILD')
73
- t.match(err.message, /Failed building the validation schema for POST: \/hello\/form/)
69
+ await t.assert.rejects(fastify.ready(), {
70
+ code: 'FST_ERR_SCH_VALIDATION_BUILD',
71
+ message: /Failed building the validation schema for POST: \/hello\/form/
74
72
  })
75
73
  })
76
74
 
77
- test('Should throw on unsupported method', t => {
75
+ test('Should throw on unsupported method', async (t) => {
78
76
  t.plan(1)
79
77
  const fastify = Fastify()
80
78
  try {
@@ -84,13 +82,13 @@ test('Should throw on unsupported method', t => {
84
82
  schema: {},
85
83
  handler: function (req, reply) {}
86
84
  })
87
- t.fail()
85
+ t.assert.fail()
88
86
  } catch (e) {
89
- t.pass()
87
+ t.assert.ok(true)
90
88
  }
91
89
  })
92
90
 
93
- test('Should throw on missing handler', t => {
91
+ test('Should throw on missing handler', (t) => {
94
92
  t.plan(1)
95
93
  const fastify = Fastify()
96
94
  try {
@@ -98,15 +96,15 @@ test('Should throw on missing handler', t => {
98
96
  method: 'GET',
99
97
  url: '/'
100
98
  })
101
- t.fail()
99
+ t.assert.fail()
102
100
  } catch (e) {
103
- t.pass()
101
+ t.assert.ok(true)
104
102
  }
105
103
  })
106
104
 
107
- test('Should throw if one method is unsupported', t => {
108
- const fastify = Fastify()
105
+ test('Should throw if one method is unsupported', async (t) => {
109
106
  t.plan(1)
107
+ const fastify = Fastify()
110
108
  try {
111
109
  fastify.route({
112
110
  method: ['GET', 'TROLL'],
@@ -114,28 +112,27 @@ test('Should throw if one method is unsupported', t => {
114
112
  schema: {},
115
113
  handler: function (req, reply) {}
116
114
  })
117
- t.fail()
115
+ t.assert.fail()
118
116
  } catch (e) {
119
- t.pass()
117
+ t.assert.ok(true)
120
118
  }
121
119
  })
122
120
 
123
- test('Should throw on duplicate content type parser', t => {
121
+ test('Should throw on duplicate content type parser', async (t) => {
124
122
  t.plan(1)
125
-
126
123
  const fastify = Fastify()
127
124
  function customParser (req, payload, done) { done(null, '') }
128
125
 
129
126
  fastify.addContentTypeParser('application/qq', customParser)
130
127
  try {
131
128
  fastify.addContentTypeParser('application/qq', customParser)
132
- t.fail()
129
+ t.assert.fail()
133
130
  } catch (e) {
134
- t.pass()
131
+ t.assert.ok(true)
135
132
  }
136
133
  })
137
134
 
138
- test('Should throw on duplicate decorator', t => {
135
+ test('Should throw on duplicate decorator', async (t) => {
139
136
  t.plan(1)
140
137
 
141
138
  const fastify = Fastify()
@@ -144,31 +141,30 @@ test('Should throw on duplicate decorator', t => {
144
141
  fastify.decorate('foo', fooObj)
145
142
  try {
146
143
  fastify.decorate('foo', fooObj)
147
- t.fail()
144
+ t.assert.fail()
148
145
  } catch (e) {
149
- t.pass()
146
+ t.assert.ok(true)
150
147
  }
151
148
  })
152
149
 
153
- test('Should not throw on duplicate decorator encapsulation', t => {
150
+ test('Should not throw on duplicate decorator encapsulation', async (t) => {
154
151
  t.plan(1)
155
-
156
152
  const fastify = Fastify()
157
153
  const foo2Obj = {}
158
154
 
159
155
  fastify.decorate('foo2', foo2Obj)
160
156
 
161
157
  fastify.register(function (fastify, opts, done) {
162
- t.doesNotThrow(() => {
158
+ t.assert.doesNotThrow(() => {
163
159
  fastify.decorate('foo2', foo2Obj)
164
160
  })
165
161
  done()
166
162
  })
167
163
 
168
- fastify.ready()
164
+ await fastify.ready()
169
165
  })
170
166
 
171
- test('Should throw on duplicate request decorator', t => {
167
+ test('Should throw on duplicate request decorator', async (t) => {
172
168
  t.plan(2)
173
169
 
174
170
  const fastify = Fastify()
@@ -176,28 +172,28 @@ test('Should throw on duplicate request decorator', t => {
176
172
  fastify.decorateRequest('foo', null)
177
173
  try {
178
174
  fastify.decorateRequest('foo', null)
179
- t.fail()
175
+ t.assert.fail()
180
176
  } catch (e) {
181
- t.equal(e.code, 'FST_ERR_DEC_ALREADY_PRESENT')
182
- t.equal(e.message, 'The decorator \'foo\' has already been added!')
177
+ t.assert.strictEqual(e.code, 'FST_ERR_DEC_ALREADY_PRESENT')
178
+ t.assert.strictEqual(e.message, 'The decorator \'foo\' has already been added!')
183
179
  }
184
180
  })
185
181
 
186
- test('Should throw if request decorator dependencies are not met', t => {
182
+ test('Should throw if request decorator dependencies are not met', async (t) => {
187
183
  t.plan(2)
188
184
 
189
185
  const fastify = Fastify()
190
186
 
191
187
  try {
192
188
  fastify.decorateRequest('bar', null, ['world'])
193
- t.fail()
189
+ t.assert.fail()
194
190
  } catch (e) {
195
- t.equal(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
196
- t.equal(e.message, 'The decorator is missing dependency \'world\'.')
191
+ t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
192
+ t.assert.strictEqual(e.message, 'The decorator is missing dependency \'world\'.')
197
193
  }
198
194
  })
199
195
 
200
- test('Should throw on duplicate reply decorator', t => {
196
+ test('Should throw on duplicate reply decorator', async (t) => {
201
197
  t.plan(1)
202
198
 
203
199
  const fastify = Fastify()
@@ -205,149 +201,149 @@ test('Should throw on duplicate reply decorator', t => {
205
201
  fastify.decorateReply('foo', null)
206
202
  try {
207
203
  fastify.decorateReply('foo', null)
208
- t.fail()
204
+ t.assert.fail()
209
205
  } catch (e) {
210
- t.ok(/has already been added/.test(e.message))
206
+ t.assert.ok(/has already been added/.test(e.message))
211
207
  }
212
208
  })
213
209
 
214
- test('Should throw if reply decorator dependencies are not met', t => {
210
+ test('Should throw if reply decorator dependencies are not met', async (t) => {
215
211
  t.plan(1)
216
212
 
217
213
  const fastify = Fastify()
218
214
 
219
215
  try {
220
216
  fastify.decorateReply('bar', null, ['world'])
221
- t.fail()
217
+ t.assert.fail()
222
218
  } catch (e) {
223
- t.ok(/missing dependency/.test(e.message))
219
+ t.assert.ok(/missing dependency/.test(e.message))
224
220
  }
225
221
  })
226
222
 
227
- test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
223
+ test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', async (t) => {
228
224
  t.plan(5)
229
225
 
230
226
  const fastify = Fastify()
231
227
 
232
228
  try {
233
229
  fastify.get('/foo/1', '')
234
- t.fail()
230
+ t.assert.fail()
235
231
  } catch (e) {
236
- t.pass()
232
+ t.assert.ok(true)
237
233
  }
238
234
 
239
235
  try {
240
236
  fastify.get('/foo/2', 1)
241
- t.fail()
237
+ t.assert.fail()
242
238
  } catch (e) {
243
- t.pass()
239
+ t.assert.ok(true)
244
240
  }
245
241
 
246
242
  try {
247
243
  fastify.get('/foo/3', [])
248
- t.fail()
244
+ t.assert.fail()
249
245
  } catch (e) {
250
- t.pass()
246
+ t.assert.ok(true)
251
247
  }
252
248
 
253
249
  try {
254
250
  fastify.get('/foo/4', undefined)
255
- t.fail()
251
+ t.assert.fail()
256
252
  } catch (e) {
257
- t.pass()
253
+ t.assert.ok(true)
258
254
  }
259
255
 
260
256
  try {
261
257
  fastify.get('/foo/5', null)
262
- t.fail()
258
+ t.assert.fail()
263
259
  } catch (e) {
264
- t.pass()
260
+ t.assert.ok(true)
265
261
  }
266
262
  })
267
263
 
268
- test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
264
+ test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', async (t) => {
269
265
  t.plan(5)
270
266
 
271
267
  const fastify = Fastify()
272
268
 
273
269
  try {
274
270
  fastify.get('/foo/1', '')
275
- t.fail()
271
+ t.assert.fail()
276
272
  } catch (e) {
277
- t.pass()
273
+ t.assert.ok(true)
278
274
  }
279
275
 
280
276
  try {
281
277
  fastify.get('/foo/2', 1)
282
- t.fail()
278
+ t.assert.fail()
283
279
  } catch (e) {
284
- t.pass()
280
+ t.assert.ok(true)
285
281
  }
286
282
 
287
283
  try {
288
284
  fastify.get('/foo/3', [])
289
- t.fail()
285
+ t.assert.fail()
290
286
  } catch (e) {
291
- t.pass()
287
+ t.assert.ok(true)
292
288
  }
293
289
 
294
290
  try {
295
291
  fastify.get('/foo/4', undefined)
296
- t.fail()
292
+ t.assert.fail()
297
293
  } catch (e) {
298
- t.pass()
294
+ t.assert.ok(true)
299
295
  }
300
296
 
301
297
  try {
302
298
  fastify.get('/foo/5', null)
303
- t.fail()
299
+ t.assert.fail()
304
300
  } catch (e) {
305
- t.pass()
301
+ t.assert.ok(true)
306
302
  }
307
303
  })
308
304
 
309
- test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', t => {
305
+ test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', async (t) => {
310
306
  t.plan(5)
311
307
 
312
308
  const fastify = Fastify()
313
309
 
314
310
  try {
315
311
  fastify.get('/foo/1', '', (req, res) => {})
316
- t.fail()
312
+ t.assert.fail()
317
313
  } catch (e) {
318
- t.pass()
314
+ t.assert.ok(true)
319
315
  }
320
316
 
321
317
  try {
322
318
  fastify.get('/foo/2', 1, (req, res) => {})
323
- t.fail()
319
+ t.assert.fail()
324
320
  } catch (e) {
325
- t.pass()
321
+ t.assert.ok(true)
326
322
  }
327
323
 
328
324
  try {
329
325
  fastify.get('/foo/3', [], (req, res) => {})
330
- t.fail()
326
+ t.assert.fail()
331
327
  } catch (e) {
332
- t.pass()
328
+ t.assert.ok(true)
333
329
  }
334
330
 
335
331
  try {
336
332
  fastify.get('/foo/4', undefined, (req, res) => {})
337
- t.fail()
333
+ t.assert.fail()
338
334
  } catch (e) {
339
- t.pass()
335
+ t.assert.ok(true)
340
336
  }
341
337
 
342
338
  try {
343
339
  fastify.get('/foo/5', null, (req, res) => {})
344
- t.fail()
340
+ t.assert.fail()
345
341
  } catch (e) {
346
- t.pass()
342
+ t.assert.ok(true)
347
343
  }
348
344
  })
349
345
 
350
- test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', t => {
346
+ test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', async (t) => {
351
347
  t.plan(1)
352
348
 
353
349
  const fastify = Fastify()
@@ -356,8 +352,8 @@ test('Should throw if found duplicate handler as the third parameter to the shor
356
352
  fastify.get('/foo/abc', {
357
353
  handler: (req, res) => {}
358
354
  }, (req, res) => {})
359
- t.fail()
355
+ t.assert.fail()
360
356
  } catch (e) {
361
- t.pass()
357
+ t.assert.ok(true)
362
358
  }
363
359
  })
@@ -0,0 +1,32 @@
1
+ 'use strict'
2
+
3
+ exports.waitForCb = function (options) {
4
+ let count = null
5
+ let done = false
6
+ let iResolve
7
+ let iReject
8
+
9
+ function stepIn () {
10
+ if (done) {
11
+ iReject(new Error('Unexpected done call'))
12
+ return
13
+ }
14
+
15
+ if (--count) {
16
+ return
17
+ }
18
+
19
+ done = true
20
+ iResolve()
21
+ }
22
+
23
+ const patience = new Promise((resolve, reject) => {
24
+ iResolve = resolve
25
+ iReject = reject
26
+ })
27
+
28
+ count = options.steps || 1
29
+ done = false
30
+
31
+ return { stepIn, patience }
32
+ }