Bird
Raised Fist0
Cypresstesting~15 mins

Mochawesome reporter setup in Cypress - Build an Automation Script

Choose your learning style10 modes available
Setup Mochawesome reporter in Cypress and verify report generation
Preconditions (3)
Step 1: Install mochawesome and mochawesome-merge npm packages
Step 2: Configure Cypress to use mochawesome reporter in cypress.config.js
Step 3: Run a sample Cypress test
Step 4: Verify that mochawesome report files are generated in the specified folder
Step 5: Open the generated mochawesome report HTML file in a browser
Step 6: Verify the report shows test results with pass/fail status and test details
✅ Expected Result: Mochawesome report files (JSON and HTML) are generated after test run, and the HTML report correctly displays the test results with pass/fail status and details.
Automation Requirements - Cypress
Assertions Needed:
Verify mochawesome report JSON file exists after test run
Verify mochawesome report HTML file exists after test run
Verify report HTML contains test suite and test case names
Verify test pass/fail status is correctly shown in report
Best Practices:
Use cypress.config.js for reporter configuration
Use npm scripts to run tests and generate reports
Use file system checks to verify report files
Avoid hardcoded paths; use relative paths
Clean up reports folder before test run
Automated Solution
Cypress
/// <reference types="cypress" />

import { existsSync, readFileSync } from 'fs';
import path from 'path';

describe('Mochawesome Reporter Setup Test', () => {
  const reportDir = 'cypress/reports/mochawesome';
  const jsonReport = path.join(reportDir, 'mochawesome.json');
  const htmlReport = path.join(reportDir, 'mochawesome.html');

  before(() => {
    // Clean reports folder before tests
    if (existsSync(jsonReport)) {
      // Delete files if needed - simplified here
    }
  });

  it('runs a sample test', () => {
    cy.visit('https://example.cypress.io');
    cy.contains('type').click();
    cy.url().should('include', '/commands/actions');
    cy.get('.action-email').type('test@example.com').should('have.value', 'test@example.com');
  });

  it('verifies mochawesome report files exist and contain test info', () => {
    cy.wait(1000); // wait for report generation
    cy.task('fileExists', jsonReport).should('be.true');
    cy.task('fileExists', htmlReport).should('be.true');

    cy.task('readFile', htmlReport).then((content) => {
      expect(content).to.include('Mochawesome Report');
      expect(content).to.include('Mochawesome Reporter Setup Test');
      expect(content).to.match(/<span class="pass">1</span>/);
    });
  });
});

// In cypress.config.js
// import { defineConfig } from 'cypress';
// export default defineConfig({
//   reporter: 'mochawesome',
//   reporterOptions: {
//     reportDir: 'cypress/reports/mochawesome',
//     overwrite: false,
//     html: true,
//     json: true
//   },
//   e2e: {
//     setupNodeEvents(on, config) {
//       const fs = require('fs');
//       on('task', {
//         fileExists(filePath) {
//           return fs.existsSync(filePath);
//         },
//         readFile(filePath) {
//           return fs.readFileSync(filePath, 'utf8');
//         }
//       });
//     }
//   }
// });

// npm scripts example in package.json
// "scripts": {
//   "test": "cypress run"
// }

This test suite does two things:

  • Runs a simple Cypress test visiting a page and typing text.
  • Checks that the mochawesome report files (JSON and HTML) are created after the test run.

The cypress.config.js is configured to use mochawesome as the reporter with JSON and HTML output enabled in a specific folder.

We use Cypress tasks to check if the report files exist and to read the HTML report content. This allows us to assert that the report contains the test suite name and shows the test pass count.

This setup ensures the mochawesome reporter is correctly generating reports and that the reports reflect the test results.

Common Mistakes - 4 Pitfalls
Not configuring the reporter in cypress.config.js
Checking for report files immediately after test without wait
Hardcoding absolute paths for report files
Not using Cypress tasks to access file system
Bonus Challenge

Now add data-driven testing with 3 different URLs and verify mochawesome reports for each run

Show Hint

Practice

(1/5)
1. What is the main purpose of using the mochawesome reporter in Cypress testing?
easy
A. To create test data for Cypress tests
B. To speed up test execution time
C. To automatically fix failing tests
D. To generate clear and detailed HTML and JSON test reports

Solution

  1. Step 1: Understand the role of reporters in Cypress

    Reporters provide a way to view test results in a readable format after tests run.
  2. Step 2: Identify what mochawesome specifically does

    Mochawesome generates detailed HTML and JSON reports that are easy to read and share.
  3. Final Answer:

    To generate clear and detailed HTML and JSON test reports -> Option D
  4. Quick Check:

    Reporter purpose = Generate reports [OK]
Hint: Mochawesome = report generator, not test runner or fixer [OK]
Common Mistakes:
  • Thinking mochawesome speeds up tests
  • Confusing mochawesome with test data tools
  • Assuming mochawesome fixes test failures
2. Which of the following is the correct way to enable mochawesome reporter in cypress.config.js?
easy
A. reporter: 'mochawesome', reporterOptions: { reportDir: 'cypress/reports' }
B. reporter: 'mocha', reporterOptions: { output: 'cypress/reports' }
C. reporter: 'mochawesome', outputDir: 'cypress/reports'
D. reporter: 'mochawesome', reportPath: 'cypress/reports'

Solution

  1. Step 1: Recall the correct reporter property name

    The reporter is set using the key reporter with value 'mochawesome'.
  2. Step 2: Use the correct options key for mochawesome

    The options for mochawesome are passed under reporterOptions, including reportDir for output folder.
  3. Final Answer:

    reporter: 'mochawesome', reporterOptions: { reportDir: 'cypress/reports' } -> Option A
  4. Quick Check:

    Correct keys = reporter + reporterOptions [OK]
Hint: Use reporterOptions, not outputDir or reportPath [OK]
Common Mistakes:
  • Using 'mocha' instead of 'mochawesome'
  • Wrong option keys like outputDir or reportPath
  • Missing reporterOptions wrapper
3. Given this snippet in cypress.config.js:
reporter: 'mochawesome',
reporterOptions: {
  reportDir: 'cypress/reports',
  overwrite: false,
  html: false,
  json: true
}
What will be the result after running tests?
medium
A. HTML and JSON reports will be generated and overwritten each run
B. Only HTML reports will be generated and overwrite previous reports
C. Only JSON reports will be generated without overwriting previous ones
D. No reports will be generated because html is false

Solution

  1. Step 1: Analyze reporterOptions settings

    html is set to false, so no HTML report is generated. json is true, so JSON report is generated.
  2. Step 2: Understand overwrite behavior

    overwrite is false, so new reports will not overwrite existing ones, allowing multiple JSON reports to accumulate.
  3. Final Answer:

    Only JSON reports will be generated without overwriting previous ones -> Option C
  4. Quick Check:

    html=false + json=true + overwrite=false = JSON only, no overwrite [OK]
Hint: html false means no HTML report; overwrite false keeps old reports [OK]
Common Mistakes:
  • Assuming HTML report is generated when html=false
  • Thinking overwrite=false deletes old reports
  • Confusing json and html options
4. You configured mochawesome in cypress.config.js but after running tests, no report files appear. Which fix is most likely correct?
medium
A. Remove reporterOptions from config to fix issue
B. Install mochawesome and mochawesome-merge packages and update config
C. Change reporter to 'mocha' to generate reports
D. Run tests with --no-report flag to enable reports

Solution

  1. Step 1: Check if mochawesome packages are installed

    Mochawesome requires installing both mochawesome and mochawesome-merge packages to generate and merge reports.
  2. Step 2: Verify config and installation

    Without these packages installed, no reports will be created even if config is correct.
  3. Final Answer:

    Install mochawesome and mochawesome-merge packages and update config -> Option B
  4. Quick Check:

    Missing packages = no reports [OK]
Hint: Always install mochawesome and merge packages before running tests [OK]
Common Mistakes:
  • Removing reporterOptions thinking it fixes issue
  • Changing reporter to 'mocha' which doesn't generate mochawesome reports
  • Using wrong CLI flags like --no-report
5. You want to generate a single combined HTML report after running multiple Cypress test runs with mochawesome. Which approach is correct?
hard
A. Use mochawesome-merge to combine JSON reports, then generate HTML from merged JSON
B. Run tests with overwrite: true and html: true to auto-merge reports
C. Set reporter to 'mochawesome-merge' in cypress.config.js to merge reports automatically
D. Run tests multiple times with html: false and manually copy JSON files into one

Solution

  1. Step 1: Understand mochawesome report generation

    Mochawesome generates separate JSON reports per test run; it does not merge automatically.
  2. Step 2: Use mochawesome-merge tool

    mochawesome-merge combines multiple JSON reports into one, which can then be converted to a single HTML report.
  3. Step 3: Confirm other options are incorrect

    overwrite: true overwrites reports, no auto-merge; 'mochawesome-merge' is not a reporter; manual copying is error-prone.
  4. Final Answer:

    Use mochawesome-merge to combine JSON reports, then generate HTML from merged JSON -> Option A
  5. Quick Check:

    Merge JSON then generate HTML = combined report [OK]
Hint: Merge JSON reports first, then create one HTML report [OK]
Common Mistakes:
  • Expecting mochawesome to auto-merge reports
  • Setting reporter to 'mochawesome-merge' which is not valid
  • Manually copying JSON files without merging
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard