Skip to content

Commit

Permalink
fix: fix flaky debug test (#2714)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Feb 8, 2024
1 parent f430b24 commit 7308f53
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions test/node-test/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { tspl } = require('@matteo.collina/tspl')
const removeEscapeColorsRE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g

test('debug#websocket', async t => {
const assert = tspl(t, { plan: 6 })
const assert = tspl(t, { plan: 8 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/websocket.js')],
Expand All @@ -24,20 +24,23 @@ test('debug#websocket', async t => {
/(WEBSOCKET [0-9]+:) (connecting to)/,
// Skip the chunk that comes with the experimental warning
/(\[UNDICI-WS\])/,
/\(Use `node --trace-warnings \.\.\.` to show where the warning was created\)/,
/(WEBSOCKET [0-9]+:) (connected to)/,
/(WEBSOCKET [0-9]+:) (sending request)/,
/(WEBSOCKET [0-9]+:) (connection opened)/,
/(WEBSOCKET [0-9]+:) (closed connection to)/
/(WEBSOCKET [0-9]+:) (closed connection to)/,
/^$/
]

child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
for (let i = 1; i < chunks.length; i++) {
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 1; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})

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

test('debug#fetch', async t => {
const assert = tspl(t, { plan: 6 })
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/fetch.js')],
Expand All @@ -60,17 +63,19 @@ test('debug#fetch', async t => {
/(FETCH [0-9]+:) (connected to)/,
/(FETCH [0-9]+:) (sending request)/,
/(FETCH [0-9]+:) (received response)/,
/(FETCH [0-9]+:) (trailers received)/
/(FETCH [0-9]+:) (trailers received)/,
/^$/
]

child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
for (let i = 0; i < chunks.length; i++) {
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})

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

test('debug#undici', async t => {
// Due to Node.js webpage redirect
const assert = tspl(t, { plan: 6 })
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/undici.js')],
Expand All @@ -96,20 +101,29 @@ test('debug#undici', async t => {
/(UNDICI [0-9]+:) (connected to)/,
/(UNDICI [0-9]+:) (sending request)/,
/(UNDICI [0-9]+:) (received response)/,
/(UNDICI [0-9]+:) (trailers received)/
/(UNDICI [0-9]+:) (trailers received)/,
/^$/
]

child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
assert.strictEqual(chunks.length, assertions.length, JSON.stringify(chunks))
for (let i = 0; i < chunks.length; i++) {
assert.match(chunks[i].replace(removeEscapeColorsRE, ''), assertions[i])
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})

await assert.completed
child.kill()
})

function extractLines (chunks) {
return chunks
.join('')
.split('\n')
.map(v => v.replace(removeEscapeColorsRE, ''))
}

0 comments on commit 7308f53

Please sign in to comment.