Mocha Common Test Report Format (CTRF) JSON Reporter
This package provides a Mocha test reporter that generates test results in the Common Test Report Format (CTRF) as a JSON file. CTRF is an open, community-driven standard for normalizing test reports across different programming languages and test frameworks, enabling consistent validation, merging, comparison, and analysis of test results. The current stable version is `0.0.11`. Releases appear to be ad-hoc, primarily driven by dependency updates or feature additions, without a strict cadence. Its key differentiator is its adherence to the CTRF specification, providing a standardized output that integrates into a broader ecosystem of tools designed to consume CTRF data, abstracting away framework-specific report parsing. This allows for unified reporting in CI/CD pipelines.
Common errors
-
Error: Cannot find module 'mocha-ctrf-json-reporter'
cause The reporter package has not been installed or is not accessible in the current execution environment.fixRun `npm install --save-dev mocha-ctrf-json-reporter` to install the package as a development dependency. -
Report file 'ctrf-report.json' not found in 'ctrf/' directory.
cause Mocha tests might not have executed successfully, the reporter encountered an error, or the `outputDir` and `outputFile` configuration was incorrect, leading the report to be generated elsewhere or not at all.fixVerify that your Mocha tests are running and passing. Double-check the `reporterOptions` in your `.mocharc.js` or command-line arguments for correct `outputDir` and `outputFile` settings. Also, ensure the specified output directory exists or can be created by the reporter. -
Mocha exited with error code 1 without generating a report.
cause This typically indicates that tests failed, or Mocha itself encountered a critical error before the reporter could finalize and write the report. The reporter will only write a report if the test run completes, even with failures.fixExamine Mocha's console output for specific test failures or critical errors. Address any failing tests or configuration issues. Ensure Mocha is configured to run at least one test; an empty test run might not produce a report.
Warnings
- gotcha Reporter options are typically defined in `.mocharc.js`, `.mocharc.json`, or via CLI arguments. Programmatic configuration of Mocha reporters with specific `reporterOptions` can sometimes be less intuitive or require direct instantiation of the reporter class, which is not the primary use case for this package.
- gotcha The default output file location is `ctrf/ctrf-report.json`. If not explicitly configured, multiple test runs or CI/CD pipelines might overwrite previous reports. Furthermore, ensure the output directory is accessible for writing.
- gotcha The Common Test Report Format (CTRF) is an evolving open standard. While this reporter aims for compliance with the latest specification, users consuming CTRF reports in other tools should ensure all components (reporter, consuming tools) are compatible with the specific CTRF version being generated to avoid parsing errors or data interpretation mismatches.
Install
-
npm install mocha-ctrf-json-reporter -
yarn add mocha-ctrf-json-reporter -
pnpm add mocha-ctrf-json-reporter
Imports
- CTRFReporter
import { CTRFReporter } from 'mocha-ctrf-json-reporter';import CTRFReporter from 'mocha-ctrf-json-reporter';
- ReporterOptions
import { ReporterOptions } from 'mocha-ctrf-json-reporter';import type { ReporterOptions } from 'mocha-ctrf-json-reporter'; - CTRFReport
import { CTRFReport } from 'mocha-ctrf-json-reporter';import type { CTRFReport } from 'mocha-ctrf-json-reporter';
Quickstart
npm install --save-dev mocha mocha-ctrf-json-reporter
// .mocharc.js
module.exports = {
reporter: 'mocha-ctrf-json-reporter',
reporterOptions: {
outputFile: 'my-custom-ctrf-report.json',
outputDir: './test-reports',
appName: 'MyAwesomeApp',
appVersion: '1.0.0',
buildName: 'CI Build',
buildNumber: process.env.CI_BUILD_NUMBER ?? 'local-run',
testEnvironment: process.env.NODE_ENV ?? 'development'
}
};
// test/example.test.js
const assert = require('assert');
describe('Application Feature', function() {
it('should handle basic arithmetic correctly', function() {
assert.strictEqual(2 + 2, 4);
});
it('should skip this test as an example', function() {
this.skip();
assert.fail('This test should not be executed');
});
it('should correctly identify string length', function() {
assert.strictEqual('hello'.length, 5);
});
});
// To run the tests, add to package.json scripts:
// "scripts": {
// "test": "mocha"
// }
// Then run: npm test
// The report will be found at ./test-reports/my-custom-ctrf-report.json