Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions parse-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit of a hack to hold on to the last test, so that messages that come in without file, line, and column have somewhere to go.

let totalDuration = 0

function lastTestInStack() {
return testStack.length ? testStack[testStack.length - 1] : null
}

function appendDiagnosticMessage(message) {
diagnosticMessage += `${message}\n`
function appendDiagnosticMessage(data) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reordered these when I was going to use lastTestInStack. Can put them back in the previous order if this is confusing.

// 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) {
Expand All @@ -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

Expand All @@ -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
Expand All @@ -89,10 +95,6 @@ export default async function parseReport(source) {
}
}

if (diagnosticMessage) {
currentTest.diagnostic = diagnosticMessage
}

if (lastTestInStack()) {
lastTestInStack().tests.push(currentTest)
} else {
Expand All @@ -111,7 +113,7 @@ export default async function parseReport(source) {
totalDuration = parseFloat(durationMatch[1])
}

appendDiagnosticMessage(message)
appendDiagnosticMessage(event.data)

break
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions test/resources/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"tests": [],
"duration": 0,
"skip": false,
"todo": false
"todo": false,
"diagnostic": "JJJJAJAJSDADS\n"
},
{
"name": "fails",
Expand All @@ -50,7 +51,8 @@
"expected": 2,
"operator": "strictEqual"
}
}
},
"diagnostic": "IIIIOASDASD\n"
}
],
"duration": 0,
Expand Down Expand Up @@ -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
}
}
6 changes: 4 additions & 2 deletions test/resources/sample-tests/nested.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down
Loading