Skip to content

Commit 0a9aa87

Browse files
authored
test: migrate stream 4 to node test runner (#6062)
1 parent 417b1f8 commit 0a9aa87

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

test/stream.4.test.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

3-
const t = require('tap')
4-
const test = t.test
3+
const { test } = require('node:test')
54
const sget = require('simple-get').concat
65
const errors = require('http-errors')
76
const JSONStream = require('JSONStream')
@@ -10,7 +9,7 @@ const split = require('split2')
109
const Fastify = require('..')
1110
const { kDisableRequestLogging } = require('../lib/symbols.js')
1211

13-
test('Destroying streams prematurely should call abort method', t => {
12+
test('Destroying streams prematurely should call abort method', (t, testDone) => {
1413
t.plan(7)
1514

1615
let fastify = null
@@ -23,21 +22,22 @@ test('Destroying streams prematurely should call abort method', t => {
2322
}
2423
})
2524
} catch (e) {
26-
t.fail()
25+
t.assert.fail()
2726
}
2827
const stream = require('node:stream')
2928
const http = require('node:http')
3029

3130
// Test that "premature close" errors are logged with level warn
3231
logStream.on('data', line => {
3332
if (line.res) {
34-
t.equal(line.msg, 'stream closed prematurely')
35-
t.equal(line.level, 30)
33+
t.assert.strictEqual(line.msg, 'stream closed prematurely')
34+
t.assert.strictEqual(line.level, 30)
35+
testDone()
3636
}
3737
})
3838

3939
fastify.get('/', function (request, reply) {
40-
t.pass('Received request')
40+
t.assert.ok('Received request')
4141

4242
let sent = false
4343
const reallyLongStream = new stream.Readable({
@@ -50,30 +50,30 @@ test('Destroying streams prematurely should call abort method', t => {
5050
})
5151
reallyLongStream.destroy = undefined
5252
reallyLongStream.close = undefined
53-
reallyLongStream.abort = () => t.ok('called')
53+
reallyLongStream.abort = () => t.assert.ok('called')
5454
reply.send(reallyLongStream)
5555
})
5656

5757
fastify.listen({ port: 0 }, err => {
58-
t.error(err)
59-
t.teardown(() => { fastify.close() })
58+
t.assert.ifError(err)
59+
t.after(() => { fastify.close() })
6060

6161
const port = fastify.server.address().port
6262

6363
http.get(`http://localhost:${port}`, function (response) {
64-
t.equal(response.statusCode, 200)
64+
t.assert.strictEqual(response.statusCode, 200)
6565
response.on('readable', function () {
6666
response.destroy()
6767
})
6868
// Node bug? Node never emits 'close' here.
6969
response.on('aborted', function () {
70-
t.pass('Response closed')
70+
t.assert.ok('Response closed')
7171
})
7272
})
7373
})
7474
})
7575

76-
test('Destroying streams prematurely, log is disabled', t => {
76+
test('Destroying streams prematurely, log is disabled', (t, testDone) => {
7777
t.plan(4)
7878

7979
let fastify = null
@@ -82,7 +82,7 @@ test('Destroying streams prematurely, log is disabled', t => {
8282
logger: false
8383
})
8484
} catch (e) {
85-
t.fail()
85+
t.assert.fail()
8686
}
8787
const stream = require('node:stream')
8888
const http = require('node:http')
@@ -100,30 +100,33 @@ test('Destroying streams prematurely, log is disabled', t => {
100100
}
101101
})
102102
reallyLongStream.destroy = true
103-
reallyLongStream.close = () => t.ok('called')
103+
reallyLongStream.close = () => {
104+
t.assert.ok('called')
105+
testDone()
106+
}
104107
reply.send(reallyLongStream)
105108
})
106109

107110
fastify.listen({ port: 0 }, err => {
108-
t.error(err)
109-
t.teardown(() => { fastify.close() })
111+
t.assert.ifError(err)
112+
t.after(() => { fastify.close() })
110113

111114
const port = fastify.server.address().port
112115

113116
http.get(`http://localhost:${port}`, function (response) {
114-
t.equal(response.statusCode, 200)
117+
t.assert.strictEqual(response.statusCode, 200)
115118
response.on('readable', function () {
116119
response.destroy()
117120
})
118121
// Node bug? Node never emits 'close' here.
119122
response.on('aborted', function () {
120-
t.pass('Response closed')
123+
t.assert.ok('Response closed')
121124
})
122125
})
123126
})
124127
})
125128

126-
test('should respond with a stream1', t => {
129+
test('should respond with a stream1', (t, testDone) => {
127130
t.plan(5)
128131
const fastify = Fastify()
129132

@@ -135,25 +138,26 @@ test('should respond with a stream1', t => {
135138
})
136139

137140
fastify.listen({ port: 0 }, err => {
138-
t.error(err)
139-
t.teardown(() => { fastify.close() })
141+
t.assert.ifError(err)
142+
t.after(() => { fastify.close() })
140143

141144
sget(`http://localhost:${fastify.server.address().port}`, function (err, response, body) {
142-
t.error(err)
143-
t.equal(response.headers['content-type'], 'application/json')
144-
t.equal(response.statusCode, 200)
145-
t.same(JSON.parse(body), [{ hello: 'world' }, { a: 42 }])
145+
t.assert.ifError(err)
146+
t.assert.strictEqual(response.headers['content-type'], 'application/json')
147+
t.assert.strictEqual(response.statusCode, 200)
148+
t.assert.deepStrictEqual(JSON.parse(body), [{ hello: 'world' }, { a: 42 }])
149+
testDone()
146150
})
147151
})
148152
})
149153

150-
test('return a 404 if the stream emits a 404 error', t => {
154+
test('return a 404 if the stream emits a 404 error', (t, testDone) => {
151155
t.plan(5)
152156

153157
const fastify = Fastify()
154158

155159
fastify.get('/', function (request, reply) {
156-
t.pass('Received request')
160+
t.assert.ok('Received request')
157161

158162
const reallyLongStream = new Readable({
159163
read: function () {
@@ -167,15 +171,16 @@ test('return a 404 if the stream emits a 404 error', t => {
167171
})
168172

169173
fastify.listen({ port: 0 }, err => {
170-
t.error(err)
171-
t.teardown(() => { fastify.close() })
174+
t.assert.ifError(err)
175+
t.after(() => { fastify.close() })
172176

173177
const port = fastify.server.address().port
174178

175179
sget(`http://localhost:${port}`, function (err, response) {
176-
t.error(err)
177-
t.equal(response.headers['content-type'], 'application/json; charset=utf-8')
178-
t.equal(response.statusCode, 404)
180+
t.assert.ifError(err)
181+
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
182+
t.assert.strictEqual(response.statusCode, 404)
183+
testDone()
179184
})
180185
})
181186
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy