JUnit XML to JavaScript Parser
The `junitxml-to-javascript` package, currently at version 1.1.4, is a Node.js library designed for parsing JUnit XML report files or strings into structured JavaScript objects. It provides a flexible API that allows for custom modification functions to preprocess the raw XML object (initially parsed by `xml2js-parser`) before the final transformation, making it adaptable to various non-standard JUnit XML formats. The package primarily uses CommonJS `require` syntax and has an `engines.node` requirement of `>=8.2.1`, suggesting it is an older project. Its release cadence is not active, and the package appears to be in a maintenance or abandoned state. Its key differentiator is the `modifier` option, enabling deep customization of the parsing logic for specific XML structures.
Common errors
-
TypeError: Parser is not a constructor
cause Attempting to use `Parser` with ESM `import` syntax as a default export, or incorrectly calling `Parser()` without `new`.fixUse `const Parser = require('junitxml-to-javascript');` to import the module. Ensure you use `new Parser()` to create an instance. -
ENOENT: no such file or directory, open '/tmp/passed.xml'
cause The `parseXMLFile` method could not find the specified XML file at the given path.fixVerify the file path is correct and accessible. Ensure the file exists and the application has read permissions for it.
Warnings
- gotcha The package is explicitly CommonJS-only, requiring `const Parser = require('pkg');` syntax. Attempting to use ESM `import` statements will result in runtime errors.
- gotcha The package specifies an old Node.js engine requirement (`>=8.2.1`). While it might run on newer Node.js versions, it's not actively maintained for modern environments, potentially leading to compatibility issues or missed optimizations/security patches.
- gotcha This package does not ship with TypeScript type definitions. Developers using TypeScript will need to create their own declaration files (`.d.ts`) or use a global `declare module` to avoid type errors.
- deprecated The package appears to be unmaintained or in a long-term maintenance state, with no active development or recent updates indicated by its version (1.1.4) and reliance on older technologies. This could lead to a lack of support for new JUnit XML features or unresolved bugs.
Install
-
npm install junitxml-to-javascript -
yarn add junitxml-to-javascript -
pnpm add junitxml-to-javascript
Imports
- Parser
import Parser from 'junitxml-to-javascript';
const Parser = require('junitxml-to-javascript'); - new Parser().parseXMLFile
Parser.parseXMLFile('/path/to/report.xml')new Parser().parseXMLFile('/path/to/report.xml') - new Parser().parseXMLString
Parser.parseXMLString('<testsuite>...</testsuite>')new Parser().parseXMLString('<testsuite>...</testsuite>')
Quickstart
const Parser = require("junitxml-to-javascript");
const fs = require('fs');
const path = require('path');
const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="MyTestSuit" errors="0" tests="2" failures="0" skipped="0"
timestamp="2023-01-01T12:00:00+0000" time="10.5">
<properties>
<property name="build.id" value="123"/>
</properties>
<testcase classname="com.example.TestA" name="shouldPass" time="5.2"/>
<testcase classname="com.example.TestB" name="shouldAlsoPass" time="5.3"/>
</testsuite>`;
console.log('Parsing XML string...');
new Parser()
.parseXMLString(xmlString)
.then(report => {
console.log('--- Parsed XML String Output ---');
console.log(JSON.stringify(report, null, 2));
})
.catch(err => console.error('Error parsing string:', err.message));
// Demonstrating file parsing (requires creating a dummy file)
const tempFilePath = path.join(__dirname, 'temp_junit_report.xml');
fs.writeFileSync(tempFilePath, xmlString, 'utf8');
console.log('\nParsing XML file...');
new Parser({ customTag: "CI_BUILD" })
.parseXMLFile(tempFilePath)
.then(report => {
console.log('--- Parsed XML File Output ---');
console.log(JSON.stringify(report, null, 2));
})
.catch(err => console.error('Error parsing file:', err.message))
.finally(() => {
// Clean up the temporary file
fs.unlinkSync(tempFilePath);
console.log('Temporary file cleaned up.');
});