Convert JUnit XML format to JSON
npm install junit2jsonts-juni2json provides a converter that convert JUnit XML format to JSON. Also provides TypeScript types definition.
And also provide CLI that can convert a JUnit XML to JSON.
Many languages and test frameworks supporting output test result data as JUnit XML format that de fact standard in today. On the other hand, BigQuery does not support to import XML but does support JSON.
You notice that you can upload test data to BigQuery with converting XML to JSON. ts-junit2json provides a simple JUnit XML to JSON converter for that purpose.
On the other hand, ts-junit2json only supports JUnit XML schema, but restructures original XML structure into a BigQuery friendly structure. Details are described below.
bash
npm install junit2json
`Usage
Node.js
junit2json supports both ESModule and CommonJS.`ts
import { parse } from 'junit2json' // ESM
// const { parse } = require('junit2json') // CommonJSconst main = async () => {
const xmlString =
const output = await parse(xmlString)
console.log(JSON.stringify(output, null, 2))
}
main()
`Deno
junit2json also published to jsr.io as @kesin11/junit2json.
If you using Deno, you can import from jsr as below.`ts
import { parse } from "jsr:@kesin11/junit2json";
`Output sample
`json
{
"name": "gcf_junit_xml_to_bq_dummy",
"tests": 2,
"failures": 1,
"time": 1.506,
"testsuite": [
{
"name": "__tests__/basic.test.ts",
"errors": 0,
"failures": 0,
"skipped": 0,
"timestamp": "2020-01-26T13:45:02",
"time": 1.019,
"tests": 1,
"testcase": [
{
"classname": "convert xml2js output basic",
"name": "convert xml2js output basic",
"time": 0.01
}
]
},
{
"name": "__tests__/snapshot.test.ts",
"errors": 0,
"failures": 1,
"skipped": 0,
"timestamp": "2020-01-26T13:45:02",
"time": 1.105,
"tests": 1,
"testcase": [
{
"classname": "parse snapshot nunit failure xml",
"name": "parse snapshot nunit failure xml",
"time": 0.013,
"failure": [
{
"inner": "Error: Something wrong."
}
]
}
]
}
]
}
`Filter out some tags
If you want to filter out some tags like or , you can use replacer function argument in JSON.stringify().`ts
const output = await parse(xmlString)
const replacer = (key: any, value: any) => {
if (key === 'system-out' || key === 'system-err') return undefined
return value
}
console.log(JSON.stringify(output, replacer, 2))
`Notice
ts-junit2json changes the structure of some tags for simpler and more consistent output.- XML Tag inner text is set to value of 'inner' key.
- The only exceptions are
and . These inner text is set to the string array.
- Example: snapshot.test.ts.snapCLI
`bash
npx junit2json junit.xmlwith full options
npx junit2json -p -f system-out,system-err junit.xml
``
junit2json - Convert JUnit XML format to JSONPositionals:
path JUnit XML path (use '-' for stdin) [string]
Options:
--help Show help [boolean]
--version Show version number [boolean]
-p, --pretty Output pretty JSON[boolean] [default: false]
-f, --filter-tags Filter XML tag names [string]
Examples:
# Output pretty JSON with filter and tags.
npx junit2json -p -f system-out,system-err junit.xml
# Pipe node --test junit output into junit2json
node --test --test-reporter=junit ... | npx junit2json -p -
`CLI with
jq examples
$3
`bash
npx junit2json junit.xml | jq .tests
`$3
`bash
npx junit2json junit.xml | jq .testsuite[].name
`$3
`bash
npx junit2json junit.xml | jq .testsuite[].testcase[].classname
``- https://llg.cubic.org/docs/junit/
- https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsd