Last active
November 24, 2023 22:49
-
-
Save craigmulligan/00aec33f5ca427f236763b12245949f7 to your computer and use it in GitHub Desktop.
Node:test Custom reporter to only print failed test console.logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { spec } from "node:test/reporters"; | |
import { Transform } from 'node:stream'; | |
class Reporter extends Transform { | |
// This is a custom test reporter for node:test | |
// it'll only log console.logs that occur during | |
// failed tests. For everything else it delegates | |
// to the default "spec" reporter. | |
constructor() { | |
super({ writableObjectMode: true }) | |
this.specReporter = new spec() | |
this.logs = [] | |
} | |
addToFailedLogs = (_, data) => { | |
this.logs.push(data) | |
} | |
_transform(event, encoding, callback) { | |
switch (event.type) { | |
case 'test:stderr': | |
case 'test:stdout': | |
this.logs.push(this.specReporter._transform(event, encoding, this.addToFailedLogs)); | |
callback(null) | |
break; | |
case 'test:pass': | |
this.specReporter._transform(event, encoding, callback); | |
this.logs = []; | |
break; | |
case 'test:fail': | |
this.specReporter._transform(event, encoding, this.addToFailedLogs); | |
callback(null, this.logs.join('')) | |
this.logs = [] | |
break; | |
default: | |
this.specReporter._transform(event, encoding, callback); | |
break; | |
} | |
} | |
_flush(callback) { | |
this.specReporter._flush(callback) | |
} | |
} | |
export default new Reporter() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment