Skip to content

Commit 43286a9

Browse files
authored
chore: migrate a batch of tests to node test runner (#2738)
1 parent e49471f commit 43286a9

10 files changed

+299
-252
lines changed

test/invalid-headers.js

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

3-
const { test } = require('tap')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
45
const { Client, errors } = require('..')
56

67
test('invalid headers', (t) => {
7-
t.plan(10)
8+
t = tspl(t, { plan: 10 })
89

910
const client = new Client('http://localhost:3000')
10-
t.teardown(client.destroy.bind(client))
11+
after(() => client.close())
1112
client.request({
1213
path: '/',
1314
method: 'GET',

test/issue-2078.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
'use strict'
22

3-
const { test } = require('tap')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
45
const { MockAgent, getGlobalDispatcher, setGlobalDispatcher, fetch } = require('..')
56

67
test('MockPool.reply headers are an object, not an array - issue #2078', async (t) => {
8+
t = tspl(t, { plan: 1 })
9+
710
const global = getGlobalDispatcher()
811
const mockAgent = new MockAgent()
912
const mockPool = mockAgent.get('http://localhost')
1013

11-
t.teardown(() => setGlobalDispatcher(global))
14+
after(() => setGlobalDispatcher(global))
1215
setGlobalDispatcher(mockAgent)
1316

1417
mockPool.intercept({
1518
path: '/foo',
1619
method: 'GET'
1720
}).reply((options) => {
18-
t.ok(!Array.isArray(options.headers))
21+
t.strictEqual(Array.isArray(options.headers), false)
1922

2023
return { statusCode: 200 }
2124
})
2225

23-
await t.resolves(fetch('http://localhost/foo'))
26+
await fetch('http://localhost/foo')
27+
28+
await t.completed
2429
})

test/issue-803.js

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

3-
const { test } = require('tap')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
5+
const { once } = require('node:events')
46
const { Client } = require('..')
57
const { createServer } = require('node:http')
68
const EE = require('node:events')
79

8-
test('https://github.com/nodejs/undici/issues/803', (t) => {
9-
t.plan(2)
10+
test('https://github.com/nodejs/undici/issues/803', async (t) => {
11+
t = tspl(t, { plan: 2 })
1012

1113
const SIZE = 5900373096
1214

@@ -23,25 +25,27 @@ test('https://github.com/nodejs/undici/issues/803', (t) => {
2325

2426
res.end()
2527
})
26-
t.teardown(server.close.bind(server))
27-
28-
server.listen(0, () => {
29-
const client = new Client(`http://localhost:${server.address().port}`)
30-
t.teardown(client.close.bind(client))
31-
32-
client.request({
33-
path: '/',
34-
method: 'GET'
35-
}, (err, data) => {
36-
t.error(err)
37-
38-
let pos = 0
39-
data.body.on('data', (buf) => {
40-
pos += buf.length
41-
})
42-
data.body.on('end', () => {
43-
t.equal(pos, SIZE)
44-
})
28+
after(() => server.close())
29+
30+
server.listen(0)
31+
32+
await once(server, 'listening')
33+
const client = new Client(`http://localhost:${server.address().port}`)
34+
after(() => client.close())
35+
36+
client.request({
37+
path: '/',
38+
method: 'GET'
39+
}, (err, data) => {
40+
t.ifError(err)
41+
42+
let pos = 0
43+
data.body.on('data', (buf) => {
44+
pos += buf.length
45+
})
46+
data.body.on('end', () => {
47+
t.strictEqual(pos, SIZE)
4548
})
4649
})
50+
await t.completed
4751
})

test/issue-810.js

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

3-
const { test } = require('tap')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
5+
const { once } = require('node:events')
46
const { Client, errors } = require('..')
57
const net = require('node:net')
68

7-
test('https://github.com/mcollina/undici/issues/810', (t) => {
8-
t.plan(3)
9+
test('https://github.com/mcollina/undici/issues/810', async (t) => {
10+
t = tspl(t, { plan: 3 })
911

1012
let x = 0
1113
const server = net.createServer(socket => {
@@ -19,117 +21,127 @@ test('https://github.com/mcollina/undici/issues/810', (t) => {
1921
socket.write('\r\n')
2022
}
2123
})
22-
t.teardown(server.close.bind(server))
23-
24-
server.listen(0, () => {
25-
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 })
26-
t.teardown(client.destroy.bind(client))
27-
28-
client.request({
29-
path: '/',
30-
method: 'GET'
31-
}, (err, data) => {
32-
t.error(err)
33-
data.body.resume().on('end', () => {
34-
// t.fail() FIX: Should fail.
35-
t.ok(true, 'pass')
36-
}).on('error', err => (
37-
t.ok(err instanceof errors.HTTPParserError)
38-
))
39-
})
40-
client.request({
41-
path: '/',
42-
method: 'GET'
43-
}, (err, data) => {
24+
after(() => server.close())
25+
26+
server.listen(0)
27+
28+
await once(server, 'listening')
29+
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 })
30+
after(() => client.close())
31+
32+
client.request({
33+
path: '/',
34+
method: 'GET'
35+
}, (err, data) => {
36+
t.ifError(err)
37+
data.body.resume().on('end', () => {
38+
// t.fail() FIX: Should fail.
39+
t.ok(true, 'pass')
40+
}).on('error', err => (
4441
t.ok(err instanceof errors.HTTPParserError)
45-
})
42+
))
43+
})
44+
client.request({
45+
path: '/',
46+
method: 'GET'
47+
}, (err, data) => {
48+
t.ok(err instanceof errors.HTTPParserError)
4649
})
50+
await t.completed
4751
})
4852

49-
test('https://github.com/mcollina/undici/issues/810 no pipelining', (t) => {
50-
t.plan(2)
53+
test('https://github.com/mcollina/undici/issues/810 no pipelining', async (t) => {
54+
t = tspl(t, { plan: 2 })
5155

5256
const server = net.createServer(socket => {
5357
socket.write('HTTP/1.1 200 OK\r\n')
5458
socket.write('Content-Length: 1\r\n\r\n')
5559
socket.write('11111\r\n')
5660
})
57-
t.teardown(server.close.bind(server))
58-
59-
server.listen(0, () => {
60-
const client = new Client(`http://localhost:${server.address().port}`)
61-
62-
client.request({
63-
path: '/',
64-
method: 'GET'
65-
}, (err, data) => {
66-
t.error(err)
67-
data.body.resume().on('end', () => {
68-
// t.fail() FIX: Should fail.
69-
t.ok(true, 'pass')
70-
})
61+
after(() => server.close())
62+
63+
server.listen(0)
64+
65+
await once(server, 'listening')
66+
const client = new Client(`http://localhost:${server.address().port}`)
67+
68+
client.request({
69+
path: '/',
70+
method: 'GET'
71+
}, (err, data) => {
72+
t.ifError(err)
73+
data.body.resume().on('end', () => {
74+
// t.fail() FIX: Should fail.
75+
t.ok(true, 'pass')
7176
})
7277
})
78+
await t.completed
7379
})
7480

75-
test('https://github.com/mcollina/undici/issues/810 pipelining', (t) => {
76-
t.plan(2)
81+
test('https://github.com/mcollina/undici/issues/810 pipelining', async (t) => {
82+
t = tspl(t, { plan: 2 })
7783

7884
const server = net.createServer(socket => {
7985
socket.write('HTTP/1.1 200 OK\r\n')
8086
socket.write('Content-Length: 1\r\n\r\n')
8187
socket.write('11111\r\n')
8288
})
83-
t.teardown(server.close.bind(server))
84-
85-
server.listen(0, () => {
86-
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
87-
t.teardown(client.destroy.bind(client))
88-
89-
client.request({
90-
path: '/',
91-
method: 'GET'
92-
}, (err, data) => {
93-
t.error(err)
94-
data.body.resume().on('end', () => {
95-
// t.fail() FIX: Should fail.
96-
t.ok(true, 'pass')
97-
})
89+
after(() => server.close())
90+
91+
server.listen(0)
92+
93+
await once(server, 'listening')
94+
95+
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
96+
after(() => client.close())
97+
98+
client.request({
99+
path: '/',
100+
method: 'GET'
101+
}, (err, data) => {
102+
t.ifError(err)
103+
data.body.resume().on('end', () => {
104+
// t.fail() FIX: Should fail.
105+
t.ok(true, 'pass')
98106
})
99107
})
108+
await t.completed
100109
})
101110

102-
test('https://github.com/mcollina/undici/issues/810 pipelining 2', (t) => {
103-
t.plan(4)
111+
test('https://github.com/mcollina/undici/issues/810 pipelining 2', async (t) => {
112+
t = tspl(t, { plan: 4 })
104113

105114
const server = net.createServer(socket => {
106115
socket.write('HTTP/1.1 200 OK\r\n')
107116
socket.write('Content-Length: 1\r\n\r\n')
108117
socket.write('11111\r\n')
109118
})
110-
t.teardown(server.close.bind(server))
111-
112-
server.listen(0, () => {
113-
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
114-
t.teardown(client.destroy.bind(client))
115-
116-
client.request({
117-
path: '/',
118-
method: 'GET'
119-
}, (err, data) => {
120-
t.error(err)
121-
data.body.resume().on('end', () => {
122-
// t.fail() FIX: Should fail.
123-
t.ok(true, 'pass')
124-
})
125-
})
119+
after(() => server.close())
126120

127-
client.request({
128-
path: '/',
129-
method: 'GET'
130-
}, (err, data) => {
131-
t.equal(err.code, 'HPE_INVALID_CONSTANT')
132-
t.ok(err instanceof errors.HTTPParserError)
121+
server.listen(0)
122+
123+
await once(server, 'listening')
124+
125+
const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true })
126+
after(() => client.close())
127+
128+
client.request({
129+
path: '/',
130+
method: 'GET'
131+
}, (err, data) => {
132+
t.ifError(err)
133+
data.body.resume().on('end', () => {
134+
// t.fail() FIX: Should fail.
135+
t.ok(true, 'pass')
133136
})
134137
})
138+
139+
client.request({
140+
path: '/',
141+
method: 'GET'
142+
}, (err, data) => {
143+
t.strictEqual(err.code, 'HPE_INVALID_CONSTANT')
144+
t.ok(err instanceof errors.HTTPParserError)
145+
})
146+
await t.completed
135147
})

0 commit comments

Comments
 (0)