Skip to content

Commit eb6d790

Browse files
authored
chore(tests): removed simple-get (#239)
* chore: removed simple-get for 404 file * chore: removed simple-get from basic test * chore: migrate enhance-request and middleware
1 parent 94c7431 commit eb6d790

5 files changed

Lines changed: 333 additions & 495 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"helmet": "^8.0.0",
6969
"neostandard": "^0.12.0",
7070
"serve-static": "^2.2.0",
71-
"simple-get": "^4.0.1",
7271
"tsd": "^0.32.0"
7372
},
7473
"dependencies": {

test/404s.test.js

Lines changed: 66 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
const { test } = require('node:test')
44
const fp = require('fastify-plugin')
55
const Fastify = require('fastify')
6-
const sget = require('simple-get').concat
76
const middiePlugin = require('../index')
87

9-
test('run hooks and middleware on default 404', (t, done) => {
10-
t.plan(8)
8+
test('run hooks and middleware on default 404', async (t) => {
9+
t.plan(7)
1110

1211
const fastify = Fastify()
1312

@@ -46,24 +45,20 @@ test('run hooks and middleware on default 404', (t, done) => {
4645

4746
t.after(() => fastify.close())
4847

49-
fastify.listen({ port: 0 }, err => {
50-
t.assert.ifError(err)
51-
52-
sget({
53-
method: 'PUT',
54-
url: 'http://localhost:' + fastify.server.address().port,
55-
body: JSON.stringify({ hello: 'world' }),
56-
headers: { 'Content-Type': 'application/json' }
57-
}, (err, response) => {
58-
t.assert.ifError(err)
59-
t.assert.strictEqual(response.statusCode, 404)
60-
done()
61-
})
48+
const fastifyServerAddress = await fastify.listen({ port: 0 })
49+
50+
const result = await fetch(fastifyServerAddress, {
51+
method: 'PUT',
52+
body: JSON.stringify({ hello: 'world' }),
53+
headers: { 'Content-Type': 'application/json' }
6254
})
55+
56+
t.assert.ok(!result.ok)
57+
t.assert.strictEqual(result.status, 404)
6358
})
6459

65-
test('run non-encapsulated plugin hooks and middleware on default 404', (t, done) => {
66-
t.plan(8)
60+
test('run non-encapsulated plugin hooks and middleware on default 404', async (t) => {
61+
t.plan(7)
6762

6863
const fastify = Fastify()
6964
t.after(() => fastify.close())
@@ -102,23 +97,20 @@ test('run non-encapsulated plugin hooks and middleware on default 404', (t, done
10297
reply.send({ hello: 'world' })
10398
})
10499

105-
fastify.listen({ port: 0 }, (err, address) => {
106-
t.assert.ifError(err)
107-
sget({
108-
method: 'POST',
109-
url: address,
110-
body: JSON.stringify({ hello: 'world' }),
111-
headers: { 'Content-Type': 'application/json' }
112-
}, (err, response) => {
113-
t.assert.ifError(err)
114-
t.assert.strictEqual(response.statusCode, 404)
115-
done()
116-
})
100+
const address = await fastify.listen({ port: 0 })
101+
102+
const response = await fetch(address, {
103+
method: 'POST',
104+
body: JSON.stringify({ hello: 'world' }),
105+
headers: { 'Content-Type': 'application/json' }
117106
})
107+
108+
t.assert.ok(!response.ok)
109+
t.assert.strictEqual(response.status, 404)
118110
})
119111

120-
test('run non-encapsulated plugin hooks and middleware on custom 404', (t, done) => {
121-
t.plan(14)
112+
test('run non-encapsulated plugin hooks and middleware on custom 404', async (t) => {
113+
t.plan(13)
122114

123115
const fastify = Fastify()
124116
t.after(() => fastify.close())
@@ -165,22 +157,20 @@ test('run non-encapsulated plugin hooks and middleware on custom 404', (t, done)
165157

166158
fastify.register(plugin) // Registering plugin after handler also works
167159

168-
fastify.listen({ port: 0 }, (err, address) => {
169-
t.assert.ifError(err)
170-
sget({
171-
method: 'GET',
172-
url: address + '/not-found'
173-
}, (err, response, body) => {
174-
t.assert.ifError(err)
175-
t.assert.strictEqual(body.toString(), 'this was not found')
176-
t.assert.strictEqual(response.statusCode, 404)
177-
done()
178-
})
160+
const address = await fastify.listen({ port: 0 })
161+
162+
const response = await fetch(address + '/not-found', {
163+
method: 'GET'
179164
})
165+
166+
const body = await response.text()
167+
t.assert.strictEqual(body, 'this was not found')
168+
t.assert.ok(!response.ok)
169+
t.assert.strictEqual(response.status, 404)
180170
})
181171

182-
test('run hooks and middleware with encapsulated 404', (t, done) => {
183-
t.plan(13)
172+
test('run hooks and middleware with encapsulated 404', async (t) => {
173+
t.plan(12)
184174

185175
const fastify = Fastify()
186176

@@ -248,24 +238,20 @@ test('run hooks and middleware with encapsulated 404', (t, done) => {
248238

249239
t.after(() => fastify.close())
250240

251-
fastify.listen({ port: 0 }, err => {
252-
t.assert.ifError(err)
253-
254-
sget({
255-
method: 'PUT',
256-
url: 'http://localhost:' + fastify.server.address().port + '/test',
257-
body: JSON.stringify({ hello: 'world' }),
258-
headers: { 'Content-Type': 'application/json' }
259-
}, (err, response) => {
260-
t.assert.ifError(err)
261-
t.assert.strictEqual(response.statusCode, 404)
262-
done()
263-
})
241+
const address = await fastify.listen({ port: 0 })
242+
243+
const response = await fetch(address + '/test', {
244+
method: 'PUT',
245+
body: JSON.stringify({ hello: 'world' }),
246+
headers: { 'Content-Type': 'application/json' }
264247
})
248+
249+
t.assert.ok(!response.ok)
250+
t.assert.strictEqual(response.status, 404)
265251
})
266252

267-
test('run middlewares on default 404', (t, done) => {
268-
t.plan(4)
253+
test('run middlewares on default 404', async (t) => {
254+
t.plan(3)
269255

270256
const fastify = Fastify()
271257
fastify
@@ -283,24 +269,20 @@ test('run middlewares on default 404', (t, done) => {
283269

284270
t.after(() => fastify.close())
285271

286-
fastify.listen({ port: 0 }, err => {
287-
t.assert.ifError(err)
288-
289-
sget({
290-
method: 'PUT',
291-
url: 'http://localhost:' + fastify.server.address().port,
292-
body: JSON.stringify({ hello: 'world' }),
293-
headers: { 'Content-Type': 'application/json' }
294-
}, (err, response) => {
295-
t.assert.ifError(err)
296-
t.assert.strictEqual(response.statusCode, 404)
297-
done()
298-
})
272+
const address = await fastify.listen({ port: 0 })
273+
274+
const response = await fetch(address, {
275+
method: 'PUT',
276+
body: JSON.stringify({ hello: 'world' }),
277+
headers: { 'Content-Type': 'application/json' }
299278
})
279+
280+
t.assert.ok(!response.ok)
281+
t.assert.strictEqual(response.status, 404)
300282
})
301283

302-
test('run middlewares with encapsulated 404', (t, done) => {
303-
t.plan(5)
284+
test('run middlewares with encapsulated 404', async (t) => {
285+
t.plan(4)
304286

305287
const fastify = Fastify()
306288
fastify
@@ -327,18 +309,14 @@ test('run middlewares with encapsulated 404', (t, done) => {
327309

328310
t.after(() => fastify.close())
329311

330-
fastify.listen({ port: 0 }, err => {
331-
t.assert.ifError(err)
332-
333-
sget({
334-
method: 'PUT',
335-
url: 'http://localhost:' + fastify.server.address().port + '/test',
336-
body: JSON.stringify({ hello: 'world' }),
337-
headers: { 'Content-Type': 'application/json' }
338-
}, (err, response) => {
339-
t.assert.ifError(err)
340-
t.assert.strictEqual(response.statusCode, 404)
341-
done()
342-
})
312+
const address = await fastify.listen({ port: 0 })
313+
314+
const response = await fetch(address + '/test', {
315+
method: 'PUT',
316+
body: JSON.stringify({ hello: 'world' }),
317+
headers: { 'Content-Type': 'application/json' }
343318
})
319+
320+
t.assert.ok(!response.ok)
321+
t.assert.strictEqual(response.status, 404)
344322
})

0 commit comments

Comments
 (0)