Coder Social home page Coder Social logo

ts-junit2json's Introduction

ts-junit2json

junit2json 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

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()

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

License

MIT

ts-junit2json's People

Contributors

aslakhellesoy avatar dependabot[bot] avatar kesin11 avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ts-junit2json's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>Kesin11/renovate-config:oss)

"Could not find a declaration file for module 'xml2js'" when import junti2json with TypeScript

When use junit2json with TypeScript, it cannot build by types error.

node_modules/junit2json/dist/index.d.ts:1:20 - error TS7016: Could not find a declaration file for module 'xml2js'. '/Users/kesin/github/CIAnalyzer/node_modules/xml2js/lib/xml2js.js' implicitly has an 'any' type.
  Try `npm install @types/xml2js` if it exists or add a new declaration (.d.ts) file containing `declare module 'xml2js';`

It will be fix moving "@types/xml2js" from devDependencies to dependencies.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update dependency @types/node to v20.12.12

Pending Status Checks

These updates await pending status checks. To force their creation now, click the checkbox below.

  • chore(deps): update dependency ts-jest to v29.1.3

Detected dependencies

devcontainer
.devcontainer/devcontainer.json
  • mcr.microsoft.com/devcontainers/typescript-node 1-20
  • ghcr.io/devcontainers/features/docker-from-docker 1
  • ghcr.io/devcontainers/features/github-cli 1
  • ghcr.io/eitsupi/devcontainer-features/jq-likes 2
  • ghcr.io/guiyomh/features/vim 0
dockerfile
Earthfile
  • node 20.13.1
github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/upload-artifact v4
  • actions/checkout v4
.github/workflows/release.yml
  • release-drafter/release-drafter v6
  • actions/setup-node v4
  • earthly/actions-setup v1.0.13
  • actions/create-github-app-token v1
  • actions/checkout v4
  • release-drafter/release-drafter v6
.github/workflows/release_test.yml
  • actions/checkout v4
  • actions/setup-node v4
  • earthly/actions-setup v1.0.13
npm
package.json
  • @types/xml2js 0.4.14
  • xml2js 0.6.2
  • yargs 17.7.2
  • @types/jest 29.5.12
  • @types/node 20.12.7
  • @types/yargs 17.0.32
  • jest 29.7.0
  • jest-junit 16.0.0
  • ts-jest 29.1.2
  • tsconfig-to-dual-package ^1.2.0
  • typescript 5.4.5

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.