Skip to content

Commit 4c9ec07

Browse files
committed
chore: use testcontext for test:node-test, batch 1
1 parent 26e85d1 commit 4c9ec07

File tree

6 files changed

+144
-141
lines changed

6 files changed

+144
-141
lines changed

test/node-test/debug.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
const { test } = require('node:test')
44
const { spawn } = require('node:child_process')
55
const { join } = require('node:path')
6-
const { tspl } = require('@matteo.collina/tspl')
76

87
// eslint-disable-next-line no-control-regex
98
const removeEscapeColorsRE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
109

1110
const isNode23Plus = process.versions.node.split('.')[0] >= 23
1211
const isCITGM = !!process.env.CITGM
1312

14-
test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus }, async t => {
15-
const assert = tspl(t, { plan: 6 })
13+
test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus }, (t, done) => {
14+
t.plan(6)
1615
const child = spawn(
1716
process.execPath,
1817
[
@@ -40,17 +39,16 @@ test('debug#websocket', { skip: !process.versions.icu || isCITGM || isNode23Plus
4039
})
4140
child.stderr.on('end', () => {
4241
const lines = extractLines(chunks)
43-
assert.strictEqual(lines.length, assertions.length)
42+
t.assert.strictEqual(lines.length, assertions.length)
4443
for (let i = 1; i < lines.length; i++) {
45-
assert.match(lines[i], assertions[i])
44+
t.assert.match(lines[i], assertions[i])
4645
}
46+
done()
4747
})
48-
49-
await assert.completed
5048
})
5149

52-
test('debug#fetch', { skip: isCITGM || isNode23Plus }, async t => {
53-
const assert = tspl(t, { plan: 7 })
50+
test('debug#fetch', { skip: isCITGM || isNode23Plus }, (t, done) => {
51+
t.plan(7)
5452
const child = spawn(
5553
process.execPath,
5654
[
@@ -77,18 +75,17 @@ test('debug#fetch', { skip: isCITGM || isNode23Plus }, async t => {
7775
})
7876
child.stderr.on('end', () => {
7977
const lines = extractLines(chunks)
80-
assert.strictEqual(lines.length, assertions.length)
78+
t.assert.strictEqual(lines.length, assertions.length)
8179
for (let i = 0; i < lines.length; i++) {
82-
assert.match(lines[i], assertions[i])
80+
t.assert.match(lines[i], assertions[i])
8381
}
82+
done()
8483
})
85-
86-
await assert.completed
8784
})
8885

89-
test('debug#undici', { skip: isCITGM || isNode23Plus }, async t => {
86+
test('debug#undici', { skip: isCITGM || isNode23Plus }, (t, done) => {
9087
// Due to Node.js webpage redirect
91-
const assert = tspl(t, { plan: 7 })
88+
t.plan(7)
9289
const child = spawn(
9390
process.execPath,
9491
[
@@ -117,13 +114,12 @@ test('debug#undici', { skip: isCITGM || isNode23Plus }, async t => {
117114
})
118115
child.stderr.on('end', () => {
119116
const lines = extractLines(chunks)
120-
assert.strictEqual(lines.length, assertions.length)
117+
t.assert.strictEqual(lines.length, assertions.length)
121118
for (let i = 0; i < lines.length; i++) {
122-
assert.match(lines[i], assertions[i])
119+
t.assert.match(lines[i], assertions[i])
123120
}
121+
done()
124122
})
125-
126-
await assert.completed
127123
})
128124

129125
function extractLines (chunks) {

test/node-test/large-body.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const { test } = require('node:test')
44
const { createServer } = require('node:http')
55
const { request } = require('../../')
6-
const { strictEqual } = require('node:assert')
76

87
test('socket should not be reused unless body is consumed', async (t) => {
98
const LARGE_BODY = 'x'.repeat(10000000)
@@ -39,7 +38,7 @@ test('socket should not be reused unless body is consumed', async (t) => {
3938
const fooBody = await fooRes.body.text()
4039
await barRes.body.text()
4140

42-
strictEqual(fooRes.headers['content-length'], String(LARGE_BODY.length))
43-
strictEqual(fooBody.length, LARGE_BODY.length)
44-
strictEqual(fooBody, LARGE_BODY)
41+
t.assert.strictEqual(fooRes.headers['content-length'], String(LARGE_BODY.length))
42+
t.assert.strictEqual(fooBody.length, LARGE_BODY.length)
43+
t.assert.strictEqual(fooBody, LARGE_BODY)
4544
})

test/node-test/tree.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,47 @@
33
const { TernarySearchTree, tree } = require('../../lib/core/tree')
44
const { wellknownHeaderNames, headerNameLowerCasedRecord } = require('../../lib/core/constants')
55
const { describe, test } = require('node:test')
6-
const assert = require('node:assert')
76

87
describe('Ternary Search Tree', () => {
9-
test('The empty key cannot be added.', () => {
10-
assert.throws(() => new TernarySearchTree().insert('', ''))
8+
test('The empty key cannot be added.', (t) => {
9+
t.assert.throws(() => new TernarySearchTree().insert('', ''))
1110
const tst = new TernarySearchTree()
1211
tst.insert('a', 'a')
13-
assert.throws(() => tst.insert('', ''))
12+
t.assert.throws(() => tst.insert('', ''))
1413
})
1514

16-
test('looking up not inserted key returns null', () => {
15+
test('looking up not inserted key returns null', (t) => {
1716
const tst = new TernarySearchTree()
1817
tst.insert('a', 'a')
19-
assert.strictEqual(tst.lookup(Buffer.from('non-existent')), null)
18+
t.assert.strictEqual(tst.lookup(Buffer.from('non-existent')), null)
2019
})
2120

22-
test('not ascii string', () => {
23-
assert.throws(() => new TernarySearchTree().insert('\x80', 'a'))
21+
test('not ascii string', (t) => {
22+
t.assert.throws(() => new TernarySearchTree().insert('\x80', 'a'))
2423
const tst = new TernarySearchTree()
2524
tst.insert('a', 'a')
2625
// throw on TstNode
27-
assert.throws(() => tst.insert('\x80', 'a'))
26+
t.assert.throws(() => tst.insert('\x80', 'a'))
2827
})
2928

30-
test('duplicate key', () => {
29+
test('duplicate key', (t) => {
3130
const tst = new TernarySearchTree()
3231
const key = 'a'
3332
const lookupKey = Buffer.from(key)
3433
tst.insert(key, 'a')
35-
assert.strictEqual(tst.lookup(lookupKey), 'a')
34+
t.assert.strictEqual(tst.lookup(lookupKey), 'a')
3635
tst.insert(key, 'b')
37-
assert.strictEqual(tst.lookup(lookupKey), 'b')
36+
t.assert.strictEqual(tst.lookup(lookupKey), 'b')
3837
})
3938

40-
test('tree', () => {
39+
test('tree', (t) => {
4140
for (let i = 0; i < wellknownHeaderNames.length; ++i) {
4241
const key = wellknownHeaderNames[i]
43-
assert.strictEqual(tree.lookup(Buffer.from(key)), headerNameLowerCasedRecord[key])
42+
t.assert.strictEqual(tree.lookup(Buffer.from(key)), headerNameLowerCasedRecord[key])
4443
}
4544
})
4645

47-
test('fuzz', () => {
46+
test('fuzz', (t) => {
4847
const LENGTH = 2000
4948
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
5049
const charactersLength = characters.length
@@ -71,7 +70,7 @@ describe('Ternary Search Tree', () => {
7170
}
7271

7372
for (let i = 0; i < LENGTH; ++i) {
74-
assert.strictEqual(tst.lookup(randomBuffer[i]), random[i])
73+
t.assert.strictEqual(tst.lookup(randomBuffer[i]), random[i])
7574
}
7675
})
7776
})

test/node-test/unix.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ const http = require('node:http')
66
const https = require('node:https')
77
const pem = require('@metcoder95/https-pem')
88
const fs = require('node:fs')
9-
const { tspl } = require('@matteo.collina/tspl')
9+
const { once } = require('node:events')
1010

1111
const skip = process.platform === 'win32'
1212

1313
test('http unix get', { skip }, async (t) => {
1414
let client
15-
const p = tspl(t, { plan: 7 })
15+
t.plan(7)
1616

1717
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
18-
p.equal('/', req.url)
19-
p.equal('GET', req.method)
20-
p.equal('localhost', req.headers.host)
18+
t.assert.strictEqual('/', req.url)
19+
t.assert.strictEqual('GET', req.method)
20+
t.assert.strictEqual('localhost', req.headers.host)
2121
res.setHeader('Content-Type', 'text/plain')
2222
res.end('hello')
2323
})
@@ -42,31 +42,32 @@ test('http unix get', { skip }, async (t) => {
4242
})
4343

4444
client.request({ path: '/', method: 'GET' }, (err, data) => {
45-
p.ifError(err)
45+
t.assert.ifError(err)
4646
const { statusCode, headers, body } = data
47-
p.equal(statusCode, 200)
48-
p.equal(headers['content-type'], 'text/plain')
47+
t.assert.strictEqual(statusCode, 200)
48+
t.assert.strictEqual(headers['content-type'], 'text/plain')
4949
const bufs = []
5050
body.on('data', (buf) => {
5151
bufs.push(buf)
5252
})
5353
body.on('end', () => {
54-
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
54+
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
55+
server.close()
5556
})
5657
})
5758
})
5859

59-
await p.completed
60+
await once(server, 'close')
6061
})
6162

6263
test('http unix get pool', { skip }, async (t) => {
6364
let client
64-
const p = tspl(t, { plan: 7 })
65+
t.plan(7)
6566

6667
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
67-
p.equal('/', req.url)
68-
p.equal('GET', req.method)
69-
p.equal('localhost', req.headers.host)
68+
t.assert.strictEqual('/', req.url)
69+
t.assert.strictEqual('GET', req.method)
70+
t.assert.strictEqual('localhost', req.headers.host)
7071
res.setHeader('Content-Type', 'text/plain')
7172
res.end('hello')
7273
})
@@ -91,30 +92,32 @@ test('http unix get pool', { skip }, async (t) => {
9192
})
9293

9394
client.request({ path: '/', method: 'GET' }, (err, data) => {
94-
p.ifError(err)
95+
t.assert.ifError(err)
9596
const { statusCode, headers, body } = data
96-
p.equal(statusCode, 200)
97-
p.equal(headers['content-type'], 'text/plain')
97+
t.assert.strictEqual(statusCode, 200)
98+
t.assert.strictEqual(headers['content-type'], 'text/plain')
9899
const bufs = []
99100
body.on('data', (buf) => {
100101
bufs.push(buf)
101102
})
102103
body.on('end', () => {
103-
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
104+
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
105+
server.close()
104106
})
105107
})
106108
})
107109

108-
await p.completed
110+
await once(server, 'close')
109111
})
110112

111113
test('https get with tls opts', { skip }, async (t) => {
114+
t.plan(6)
115+
112116
let client
113-
const p = tspl(t, { plan: 6 })
114117

115118
const server = https.createServer({ ...pem, joinDuplicateHeaders: true }, (req, res) => {
116-
p.equal('/', req.url)
117-
p.equal('GET', req.method)
119+
t.assert.strictEqual('/', req.url)
120+
t.assert.strictEqual('GET', req.method)
118121
res.setHeader('content-type', 'text/plain')
119122
res.end('hello')
120123
})
@@ -142,18 +145,20 @@ test('https get with tls opts', { skip }, async (t) => {
142145
})
143146

144147
client.request({ path: '/', method: 'GET' }, (err, data) => {
145-
p.ifError(err)
148+
t.assert.ifError(err)
146149
const { statusCode, headers, body } = data
147-
p.equal(statusCode, 200)
148-
p.equal(headers['content-type'], 'text/plain')
150+
t.assert.strictEqual(statusCode, 200)
151+
t.assert.strictEqual(headers['content-type'], 'text/plain')
149152
const bufs = []
150153
body.on('data', (buf) => {
151154
bufs.push(buf)
152155
})
153156
body.on('end', () => {
154-
p.equal('hello', Buffer.concat(bufs).toString('utf8'))
157+
t.assert.strictEqual('hello', Buffer.concat(bufs).toString('utf8'))
158+
server.close()
155159
})
156160
})
157161
})
158-
await p.completed
162+
163+
await once(server, 'close')
159164
})

0 commit comments

Comments
 (0)