Skip to content

Instantly share code, notes, and snippets.

@craigbilner
Created May 5, 2021 08:07
Show Gist options
  • Save craigbilner/bfc372a52c25796626d88c4531d52741 to your computer and use it in GitHub Desktop.
Save craigbilner/bfc372a52c25796626d88c4531d52741 to your computer and use it in GitHub Desktop.
const fs = require('fs');
// const report = {
// 'foo.tsx': {
// 12: ['foo.spec.tsx', 'bar.spec.tsx'],
// 389: ['bat.spec.tsx']
// }
// };
class reporter {
constructor() {
this.coverage = {};
}
onTestResult(config, result) {
Object.entries(result.coverage).forEach(([fileName, stats]) => {
Object.values(stats.fnMap).forEach((v) => {
if (!this.coverage[fileName]) {
this.coverage[fileName] = {
[v.line]: [result.testFilePath]
};
return;
}
if (!this.coverage[fileName][v.line]) {
this.coverage[fileName][v.line] = [result.testFilePath];
return;
}
this.coverage[fileName][v.line].push(result.testFilePath);
});
});
}
onRunComplete() {
const lines = [];
Object.entries(this.coverage).forEach(([fileName, cov]) => {
Object.entries(cov).forEach(([ln, tests]) => {
lines.push({
fileName,
ln,
tests
});
});
});
lines.sort((a, b) => {
if (a.tests.length > b.tests.length) {
return -1;
}
if (a.tests.length < b.tests.length) {
return 1;
}
return 0;
});
const strs = lines.map(
({ fileName, ln, tests }) =>
`**${fileName} (ln${ln}) - ${tests.length}**\n\n\n${tests.join('\n')}`
);
fs.writeFile('report.txt', strs.join('\n\n'), 'utf8', () =>
console.log('done')
);
}
}
module.exports = reporter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment