Skip to content

Commit fbd7411

Browse files
authored
test: migrated stream.5.test.js from tap to node:test (fastify#5955)
1 parent dd358cb commit fbd7411

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

test/stream.5.test.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict'
22

3-
const t = require('tap')
4-
const test = t.test
3+
const { test } = require('node:test')
54
const proxyquire = require('proxyquire')
65
const fs = require('node:fs')
76
const Readable = require('node:stream').Readable
87
const sget = require('simple-get').concat
98
const Fastify = require('..')
109

11-
test('should destroy stream when response is ended', t => {
10+
test('should destroy stream when response is ended', (t, done) => {
1211
t.plan(4)
1312
const stream = require('node:stream')
1413
const fastify = Fastify()
@@ -17,7 +16,7 @@ test('should destroy stream when response is ended', t => {
1716
const reallyLongStream = new stream.Readable({
1817
read: function () { },
1918
destroy: function (err, callback) {
20-
t.ok('called')
19+
t.assert.ok('called')
2120
callback(err)
2221
}
2322
})
@@ -26,23 +25,25 @@ test('should destroy stream when response is ended', t => {
2625
})
2726

2827
fastify.listen({ port: 0 }, err => {
29-
t.error(err)
30-
t.teardown(() => { fastify.close() })
28+
t.assert.ifError(err)
29+
t.after(() => fastify.close())
3130

3231
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()
3535
})
3636
})
3737
})
3838

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) => {
4040
t.plan(3)
41+
t.after(() => fastify.close())
4142

4243
const handleRequest = proxyquire('../lib/handleRequest', {
4344
'./wrapThenable': (thenable, reply) => {
4445
thenable.then(function (payload) {
45-
t.equal(reply.sent, true)
46+
t.assert.strictEqual(reply.sent, true)
4647
})
4748
}
4849
})
@@ -66,20 +67,20 @@ test('should mark reply as sent before pumping the payload stream into response
6667
url: '/',
6768
method: 'GET'
6869
}, (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()
7273
})
7374
})
7475

75-
test('reply.send handles aborted requests', t => {
76+
test('reply.send handles aborted requests', (t, done) => {
7677
t.plan(2)
7778

7879
const spyLogger = {
7980
level: 'error',
8081
fatal: () => { },
8182
error: () => {
82-
t.fail('should not log an error')
83+
t.assert.fail('should not log an error')
8384
},
8485
warn: () => { },
8586
info: () => { },
@@ -103,31 +104,31 @@ test('reply.send handles aborted requests', t => {
103104
})
104105

105106
fastify.listen({ port: 0 }, err => {
106-
t.error(err)
107-
t.teardown(() => { fastify.close() })
107+
t.assert.ifError(err)
108+
t.after(() => fastify.close())
108109

109110
const port = fastify.server.address().port
110111
const http = require('node:http')
111112
const req = http.get(`http://localhost:${port}`)
112113
.on('error', (err) => {
113-
t.equal(err.code, 'ECONNRESET')
114-
fastify.close()
114+
t.assert.strictEqual(err.code, 'ECONNRESET')
115+
done()
115116
})
116117

117118
setTimeout(() => {
118-
req.abort()
119+
req.destroy()
119120
}, 1)
120121
})
121122
})
122123

123-
test('request terminated should not crash fastify', t => {
124+
test('request terminated should not crash fastify', (t, done) => {
124125
t.plan(10)
125126

126127
const spyLogger = {
127128
level: 'error',
128129
fatal: () => { },
129130
error: () => {
130-
t.fail('should not log an error')
131+
t.assert.fail('should not log an error')
131132
},
132133
warn: () => { },
133134
info: () => { },
@@ -156,18 +157,18 @@ test('request terminated should not crash fastify', t => {
156157
})
157158

158159
fastify.listen({ port: 0 }, err => {
159-
t.error(err)
160-
t.teardown(() => { fastify.close() })
160+
t.assert.ifError(err)
161+
t.after(() => fastify.close())
161162

162163
const port = fastify.server.address().port
163164
const http = require('node:http')
164165
const req = http.get(`http://localhost:${port}`, function (res) {
165166
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')
169170
res.on('data', function (chunk) {
170-
t.equal(chunk.toString(), '<h1>HTML</h1>')
171+
t.assert.strictEqual(chunk.toString(), '<h1>HTML</h1>')
171172
})
172173

173174
setTimeout(() => {
@@ -176,16 +177,17 @@ test('request terminated should not crash fastify', t => {
176177
// the server is not crash, we can connect it
177178
http.get(`http://localhost:${port}`, function (res) {
178179
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')
182183
let payload = ''
183184
res.on('data', function (chunk) {
184185
payload += chunk.toString()
185186
})
186187
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()
189191
})
190192
})
191193
}, 1)

0 commit comments

Comments
 (0)