javascript / expert
Snippet
Custom Test Event Parsing with Node.js Reporter Streams
Node.js test runner exports executable streams via run() that can be piped into official reporters like tap and transformed using standard stream pipelines to build custom CI/CD output formats.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { run } from 'node:test';import { tap } from 'node:test/reporters';import { Transform } from 'node:stream';const customFilter = new Transform({writableObjectMode: true,transform(chunk, encoding, callback) {const line = chunk.toString();if (line.startsWith('not ok')) {this.push(`[FAILURE ENCOUNTERED]: ${line}`);}callback();}});run({ files: ['./test/suite.js'] }).compose(tap).pipe(customFilter).pipe(process.stdout);
nodejs
Breakdown
1
import { run } from 'node:test';
Imports the programmatical test runner execution mechanism.
2
run({ files: ['./test/suite.js'] })
Initiates asynchronous execution of test files as an object stream.
3
.compose(tap)
Pipes raw test runner event objects into the TAP reporter transform stream.
4
.pipe(customFilter)
Routes formatted TAP text lines through a custom Transform stream for selective filtering.