mocha-detailed-json-reporter
v1.0.0
Published
Detailed JSON reporter for Mocha with DRY approach, works in Node.js and browser
Downloads
19
Maintainers
Readme
Mocha Detailed JSON Reporter
A comprehensive Mocha reporter that provides detailed test results in JSON format. Works in both Node.js and browser environments, with advanced features like console output capture, stack trace parsing, and source code inclusion.
Features
- Detailed test results in a structured JSON format
- Works in both Node.js and browser environments
- Captures console output during tests (configurable)
- Collects source code context for errors and test files
- Stack trace parsing and formatting
- Flexible output options (file, variable, window object)
- No external dependencies beyond Mocha
- TypeScript support
Installation
npm install mocha-detailed-json-reporter --save-devUsage
Node.js Environment
Command Line
mocha tests/ --reporter mocha-detailed-json-reporterWith options:
mocha tests/ --reporter mocha-detailed-json-reporter --reporter-options outputToFile=true,outputFilePath=results.jsonProgrammatic Usage
const Mocha = require('mocha');
const { DetailedJsonReporter } = require('mocha-detailed-json-reporter');
const mocha = new Mocha({
reporter: DetailedJsonReporter,
reporterOptions: {
outputToFile: true,
outputFilePath: 'results/test-results.json'
}
});
// Add files and run
mocha.addFile('test/test.js');
mocha.run(failures => {
process.exitCode = failures ? 1 : 0;
});Browser Environment
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/mocha-detailed-json-reporter/dist/browser.js"></script>
<script>
mocha.setup({
ui: 'bdd',
reporter: MochaDetailedReporter.DetailedJsonReporter
});
</script>
<!-- Your tests -->
<script src="tests.js"></script>
<script>
mocha.run();
// Results will be available in window.detailedTestResults
</script>Configuration Options
| Option | Default | Description |
| ------ | ------- | ----------- |
| outputToFile | true (Node) / false (Browser) | Whether to write results to a file |
| outputFilePath | "detailed-test-results.json" | Path to output file relative to CWD |
| outputDir | "test-results" | Directory to store test results |
| useTimestampDir | true | Create timestamped subdirectories for each run |
| splitOutput | false | Split output into multiple files by type |
| outputToVariable | false | Store results in a global variable |
| variableName | "detailedTestResults" | Name of the variable to store results |
| outputToWindow | false (Node) / true (Browser) | Store results in window object |
| windowVariableName | "detailedTestResults" | Name of window property to store results |
| captureConsoleLogs | true | Capture console output during test execution |
| captureConsoleTypes | ["log", "info", "warn", "error"] | Which console methods to capture |
| maxConsoleLogSize | 100000 | Maximum size of captured console logs in characters |
Output Format
The reporter generates a detailed JSON structure with test results, environment info, source code, console logs, and more. See the Output Format Documentation for details.
Example Output
{
"stats": {
"suites": 1,
"tests": 3,
"passes": 2,
"failures": 1,
"pending": 0,
"duration": 15,
"start": "2023-06-01T12:30:45.678Z",
"end": "2023-06-01T12:30:45.693Z"
},
"environment": {
"node": "v16.15.0",
"platform": "darwin",
"mocha": "10.2.0"
},
"rootSuiteId": "root-suite",
// ... More detailed structure (see documentation)
}Browser Support
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
Contributing
Contributions are welcome! See CONTRIBUTING.md for details.
License
MIT
