Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Last active November 7, 2024 13:42
Show Gist options
  • Save wreulicke/5807466eafbab55d934a3487163c59bf to your computer and use it in GitHub Desktop.
Save wreulicke/5807466eafbab55d934a3487163c59bf to your computer and use it in GitHub Desktop.
node:testでJSON出力するreporterサンプル
module.exports = async function * customReporter(source) {
let roots = []
let currentSuite = null;
function startTest(event) {
const parent = currentSuite;
currentSuite = {
name: event.data.name,
nesting: event.data.nesting,
parent,
children: [],
}
if (parent) {
parent.children.push(currentSuite);
} else {
roots.push(currentSuite);
}
}
for await (const event of source) {
switch (event.type) {
case 'test:start':
startTest(event);
break;
case 'test:pass':
case 'test:fail':
if(!currentSuite) {
startTest(event);
}
if (currentSuite?.name !== event.data.name ||
currentSuite?.nesting !== event.data.nesting) {
startTest(event);
}
currentSuite.status = event.type
if (currentSuite?.nesting === event.data.nesting) {
currentSuite = currentSuite.parent;
}
break;
}
}
// console.dir(roots, { depth: null })
const json = JSON.stringify(roots, (key, value) => {
return key === "parent" ? value?.name: value
}, 2)
yield json
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment