Skip to content

Commit 7c31a2b

Browse files
SUZUKI Sosukecrysmags
authored andcommitted
feat: port abort-event-emitter.js tests to node:test runnner (nodejs#2565)
1 parent 91ee826 commit 7c31a2b

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

test/abort-event-emitter.js renamed to test/node-test/abort-event-emitter.js

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { test } = require('node:test')
44
const EventEmitter = require('events')
5-
const { Client, errors } = require('..')
5+
const { Client, errors } = require('../..')
66
const { createServer } = require('http')
77
const { createReadStream } = require('fs')
88
const { Readable } = require('stream')
9-
const { wrapWithAsyncIterable } = require('./utils/async-iterators')
9+
const { tspl } = require('@matteo.collina/tspl')
10+
const { wrapWithAsyncIterable } = require('../utils/async-iterators')
1011

11-
test('Abort before sending request (no body)', (t) => {
12-
t.plan(4)
12+
test('Abort before sending request (no body)', async (t) => {
13+
const p = tspl(t, { plan: 4 })
1314

1415
let count = 0
1516
const server = createServer((req, res) => {
1617
if (count === 1) {
17-
t.fail('The second request should never be executed')
18+
p.fail('The second request should never be executed')
1819
}
1920
count += 1
2021
res.end('hello')
2122
})
2223

23-
t.teardown(server.close.bind(server))
24+
t.after(server.close.bind(server))
2425

2526
server.listen(0, () => {
2627
const client = new Client(`http://localhost:${server.address().port}`)
2728
const ee = new EventEmitter()
28-
t.teardown(client.destroy.bind(client))
29+
t.after(client.destroy.bind(client))
2930

3031
client.request({ path: '/', method: 'GET' }, (err, response) => {
31-
t.error(err)
32+
p.ifError(err)
3233
const bufs = []
3334
response.body.on('data', (buf) => {
3435
bufs.push(buf)
3536
})
3637
response.body.on('end', () => {
37-
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
38+
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
3839
})
3940
})
4041

4142
const body = new Readable({ read () { } })
4243
body.on('error', (err) => {
43-
t.type(err, errors.RequestAbortedError)
44+
p.ok(err instanceof errors.RequestAbortedError)
4445
})
4546
client.request({
4647
path: '/',
4748
method: 'GET',
4849
signal: ee,
4950
body
5051
}, (err, response) => {
51-
t.type(err, errors.RequestAbortedError)
52+
p.ok(err instanceof errors.RequestAbortedError)
5253
})
5354

5455
ee.emit('abort')
5556
})
57+
58+
await p.completed
5659
})
5760

58-
test('Abort before sending request (no body) async iterator', (t) => {
59-
t.plan(3)
61+
test('Abort before sending request (no body) async iterator', async (t) => {
62+
const p = tspl(t, { plan: 3 })
6063

6164
let count = 0
6265
const server = createServer((req, res) => {
@@ -67,21 +70,21 @@ test('Abort before sending request (no body) async iterator', (t) => {
6770
res.end('hello')
6871
})
6972

70-
t.teardown(server.close.bind(server))
73+
t.after(server.close.bind(server))
7174

7275
server.listen(0, () => {
7376
const client = new Client(`http://localhost:${server.address().port}`)
7477
const ee = new EventEmitter()
75-
t.teardown(client.destroy.bind(client))
78+
t.after(client.destroy.bind(client))
7679

7780
client.request({ path: '/', method: 'GET' }, (err, response) => {
78-
t.error(err)
81+
p.ifError(err)
7982
const bufs = []
8083
response.body.on('data', (buf) => {
8184
bufs.push(buf)
8285
})
8386
response.body.on('end', () => {
84-
t.equal('hello', Buffer.concat(bufs).toString('utf8'))
87+
p.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
8588
})
8689
})
8790

@@ -92,36 +95,40 @@ test('Abort before sending request (no body) async iterator', (t) => {
9295
signal: ee,
9396
body
9497
}, (err, response) => {
95-
t.type(err, errors.RequestAbortedError)
98+
p.ok(err instanceof errors.RequestAbortedError)
9699
})
97100

98101
ee.emit('abort')
99102
})
103+
104+
await p.completed
100105
})
101106

102-
test('Abort while waiting response (no body)', (t) => {
103-
t.plan(1)
107+
test('Abort while waiting response (no body)', async (t) => {
108+
const p = tspl(t, { plan: 1 })
104109

105110
const ee = new EventEmitter()
106111
const server = createServer((req, res) => {
107112
ee.emit('abort')
108113
res.setHeader('content-type', 'text/plain')
109114
res.end('hello world')
110115
})
111-
t.teardown(server.close.bind(server))
116+
t.after(server.close.bind(server))
112117

113118
server.listen(0, () => {
114119
const client = new Client(`http://localhost:${server.address().port}`)
115-
t.teardown(client.destroy.bind(client))
120+
t.after(client.destroy.bind(client))
116121

117122
client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
118-
t.type(err, errors.RequestAbortedError)
123+
p.ok(err instanceof errors.RequestAbortedError)
119124
})
120125
})
126+
127+
await p.completed
121128
})
122129

123-
test('Abort while waiting response (write headers started) (no body)', (t) => {
124-
t.plan(1)
130+
test('Abort while waiting response (write headers started) (no body)', async (t) => {
131+
const p = tspl(t, { plan: 1 })
125132

126133
const ee = new EventEmitter()
127134
const server = createServer((req, res) => {
@@ -130,64 +137,68 @@ test('Abort while waiting response (write headers started) (no body)', (t) => {
130137
ee.emit('abort')
131138
res.end('hello world')
132139
})
133-
t.teardown(server.close.bind(server))
140+
t.after(server.close.bind(server))
134141

135142
server.listen(0, () => {
136143
const client = new Client(`http://localhost:${server.address().port}`)
137-
t.teardown(client.destroy.bind(client))
144+
t.after(client.destroy.bind(client))
138145

139146
client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
140-
t.type(err, errors.RequestAbortedError)
147+
p.ok(err instanceof errors.RequestAbortedError)
141148
})
142149
})
150+
151+
await p.completed
143152
})
144153

145-
test('Abort while waiting response (write headers and write body started) (no body)', (t) => {
146-
t.plan(2)
154+
test('Abort while waiting response (write headers and write body started) (no body)', async (t) => {
155+
const p = tspl(t, { plan: 2 })
147156

148157
const ee = new EventEmitter()
149158
const server = createServer((req, res) => {
150159
res.writeHead(200, { 'content-type': 'text/plain' })
151160
res.write('hello')
152161
})
153-
t.teardown(server.close.bind(server))
162+
t.after(server.close.bind(server))
154163

155164
server.listen(0, () => {
156165
const client = new Client(`http://localhost:${server.address().port}`)
157-
t.teardown(client.destroy.bind(client))
166+
t.after(client.destroy.bind(client))
158167

159168
client.request({ path: '/', method: 'GET', signal: ee }, (err, response) => {
160-
t.error(err)
169+
p.ifError(err)
161170
response.body.on('data', () => {
162171
ee.emit('abort')
163172
})
164173
response.body.on('error', err => {
165-
t.type(err, errors.RequestAbortedError)
174+
p.ok(err instanceof errors.RequestAbortedError)
166175
})
167176
})
168177
})
178+
await p.completed
169179
})
170180

171181
function waitingWithBody (body, type) {
172-
test(`Abort while waiting response (with body ${type})`, (t) => {
173-
t.plan(1)
182+
test(`Abort while waiting response (with body ${type})`, async (t) => {
183+
const p = tspl(t, { plan: 1 })
174184

175185
const ee = new EventEmitter()
176186
const server = createServer((req, res) => {
177187
ee.emit('abort')
178188
res.setHeader('content-type', 'text/plain')
179189
res.end('hello world')
180190
})
181-
t.teardown(server.close.bind(server))
191+
t.after(server.close.bind(server))
182192

183193
server.listen(0, () => {
184194
const client = new Client(`http://localhost:${server.address().port}`)
185-
t.teardown(client.destroy.bind(client))
195+
t.after(client.destroy.bind(client))
186196

187197
client.request({ path: '/', method: 'POST', body, signal: ee }, (err, response) => {
188-
t.type(err, errors.RequestAbortedError)
198+
p.ok(err instanceof errors.RequestAbortedError)
189199
})
190200
})
201+
await p.completed
191202
})
192203
}
193204

@@ -197,8 +208,8 @@ waitingWithBody(new Uint8Array([42]), 'Uint8Array')
197208
waitingWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')
198209

199210
function writeHeadersStartedWithBody (body, type) {
200-
test(`Abort while waiting response (write headers started) (with body ${type})`, (t) => {
201-
t.plan(1)
211+
test(`Abort while waiting response (write headers started) (with body ${type})`, async (t) => {
212+
const p = tspl(t, { plan: 1 })
202213

203214
const ee = new EventEmitter()
204215
const server = createServer((req, res) => {
@@ -207,16 +218,17 @@ function writeHeadersStartedWithBody (body, type) {
207218
ee.emit('abort')
208219
res.end('hello world')
209220
})
210-
t.teardown(server.close.bind(server))
221+
t.after(server.close.bind(server))
211222

212223
server.listen(0, () => {
213224
const client = new Client(`http://localhost:${server.address().port}`)
214-
t.teardown(client.destroy.bind(client))
225+
t.after(client.destroy.bind(client))
215226

216227
client.request({ path: '/', method: 'POST', body, signal: ee }, (err, response) => {
217-
t.type(err, errors.RequestAbortedError)
228+
p.ok(err instanceof errors.RequestAbortedError)
218229
})
219230
})
231+
await p.completed
220232
})
221233
}
222234

@@ -226,30 +238,31 @@ writeHeadersStartedWithBody(new Uint8Array([42]), 'Uint8Array')
226238
writeHeadersStartedWithBody(wrapWithAsyncIterable(createReadStream(__filename)), 'async-iterator')
227239

228240
function writeBodyStartedWithBody (body, type) {
229-
test(`Abort while waiting response (write headers and write body started) (with body ${type})`, (t) => {
230-
t.plan(2)
241+
test(`Abort while waiting response (write headers and write body started) (with body ${type})`, async (t) => {
242+
const p = tspl(t, { plan: 2 })
231243

232244
const ee = new EventEmitter()
233245
const server = createServer((req, res) => {
234246
res.writeHead(200, { 'content-type': 'text/plain' })
235247
res.write('hello')
236248
})
237-
t.teardown(server.close.bind(server))
249+
t.after(server.close.bind(server))
238250

239251
server.listen(0, () => {
240252
const client = new Client(`http://localhost:${server.address().port}`)
241-
t.teardown(client.destroy.bind(client))
253+
t.after(client.destroy.bind(client))
242254

243255
client.request({ path: '/', method: 'POST', body, signal: ee }, (err, response) => {
244-
t.error(err)
256+
p.ifError(err)
245257
response.body.on('data', () => {
246258
ee.emit('abort')
247259
})
248260
response.body.on('error', err => {
249-
t.type(err, errors.RequestAbortedError)
261+
p.ok(err instanceof errors.RequestAbortedError)
250262
})
251263
})
252264
})
265+
await p.completed
253266
})
254267
}
255268

0 commit comments

Comments
 (0)