npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

junit2json

v3.1.12

Published

Convert JUnit XML format to JSON

Downloads

115,846

Readme

ts-junit2json

junit2json JSR CI

ts-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.

Purpose

ts-junit2json is created for uploading test result data to BigQuery.

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.

What is difference for other XML to JSON tools?

The purpose of other similar tools is handling common XML format. As a result, output JSON structure is sometimes redundant and not suitable for store in BigQuery.

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.

Installing

npm install junit2json

Usage

Node.js

junit2json supports both ESModule and CommonJS.

import { parse } from 'junit2json' // ESM
// const { parse } = require('junit2json') // CommonJS

const main = async () => {
  const xmlString = `<?xml version="1.0" encoding="UTF-8"?>
  <testsuites 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">
      </testcase>
    </testsuite>
    <testsuite 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>Error: Something wrong.</failure>
      </testcase>
    </testsuite>
  </testsuites>
  `

  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.

import { parse } from "jsr:@kesin11/junit2json";

Output sample

{
  "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 some tags

If you want to filter some tags like <system-out> or <system-err>, you can use replacer function argument in JSON.stringify().

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 <system-out> and <system-err>. These inner text is set to the string array.
    • Example: snapshot.test.ts.snap

CLI

npx junit2json junit.xml

# with full options
npx junit2json -p -f system-out,system-err junit.xml
junit2json <path>

Convert JUnit XML format to JSON

Positionals:
  path  JUnit XML path                                                  [string]

Options:
  --help             Show help                                         [boolean]
  --version          Show version number                               [boolean]
  -p, --pretty       Output pretty JSON                                [boolean]
  -f, --filter-tags  Filter XML tag names                               [string]

Examples:
  junit2json -p -f system-out,system-err    Output pretty JSON with filter
  junit.xml                                 <system-out> and <system-err> tags.

CLI with jq examples

Count testcases

npx junit2json junit.xml | jq .tests

Show testsuite names

npx junit2json junit.xml | jq .testsuite[].name

Show testcase classnames

npx junit2json junit.xml | jq .testsuite[].testcase[].classname

References

JUnit XML format

  • https://llg.cubic.org/docs/junit/
  • https://github.com/junit-team/junit5/blob/main/platform-tests/src/test/resources/jenkins-junit.xsd

API reference

https://jsr.io/@kesin11/junit2json/doc

License

MIT