Skip to content

test_runner: JUnit report is truncated when a failed test triggers empty context.diagnostic() #65355

Description

@AlexCannonball

Version

v24.15.0

Platform

Microsoft Windows NT 10.0.26200.0 x64

Subsystem

test_runner

What steps will reproduce the bug?

  1. Create any test file, like junit.test.mjs:

    import { fail } from 'node:assert/strict';
    import { test } from 'node:test';
    
    await test('testName', (context) => {
      context.diagnostic('');
      fail();
    });
  2. Run the test via console

    node --test --test-reporter=junit ".\junit.test.mjs"

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

The JUnit reporter should gracefully handle empty diagnostic messages (e.g., context.diagnostic('')). It should format them as empty XML comments (<!-- -->) without throwing errors, aligning with the default spec reporter which successfully prints empty lines for empty diagnostic events. The generated XML report must remain valid and fully closed with the </testsuites> tag.

What do you see instead?

When a failed test contains an empty diagnostic call, the internal XML formatter (treeToXML) evaluates the falsy empty string if (comment) as false and mistakenly tries to read ObjectEntries(attrs) from an undefined object. This causes an unhandled TypeError inside the stream generator, silently destroying the stream and leaving the JUnit XML report truncated and broken.

Stack trace:

TypeError: Cannot convert undefined or null to object
    at entries (<anonymous>)
    at treeToXML (node:internal/test_runner/reporter/junit:46:7)
    at junitReporter (node:internal/test_runner/reporter/junit:160:11)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions