LironEr / cypress-mochawesome-reporter

Zero config Mochawesome reporter for Cypress with screenshots and videos
MIT License
163 stars 51 forks source link

Not able to generate the mochawesome merged report #203

Open naveen1133 opened 1 week ago

naveen1133 commented 1 week ago

Environment

- OS: Windows10
- Node: 20.16.0
- cypress-mochawesome-reporter: 3.8.2
- cypress: 10.9.0

What happened?

I am not able to generate the merged reports when I running the test files by using the parallel package.

"scripts": { "cy:run": "cypress run", "cy:run:parallel": "cypress-parallel -s cy:run -t 2 -d 'cypress/e2e/*/.cy.js' -r 'cypress-mochawesome-reporter' -o 'cypressParallel=true'", "clean": "rimraf cypress/reports", "generate-report": "generate-mochawesome-report --set-exit-code", "test": "npm run clean && npm run cy:run:parallel || true && npm run generate-report" },

Config file

const { defineConfig } = require('cypress');
const { lighthouse, prepareAudit } = require('@cypress-audit/lighthouse');
const { beforeRunHook, afterRunHook } = require('cypress-mochawesome-reporter/lib');
const fs = require('fs');
const path = require('path');

module.exports = defineConfig({
  e2e: {
    specPattern: 'cypress/e2e/**/*.cy.{js,ts}',
    setupNodeEvents(on, config) {
      // Cypress Mochawesome reporter
      require('cypress-mochawesome-reporter/plugin')(on);

      // Hooks for Mochawesome
      on('before:run', async (details) => {
        console.log('override before:run');
        await beforeRunHook(details);
      });

      on('after:run', async () => {
        console.log('override after:run');
        await afterRunHook();
      });

      // Hook for Lighthouse audits
      on('before:browser:launch', (browser = {}, launchOptions) => {
        prepareAudit(launchOptions);
      });
      on('task', {
        writeAccessibilityReport({ filename, content }) {
          // Define the reports directory
          const reportsDir = path.join(__dirname, '..', 'reports');  // Ensure '..' points to the correct location

          // Check if the reports directory exists, if not, create it
          if (!fs.existsSync(reportsDir)) {
            fs.mkdirSync(reportsDir);
          }

          // Define the path for the file where the report will be saved
          const filePath = path.join(reportsDir, filename);

          // Write the report content to the file
          fs.writeFileSync(filePath, content);

          console.log(`report saved as ${filePath}`);

          return null;
        }
      });

      // Task for generating Lighthouse reports
      on('task', {
        lighthouse: lighthouse((lighthouseReport) => {
          const folderPath = 'lighthouse-report';

          const filename = `lighthouse-report-${new Date().toISOString().replace(/[:.]/g, '-')}.html`;
          const filePath = path.join(folderPath, filename);

          if (!fs.existsSync(folderPath)) {
            fs.mkdirSync(folderPath, { recursive: true });
          }

          fs.writeFile(filePath, lighthouseReport.report, (error) => {
            if (error) {
              console.error('Error writing Lighthouse report:', error);
            } else {
              console.log(`Lighthouse report saved as ${filePath}`);
            }
          });
        }),
      });
    },

    // Specify the pattern for test files
    specPattern: 'cypress/e2e/**/*.cy.js',

    // Timeout and configuration options
    pageLoadTimeout: 180000, // 3 minutes
    defaultCommandTimeout: 90000, // 1.5 minutes
    requestTimeout: 60000, // 1 minute
    responseTimeout: 60000, // 1 minute

    // Retries for failed tests
    retries: {
      runMode: 1, // Number of retries when running in CI
      openMode: 0, // Number of retries when running locally
    },

    // Cypress Mochawesome reporter configuration
    reporter: 'cypress-mochawesome-reporter',
    reporterOptions: {
      reportDir: 'cypress/reports',
      reportFilename: `cypress-report-${new Date().toISOString().replace(/[:.]/g, '-')}`,
      overwrite: false,
      html: false,
      json: true,
    },
  },
});

Relevant log output

No response

Anything else?

No response

LironEr commented 1 week ago

I dont see anything special here, you can add debug flag and add the logs here.

Will be best to create a repo with the problem and steps to reproduce the issue so I can have a look.

Thanks.