Skip to content

Commit 7308f53

Browse files
authored
fix: fix flaky debug test (#2714)
1 parent f430b24 commit 7308f53

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

test/node-test/debug.js

+29-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { tspl } = require('@matteo.collina/tspl')
99
const removeEscapeColorsRE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
1010

1111
test('debug#websocket', async t => {
12-
const assert = tspl(t, { plan: 6 })
12+
const assert = tspl(t, { plan: 8 })
1313
const child = spawn(
1414
process.execPath,
1515
[join(__dirname, '../fixtures/websocket.js')],
@@ -24,20 +24,23 @@ test('debug#websocket', async t => {
2424
/(WEBSOCKET [0-9]+:) (connecting to)/,
2525
// Skip the chunk that comes with the experimental warning
2626
/(\[UNDICI-WS\])/,
27+
/\(Use `node --trace-warnings \.\.\.` to show where the warning was created\)/,
2728
/(WEBSOCKET [0-9]+:) (connected to)/,
2829
/(WEBSOCKET [0-9]+:) (sending request)/,
2930
/(WEBSOCKET [0-9]+:) (connection opened)/,
30-
/(WEBSOCKET [0-9]+:) (closed connection to)/
31+
/(WEBSOCKET [0-9]+:) (closed connection to)/,
32+
/^$/
3133
]
3234

3335
child.stderr.setEncoding('utf8')
3436
child.stderr.on('data', chunk => {
3537
chunks.push(chunk)
3638
})
3739
child.stderr.on('end', () => {
38-
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
39-
for (let i = 1; i < chunks.length; i++) {
40-
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
40+
const lines = extractLines(chunks)
41+
assert.strictEqual(lines.length, assertions.length)
42+
for (let i = 1; i < lines.length; i++) {
43+
assert.match(lines[i], assertions[i])
4144
}
4245
})
4346

@@ -46,7 +49,7 @@ test('debug#websocket', async t => {
4649
})
4750

4851
test('debug#fetch', async t => {
49-
const assert = tspl(t, { plan: 6 })
52+
const assert = tspl(t, { plan: 7 })
5053
const child = spawn(
5154
process.execPath,
5255
[join(__dirname, '../fixtures/fetch.js')],
@@ -60,17 +63,19 @@ test('debug#fetch', async t => {
6063
/(FETCH [0-9]+:) (connected to)/,
6164
/(FETCH [0-9]+:) (sending request)/,
6265
/(FETCH [0-9]+:) (received response)/,
63-
/(FETCH [0-9]+:) (trailers received)/
66+
/(FETCH [0-9]+:) (trailers received)/,
67+
/^$/
6468
]
6569

6670
child.stderr.setEncoding('utf8')
6771
child.stderr.on('data', chunk => {
6872
chunks.push(chunk)
6973
})
7074
child.stderr.on('end', () => {
71-
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
72-
for (let i = 0; i < chunks.length; i++) {
73-
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
75+
const lines = extractLines(chunks)
76+
assert.strictEqual(lines.length, assertions.length)
77+
for (let i = 0; i < lines.length; i++) {
78+
assert.match(lines[i], assertions[i])
7479
}
7580
})
7681

@@ -80,7 +85,7 @@ test('debug#fetch', async t => {
8085

8186
test('debug#undici', async t => {
8287
// Due to Node.js webpage redirect
83-
const assert = tspl(t, { plan: 6 })
88+
const assert = tspl(t, { plan: 7 })
8489
const child = spawn(
8590
process.execPath,
8691
[join(__dirname, '../fixtures/undici.js')],
@@ -96,20 +101,29 @@ test('debug#undici', async t => {
96101
/(UNDICI [0-9]+:) (connected to)/,
97102
/(UNDICI [0-9]+:) (sending request)/,
98103
/(UNDICI [0-9]+:) (received response)/,
99-
/(UNDICI [0-9]+:) (trailers received)/
104+
/(UNDICI [0-9]+:) (trailers received)/,
105+
/^$/
100106
]
101107

102108
child.stderr.setEncoding('utf8')
103109
child.stderr.on('data', chunk => {
104110
chunks.push(chunk)
105111
})
106112
child.stderr.on('end', () => {
107-
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
108-
for (let i = 0; i < chunks.length; i++) {
109-
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
113+
const lines = extractLines(chunks)
114+
assert.strictEqual(lines.length, assertions.length)
115+
for (let i = 0; i < lines.length; i++) {
116+
assert.match(lines[i], assertions[i])
110117
}
111118
})
112119

113120
await assert.completed
114121
child.kill()
115122
})
123+
124+
function extractLines (chunks) {
125+
return chunks
126+
.join('')
127+
.split('\n')
128+
.map(v => v.replace(removeEscapeColorsRE, ''))
129+
}

0 commit comments

Comments
 (0)