diff --git a/parse-report.js b/parse-report.js index ed4a51a..8067b72 100644 --- a/parse-report.js +++ b/parse-report.js @@ -12,20 +12,26 @@ const durationRegex = /duration_ms\s([\d.]+)/ export default async function parseReport(source) { const tests = [] const testStack = [] - - let diagnosticMessage = '' + const allTests = Object.create(null) + let currentTest let totalDuration = 0 function lastTestInStack() { return testStack.length ? testStack[testStack.length - 1] : null } - function appendDiagnosticMessage(message) { - diagnosticMessage += `${message}\n` + function appendDiagnosticMessage(data) { + // currentTest is for diagnostics after all tests are complete + // which do not have a file, line, or column. + const t = allTests[testId(data)] || currentTest + if (!t.diagnostic) { + t.diagnostic = '' + } + t.diagnostic += `${data.message}\n` } - function resetDiagnosticMessage() { - diagnosticMessage = '' + function testId(data) { + return [data.file, data.line, data.column].join('\0') } function isFileUrl(urlString) { @@ -52,13 +58,13 @@ export default async function parseReport(source) { data: { name, file } } = event - resetDiagnosticMessage() - - testStack.push({ + const t = { name, file: parseFilePath(file), tests: [] - }) + } + allTests[testId(event.data)] = t + testStack.push(t) break @@ -72,7 +78,7 @@ export default async function parseReport(source) { } } = event - const currentTest = testStack.pop() + currentTest = testStack.pop() currentTest.duration = duration currentTest.skip = skip !== undefined currentTest.todo = todo !== undefined @@ -89,10 +95,6 @@ export default async function parseReport(source) { } } - if (diagnosticMessage) { - currentTest.diagnostic = diagnosticMessage - } - if (lastTestInStack()) { lastTestInStack().tests.push(currentTest) } else { @@ -111,7 +113,7 @@ export default async function parseReport(source) { totalDuration = parseFloat(durationMatch[1]) } - appendDiagnosticMessage(message) + appendDiagnosticMessage(event.data) break } diff --git a/test/e2e/compare.sh b/test/e2e/compare.sh index c59d394..4f1ddf7 100755 --- a/test/e2e/compare.sh +++ b/test/e2e/compare.sh @@ -3,7 +3,7 @@ set -e # Function to replace test duration with 0 and remove file paths remove_variables() { - echo "$1" | sed -E 's/\"duration\": [0-9.]+/\"duration\": 0/g' | sed -E 's/\/.*\/test\//test\//g' | sed -E 's/("operator": "strictEqual"),/\1/g' | sed -E '/ *"diff": "simple"/d' + echo "$1" | sed -E 's/\"duration\": [0-9.]+/\"duration\": 0/g' | sed -E 's/\/.*\/test\//test\//g' | sed -E 's/("operator": "strictEqual"),/\1/g' | sed -E '/ *"diff": "simple"/d' | sed -E 's/duration_ms [0-9.]+/duration_ms 0/g' } # Run sample tests and generate the report, ignoring errors diff --git a/test/resources/expected.json b/test/resources/expected.json index 821daf5..1c3684e 100644 --- a/test/resources/expected.json +++ b/test/resources/expected.json @@ -31,7 +31,8 @@ "tests": [], "duration": 0, "skip": false, - "todo": false + "todo": false, + "diagnostic": "JJJJAJAJSDADS\n" }, { "name": "fails", @@ -50,7 +51,8 @@ "expected": 2, "operator": "strictEqual" } - } + }, + "diagnostic": "IIIIOASDASD\n" } ], "duration": 0, @@ -127,8 +129,9 @@ ], "duration": 0, "skip": false, - "todo": true + "todo": true, + "diagnostic": "tests 6\nsuites 5\npass 2\nfail 2\ncancelled 1\nskipped 1\ntodo 0\nduration_ms 0\n" } ], "duration": 0 -} +} \ No newline at end of file diff --git a/test/resources/sample-tests/nested.test.js b/test/resources/sample-tests/nested.test.js index 186046b..99a6ff0 100644 --- a/test/resources/sample-tests/nested.test.js +++ b/test/resources/sample-tests/nested.test.js @@ -4,11 +4,13 @@ import { describe, it } from 'node:test' describe('module', () => { describe('function', () => { describe('behavior', () => { - it('asserts 1 === 1', () => { + it('asserts 1 === 1', t => { + t.diagnostic('JJJJAJAJSDADS') assert.strictEqual(1, 1) }) - it('fails', () => { + it('fails', t => { + t.diagnostic('IIIIOASDASD') assert.strictEqual(1, 2) }) })