Skip to content

Commit c95b9bc

Browse files
Fix resend mock output capture
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 3ec3a13 commit c95b9bc

1 file changed

Lines changed: 20 additions & 26 deletions

File tree

mock-servers/resend/resend-mock.test.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,27 @@ import { createTemporaryDirectory } from '#tools/temp-directory.ts'
99
const workerConfig = 'mock-servers/resend/wrangler.jsonc'
1010
const projectRoot = process.cwd()
1111
const nodeBin = process.execPath
12-
const wranglerCli = join(projectRoot, 'node_modules', 'wrangler', 'wrangler-dist', 'cli.js')
12+
const wranglerCli = join(
13+
projectRoot,
14+
'node_modules',
15+
'wrangler',
16+
'wrangler-dist',
17+
'cli.js',
18+
)
1319
const defaultTimeoutMs = 60_000
1420

15-
function captureOutput(stream: ReadableStream<Uint8Array> | null) {
21+
function captureOutput(stream: NodeJS.ReadableStream | null | undefined) {
1622
let output = ''
1723
if (!stream) {
1824
return () => output
1925
}
20-
21-
const reader = stream.getReader()
22-
const decoder = new TextDecoder()
23-
24-
const read = async () => {
25-
try {
26-
while (true) {
27-
const { value, done } = await reader.read()
28-
if (done) break
29-
if (value) {
30-
output += decoder.decode(value)
31-
}
32-
}
33-
} catch {
34-
// Ignore stream errors while capturing output.
35-
}
36-
}
37-
38-
void read()
26+
stream.setEncoding('utf8')
27+
stream.on('data', (chunk) => {
28+
output += chunk
29+
})
30+
stream.on('error', () => {
31+
// Ignore stream errors while capturing output.
32+
})
3933
return () => output
4034
}
4135

@@ -206,8 +200,8 @@ async function startMockResendWorker(persistDir: string, token: string) {
206200
},
207201
)
208202

209-
const getStdout = captureOutput(proc.stdout ? ReadableStream.from(proc.stdout) : null)
210-
const getStderr = captureOutput(proc.stderr ? ReadableStream.from(proc.stderr) : null)
203+
const getStdout = captureOutput(proc.stdout)
204+
const getStderr = captureOutput(proc.stderr)
211205

212206
await waitForMockServer(origin, proc, getStdout, getStderr)
213207

@@ -285,7 +279,7 @@ test(
285279
expect(JSON.parse(listJson.messages[0]?.payload_json ?? 'null')).toEqual(
286280
email,
287281
)
288-
}
282+
},
289283
)
290284

291285
test(
@@ -307,7 +301,7 @@ test(
307301
}),
308302
})
309303
expect(createResp.status).toBe(401)
310-
}
304+
},
311305
)
312306

313307
test(
@@ -337,5 +331,5 @@ test(
337331
const dashboardHtml = await dashboardResp.text()
338332
expect(dashboardHtml).toContain(`href="/__mocks/meta?token=${token}"`)
339333
expect(dashboardHtml).toContain(`href="/__mocks/messages?token=${token}"`)
340-
}
334+
},
341335
)

0 commit comments

Comments
 (0)