Last active
November 7, 2024 13:42
-
-
Save wreulicke/5807466eafbab55d934a3487163c59bf to your computer and use it in GitHub Desktop.
node:testでJSON出力するreporterサンプル
This file contains hidden or 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
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