Jest NUnit Reporter
jest-nunit-reporter is a utility package for Jest that transforms its test results into an XML format compatible with NUnit. This allows Jest test outcomes to be consumed by CI/CD systems that expect NUnit-style reports, such as Azure DevOps or Jenkins, facilitating integration into enterprise build pipelines. The current stable version is 1.3.2. This package operates as a `testResultsProcessor` within Jest's configuration, meaning it's invoked by Jest after all tests have completed to process the full test suite results. It offers configuration options for specifying output file names and paths, and for customizing test suite naming within the generated XML, providing flexibility for integration into various CI environments. Release cadence appears infrequent, primarily addressing maintenance and minor enhancements as needed, rather than rapid feature development, as its core functionality is stable.
Common errors
-
Error: Cannot find module './node_modules/jest-nunit-reporter' from '...' at Function.resolveSync (node:internal/modules/cjs/loader: ...)
cause The `testResultsProcessor` path in your Jest configuration is incorrect or the package is not installed.fixVerify that `jest-nunit-reporter` is installed (`npm list jest-nunit-reporter`) and that the path in `jest.config.js` or `package.json` accurately points to its location, typically `./node_modules/jest-nunit-reporter`. -
No test results XML file generated.
cause Jest did not run any tests, or the `outputPath` configuration is incorrect, or an internal error occurred during report generation.fixCheck Jest's console output for test execution errors. Ensure your `jestNunitReporter.outputPath` and `outputFileName` are correctly configured and accessible by the process. Confirm Jest actually ran tests by observing console output. -
TypeError: Cannot read properties of undefined (reading 'outputPath') at Object.<anonymous> (...jest-nunit-reporter/index.js:...) (jest-nunit-reporter: 1.x)
cause The `jestNunitReporter` configuration object is missing or malformed in your Jest configuration.fixEnsure your `jest` configuration includes a valid `jestNunitReporter` object with expected properties, especially `outputPath` if you are trying to configure it, for example: `"jestNunitReporter": { "outputPath": "reports/" }`.
Warnings
- gotcha The `testResultsProcessor` path must be correctly specified. Jest resolves this path relative to your project root. An incorrect path will prevent the reporter from running.
- gotcha Output files (e.g., `test-report.xml`) are generated in the project root by default. For CI/CD environments, it's crucial to configure `outputPath` to a predictable location to ensure reports are archived correctly.
- breaking Major Jest version updates can sometimes introduce breaking changes in how `testResultsProcessor`s interact with Jest's internal result formats. While generally stable, always test with new Jest major versions.
- gotcha This reporter only processes test results; it does not directly run tests. If Jest finds no tests, the reporter will not generate an XML file or will generate an empty one, as there are no results to process.
Install
-
npm install jest-nunit-reporter -
yarn add jest-nunit-reporter -
pnpm add jest-nunit-reporter
Imports
- Reporter Configuration Path
import { NUnitReporter } from 'jest-nunit-reporter'"testResultsProcessor": "./node_modules/jest-nunit-reporter"
- Configuration Options
import NUnitReporter, { Options } from 'jest-nunit-reporter'"jestNunitReporter": { "outputPath": "reports/", "outputFileName": "custom-report.xml" }
Quickstart
{
"name": "my-jest-nunit-project",
"version": "1.0.0",
"description": "Example project for jest-nunit-reporter",
"main": "index.js",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^29.0.0",
"jest-nunit-reporter": "^1.3.1"
},
"jest": {
"testResultsProcessor": "./node_modules/jest-nunit-reporter",
"jestNunitReporter": {
"outputPath": "./reports",
"outputFileName": "nunit-test-report.xml"
}
}
}
// File: my-component.test.js
describe('My Important Component', () => {
test('should perform basic arithmetic correctly', () => {
expect(1 + 1).toBe(2);
});
test('should handle a simple string operation', () => {
expect('hello'.toUpperCase()).toBe('HELLO');
});
it('should demonstrate a skipped test', () => {
pending('This test is not yet implemented');
});
it('should potentially fail if conditions are not met', () => {
// Uncomment the line below to simulate a failing test
// expect(true).toBe(false);
});
});
// To run:
// 1. Save the above JSON as package.json
// 2. Save the JS code as my-component.test.js
// 3. Run `npm install`
// 4. Run `npm test`
// 5. Check the `reports/nunit-test-report.xml` file.