Node.js Test Runner JUnit Reporter
node-test-junit-reporter is a utility package designed to integrate with Node.js's built-in test runner, generating JUnit XML formatted test reports. It functions as a command-line test reporter, allowing developers to output test results in a standardized, machine-readable format compatible with Continuous Integration (CI) systems like Jenkins, GitLab CI, or GitHub Actions. The current stable version is 2.0.1. Releases occur frequently, primarily driven by dependency updates and minor bug fixes, with major versions introducing Node.js compatibility changes. Its primary differentiation lies in its direct integration with the native `node --test` command, providing a straightforward solution for projects utilizing Node.js's own testing capabilities without external test frameworks.
Common errors
-
Error: The test reporter 'node-test-junit-reporter' could not be found.
cause The package is not installed as a development dependency or Node.js cannot resolve its path from the current working directory.fixEnsure `node-test-junit-reporter` is installed in your project's `node_modules` directory (`npm install -D node-test-junit-reporter`) and that the `node` command is run from the project root or a location where local `node_modules` can be resolved. -
TypeError: node-test-junit-reporter requires Node.js v22 or higher. Current: vX.Y.Z
cause Attempting to use `node-test-junit-reporter` v2.0.0 or later with a Node.js runtime version older than v22.fixUpgrade your Node.js environment to version 22 or newer, or modify your `package.json` to use a compatible earlier version of `node-test-junit-reporter` (e.g., `"node-test-junit-reporter": "^1.0.0"`).
Warnings
- breaking Version 2.0.0 introduced a breaking change requiring Node.js version 22 or higher. Earlier Node.js versions are no longer supported.
- gotcha When using multiple `--test-reporter` flags (e.g., one for `stdout` and one for JUnit XML), ensure each reporter is immediately followed by its `--test-reporter-destination`. Incorrect ordering or omission of a destination can lead to unexpected output behavior or reports overwriting each other.
- gotcha As of v2.0.1, the `times` attribute in the generated JUnit XML report is correctly expressed in seconds. Previous versions might have reported incorrect units or values, potentially affecting CI systems that parse these metrics. Users consuming these time values should re-validate their parsing logic after updating.
Install
-
npm install node-test-junit-reporter -
yarn add node-test-junit-reporter -
pnpm add node-test-junit-reporter
Quickstart
// 1. Install the package as a development dependency:
// npm i -D node-test-junit-reporter
// 2. Create a test file, for instance, 'tests/example.test.js':
// import { test } from 'node:test';
// import assert from 'node:assert';
//
// test('passing test example', () => {
// assert.strictEqual(1, 1, 'Numbers should be identical');
// });
//
// test('failing test example', () => {
// assert.notStrictEqual(1, 1, 'This assertion is intentionally designed to fail');
// });
//
// test('asynchronous test example', async () => {
// await new Promise(resolve => setTimeout(resolve, 50));
// assert.ok(true, 'Asynchronous operation successfully completed');
// });
// 3. Add a script to your package.json to run the tests with the reporter:
// {
// "name": "my-node-project",
// "version": "1.0.0",
// "scripts": {
// "test:junit": "node --test --test-reporter node-test-junit-reporter --test-reporter-destination report.xml tests/example.test.js"
// },
// "devDependencies": {
// "node-test-junit-reporter": "^2.0.1"
// }
// }
// 4. Execute the test script from your terminal:
// npm run test:junit
// This command will run your Node.js native tests and generate a 'report.xml'
// file in your project's root directory, containing the JUnit XML formatted results.
// The report will detail test successes, failures, and execution times,
// suitable for consumption by CI/CD tools.