UNPKG

@reporters/github

Version:

A github actions reporter for `node:test`

68 lines (60 loc) 1.94 kB
// `test:log` is execution-ordered: Node forwards it the moment `t.log()` runs, // bypassing the per-file declaration-order buffer that `test:diagnostic` waits // in. Reporters that group their output by test must therefore hold each log // until its owner reports, or it lands under whichever test happens to be // printing at the time. // `entryFile` (nodejs/node#64309) disambiguates the isolated processes that // share a declaration file; until it lands, `(file, testId)` can collide across // concurrent processes running tests declared in a shared helper. const keyOf = (data) => `${data.entryFile ?? data.file}${data.testId}`; export function formatLogMessage(data) { if (data.data === undefined) { return `${data.message}`; } let payload; try { payload = JSON.stringify(data.data) ?? ''; } catch { payload = String(data.data); } return `${data.message} ${payload}`; } export default class LogBuffer { #pending = new Map(); #orphans = 0; push(data) { // A log with no `testId` can never be claimed by a reported test, so it gets // a key nothing matches and surfaces in `drain()` instead of being lost. this.#orphans += data.testId === undefined ? 1 : 0; const key = data.testId === undefined ? `orphan${this.#orphans}` : keyOf(data); const logs = this.#pending.get(key); if (logs === undefined) { this.#pending.set(key, [data]); } else { logs.push(data); } } take(data) { if (data.testId === undefined) { return []; } const key = keyOf(data); const logs = this.#pending.get(key); if (logs === undefined) { return []; } this.#pending.delete(key); return logs; } drain() { if (this.#pending.size === 0) { return []; } const logs = []; for (const pending of this.#pending.values()) { logs.push(...pending); } this.#pending.clear(); return logs; } }