How lironer/cypress-mochawesome-reporter works
Project type and purpose
• This is an open-source plugin / reporter tool for Cypress tests. It integrates with the Cypress test runner, listens to test lifecycle events, and produces Mochawesome HTML reports with embedded screenshots.
File structure
• Top-level “plugin.js”, “register.js”, “cli.js” and “cucumberSupport.js” are your integration entry points.
• The “lib/” folder holds the core modules:
• config.js / consts.js – default settings and constants
• logger.js – logging support
• reporter.js – event listener that collects test results and screenshots
• enhanceReport.js – post-processing of raw JSON results
• generateReport.js – invokes the Mochawesome report generator CLI
• “examples/” and “docs/” show usage patterns but are not part of the runtime.
• “cypress.config.js”, “cypress/support/e2e.js” are how users wire up your plugin.
Extract architectural insights from the README
• The plugin hooks into Cypress’s Node event API via setupNodeEvents(on, config) in “plugin.js”.
• On test events (“before:run”, “after:spec”, “after:run”), it collects data, writes intermediate JSON, then generates a final HTML report.
• A browser-side “register.js” file injects commands (cy.addTestContext()) into Cypress’s client for attaching metadata to tests.
• Optional “cucumberSupport.js” integrates with Cypress-Cucumber preprocessors.
Main components and interactions (
a. Cypress Test Runner (Node.js process)
b. Plugin entry point (plugin.js)
c. Reporter module (lib/reporter.js)
d. Report enhancer & generator (lib/enhanceReport.js + lib/generateReport.js)
e. Browser-side commands (register.js)
f. Optional cucumber adapter (cucumberSupport.js)
g. File System (JSON & Screenshots output, final HTML report)
h. External Reporter Tool (Mochawesome Report Generator CLI)
Relationships and data flows
1. User “npm install” & “require(‘cypress-mochawesome-reporter/plugin’)” ➔ Cypress config
2. On before:run ➔ plugin.js calls beforeRunHook (initializes artifacts folder)
3. During each spec: on after:spec ➔ reporter.js collects spec results + screenshots, writes JSON
4. On after:run ➔ plugin.js calls afterRunHook which chains:
• enhanceReport.js reads all JSON, augments them
• generateReport.js spawns Mochawesome CLI to produce final HTML
5. Browser tests: register.js injects cy.addTestContext() to add per-test metadata
6. If user has Cucumber: cucumberSupport.js maps Cucumber hooks to reporter events
Architectural patterns and principles
• Event-driven: relies on Cypress’s hook API (on(…, handler)).
• Separation of concerns:
• plugin.js = wiring and lifecycle hooks
• reporter.js = data capture
• enhanceReport.js & generateReport.js = post-processing and report generation
• register.js = UI-side commands injection
• Extensibility: cucumberSupport.js is a pluggable adapter for Cucumber users.
• CLI abstraction: cli.js allows standalone invocation if needed.
Essential details to capture
• The lifecycle hook sequence and where each module intercedes
• The transition from raw test data → enhanced JSON → final HTML
• Configuration flow: user’s Cypress config ➔ plugin initialization
• Extensibility points: reporterOptions and cucumberSupport