TypeScript xUnit XML Reporter

1.2.0 · maintenance · verified Sun Apr 19

The `typescript-xunit-xml` package provides a command-line interface and a programmatic API for converting the diagnostic output of the TypeScript compiler (`tsc`) into an xUnit-style XML format. This conversion is highly beneficial for integrating TypeScript build processes with continuous integration (CI) systems like Jenkins, GitLab CI, or Azure DevOps, allowing them to interpret compilation errors and warnings as test failures. The package is currently at version 1.2.0, with its last publish occurring approximately three years ago, suggesting a maintenance rather than active development cadence. Its primary differentiator lies in its focused design specifically for `tsc` output, offering a streamlined solution for reporting TypeScript-specific issues without requiring broader testing frameworks. While it functions effectively as a CLI tool via pipe operations, it also exposes `parse` and `format` functions for direct programmatic use within Node.js applications, enabling more custom integration scenarios.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic usage of `typescript-xunit-xml` to process TypeScript compiler output from a dummy project and generate an xUnit XML report, simulating typical CI/CD integration.

import { readFileSync, writeFileSync } from 'fs';
import { spawnSync } from 'child_process';
import { parse, format } from 'typescript-xunit-xml'; // Programmatic API

const tsConfigFile = 'tsconfig.json';
const tsCodeFile = 'src/index.ts';

// Ensure src directory exists and create dummy files for demonstration
if (!require('fs').existsSync('src')) require('fs').mkdirSync('src');
writeFileSync(tsCodeFile, `
function greet(name: string): string {
    return \`Hello, \${name}!\`;
}
const x: number = 'hello'; // Intentionally cause a type error for the demo
console.log(greet("World"));
`);
writeFileSync(tsConfigFile, `
{
  "compilerOptions": {
    "target": "es2016", "module": "commonjs", "strict": true,
    "esModuleInterop": true, "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true, "noEmit": true
  },
  "include": ["src/**/*.ts"]
}
`);

console.log("Running tsc and piping output to typescript-xunit-xml...");
// Simulate the CLI usage: tsc --project . --noEmit | typescript-xunit-xml
// The --noEmit flag is crucial for tsc to output diagnostics to stdout/stderr instead of compiling files.
const tscProcess = spawnSync('tsc', ['--project', '.', '--noEmit'], { encoding: 'utf8' });

if (tscProcess.status !== 0) {
    console.warn("TypeScript compilation errors detected (expected for this demo).");
} else {
    console.log("No TypeScript errors. Outputting empty xUnit XML.");
}

// tsc often sends errors to stderr, so check both
const tscOutput = (tscProcess.stderr || tscProcess.stdout).toString();

// Use the programmatic API to process the output
const parsedMessages = parse(tscOutput);
const xunitXml = format(parsedMessages);

writeFileSync('junit.xml', xunitXml);
console.log("Generated junit.xml with xUnit report (check 'junit.xml' for content):");
// console.log(xunitXml); // Uncomment to see the XML directly in console

// Clean up dummy files
try {
  require('fs').unlinkSync(tsCodeFile);
  require('fs').unlinkSync(tsConfigFile);
  require('fs').rmdirSync('src');
  console.log("Cleaned up dummy files.");
} catch (e) {
  console.error("Cleanup failed:", e.message);
}

view raw JSON →