Skip to content

Karma JUnit Reporter

Most CI systems and dashboards understand the JUnit XML test report format regardless of what language or framework produced it. This lesson covers karma-junit-reporter, the standard way to generate that format from Karma.

Why JUnit XML Matters for CI

JUnit XML originated with the Java testing ecosystem, but it became a de facto universal format that CI platforms (Jenkins, GitHub Actions, GitLab CI, CircleCI, and others) know how to parse for test result summaries, historical trend graphs, and pull request annotations. Generating it from Karma lets your JavaScript test results plug directly into that tooling.

npm install --save-dev karma-junit-reporter

// karma.conf.js
config.set({
  reporters: ['progress', 'junit'],
  junitReporter: {
    outputDir: 'test-results',
    outputFile: 'karma-results.xml',
    useBrowserName: true
  }
});

Keeping progress alongside junit gives you readable terminal output plus the machine-readable file CI systems consume.

Common junitReporter Options

junitReporter: {
  outputDir: 'test-results',   // directory the XML file is written into
  outputFile: 'results.xml',    // filename within outputDir
  suite: '',                     // optional top-level suite name prefix
  useBrowserName: true,           // separate results per browser
  nameFormatter: undefined,        // customize test case names
  classNameFormatter: undefined,    // customize test case class names
  properties: {}                     // extra <properties> metadata in the XML
}
  • useBrowserName: true is important when running multiple browsers, so results don't collide or overwrite each other.
  • outputDir should typically be an artifact directory your CI system is already configured to collect.
  • nameFormatter/classNameFormatter let you reshape how test names appear in CI dashboards.
  • The generated XML follows the same general schema most JUnit-consuming CI integrations expect.

JUnit Reporter Cheat Sheet

Key settings and their purpose.

Setting Purpose
outputDir Directory the XML report is written to
outputFile Filename for the generated report
useBrowserName Separates results per browser to avoid collisions
suite Optional prefix added to the top-level suite name
properties Extra metadata embedded in the XML output

Wiring Up JUnit Output in a CI Pipeline

Most CI platforms need you to explicitly tell them where to find the generated JUnit XML file, since Karma itself has no knowledge of the CI platform it's running under.

# GitHub Actions step example
- name: Run Karma tests
  run: npx karma start --single-run

- name: Publish test results
  uses: dorny/test-reporter@v1
  with:
    name: Karma Tests
    path: 'test-results/*.xml'
    reporter: java-junit

The reporter/action name and syntax vary by CI platform, but the underlying idea — point it at the generated XML — is universal.

Handling Multiple Browsers in JUnit Output

With useBrowserName: true, each browser's results are clearly separated in the generated XML, which prevents a Chrome failure and a Firefox failure for the 'same' spec from being confused with each other in a CI dashboard.

Common Mistakes

  • Forgetting useBrowserName: true when running multiple browsers, causing ambiguous or overwritten results.
  • Not pointing the CI platform's test-result step at the correct outputDir/outputFile path.
  • Committing generated JUnit XML files to version control instead of treating them as CI artifacts.
  • Assuming JUnit output alone gives useful information without also keeping a human-readable terminal reporter.

Key Takeaways

  • karma-junit-reporter generates JUnit XML, a format broadly understood by CI dashboards and platforms.
  • useBrowserName: true is essential when running more than one browser to avoid collisions.
  • Generated XML files should be treated as CI artifacts, not committed to version control.
  • Pairing junit with a terminal reporter gives both human-readable and machine-readable output.

Pro Tip

Confirm your CI platform's exact expected artifact path before assuming JUnit integration 'isn't working.' The single most common cause of a missing test report in a CI dashboard is a mismatched outputDir rather than a Karma configuration bug.