LironEr / cypress-mochawesome-reporter

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

cucumberSupport will fail if there is no examples in scenario #181

Closed Phx155 closed 6 months ago

Phx155 commented 6 months ago

Environment

- OS: Win11
- Node: 18.19.0
- cypress-mochawesome-reporter: 3.8.1
- cypress: 12.7.4

What happened?

I put import 'cypress-mochawesome-reporter/cucumberSupport'; into my step definition file and it failed with error Cannot read properties of undefined (reading 'examples')

If added more checks into IF condition (row 20 in node_modules\cypress-mochawesome-reporter\cucumberSupport.js) it looks like its working

if (scenario && scenario.examples && scenario.examples.length)

Config file

import { defineConfig } from 'cypress';
import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
import getCompareSnapshotsPlugin from 'cypress-visual-regression/dist/plugin';
import { cloudPlugin } from 'cypress-cloud/plugin';
import { configureAllureAdapterPlugins } from '@mmisty/cypress-allure-adapter/plugins';
import { verifyDownloadTasks } from 'cy-verify-downloads';
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';
import fs from 'fs-extra';
import path from 'path';

const { removeDirectory } = require('cypress-delete-downloads-folder');

export default defineConfig({
    requestTimeout: 10000,
    defaultCommandTimeout: 10000,
    viewportWidth: 1920,
    viewportHeight: 1080,
    experimentalWebKitSupport: true,
    experimentalMemoryManagement: true,
    experimentalModifyObstructiveThirdPartyCode: true,
    numTestsKeptInMemory: 10,
    video: false,
    chromeWebSecurity: false,
    projectId: 'LOCALHOST',
    retries: {
        runMode: 1,
        openMode: 0,
    },
    env: {
        allure: false,
        allureResultsPath: 'cypress/results/allure-results',
        allureResults: 'cypress/results/allure-results',
        allureCleanResults: false,
        allureAttachRequests: true,
        allureAddAnalyticLabels: true,
        allureAddVideoOnPass: false,
        hars_folders: 'cypress/results/hars',
        gibLanguage: 'en',
        snapshotBaseDirectory: 'cypress/screenshotsBaseline',
        snapshotDiffDirectory: 'cypress/screenshotsDif',
        integrationFolder: 'cypress/screenshotsInt',
        type: 'actual',
        experimentalOriginDependencies: true,
    },
    reporter: 'cypress-multi-reporters',
    reporterOptions: {
        reporterEnabled: 'mocha-junit-reporter, cypress-mochawesome-reporter',
        attachments: true,
        attachmentFromTestContext: true,
        mochaJunitReporterReporterOptions: {
            mochaFile: 'cypress/reports/junit/junit-[hash].xml',
            attachments: true,
            jenkinsMode: true,
            attachmentFromTestContext: true,
        },
        cypressMochawesomeReporterReporterOptions: {
            reportDir: 'cypress/results/mochawesome',
            reportFilename: '[name]-[datetime]-report',
            reportTitle: 'Mochawesome Report',
            reportPageTitle: 'Mochawesome Report',
            overwrite: false,
            html: false,
            json: false,
            inline: true,
            consoleReporter: 'none',
            quiet: true,
            inlineAssets: true,
            embeddedScreenshots: true,
            saveAllAttempts: false,
            saveHtml: false,
            saveJson: true,
            useInlineDiffs: true,
        },
    },

    e2e: {
        video: false,
        // video: true,
        experimentalOriginDependencies: true,
        supportFile: 'cypress/support/e2e.ts',
        experimentalMemoryManagement: true,
        specPattern: 'cypress/gib/*.feature',
        slowTestThreshold: 20000,
        async setupNodeEvents(cypressOn, config) {
            const createEsbuildPlugin = require('@badeball/cypress-cucumber-preprocessor/esbuild').createEsbuildPlugin;
            const on = require('cypress-on-fix')(cypressOn);
            require('cypress-mochawesome-reporter/plugin')(on);
            await addCucumberPreprocessorPlugin(on, config);

            configureAllureAdapterPlugins(on, config);
            on(
                'file:preprocessor',
                createBundler({
                    plugins: [NodeModulesPolyfillPlugin(), createEsbuildPlugin(config)],
                })
            );
            on('task', verifyDownloadTasks);
            on('task', { removeDirectory });
            on('task', {
                fileExists: (filePath) => {
                    const fullPath = path.join(config.fileServerFolder, filePath);
                    return new Promise((resolve) => {
                        fs.access(fullPath, fs.constants.F_OK, (err: any) => {
                            resolve(!err);
                        });
                    });
                },
                readPDFFile: (filePath) => {
                    const fullPath = path.join(config.fileServerFolder, filePath);
                    return new Promise((resolve, reject) => {
                        fs.readFile(fullPath, 'utf8', (err: any, data: unknown) => {
                            if (err) {
                                reject(err);
                            } else {
                                resolve(data);
                            }
                        });
                    });
                },
            });
            getCompareSnapshotsPlugin(on, config);
            on('before:browser:launch', (browser, launchOptions) => {
                const VH = Number(process.env.CYPRESS_viewportHeight) || 1080;
                const VW = Number(process.env.CYPRESS_viewportWidth) || 1920;

                if (browser.name === 'chrome' && browser.isHeadless) {
                    // fullPage screenshot size is 1400x1200 on non-retina screens
                    // and 2800x2400 on retina screens
                    launchOptions.args.push(`--window-size=${VW},${VH}`);

                    // force screen to be non-retina (1400x1200 size)
                    launchOptions.args.push('--force-device-scale-factor=1');

                    // force screen to be retina (2800x2400 size)
                    // launchOptions.args.push('--force-device-scale-factor=2')
                }

                if (browser.name === 'electron' && browser.isHeadless) {
                    launchOptions.preferences.width = VW;
                    launchOptions.preferences.height = VH;
                }

                if (browser.name === 'firefox' && browser.isHeadless) {
                    launchOptions.args.push(`--width=${VW}`);
                    launchOptions.args.push(`--height=${VH}`);
                }
                return launchOptions;
            });

            const result = cloudPlugin(on, config);
            return result;
        },
    },
});

Relevant log output

No response

Anything else?

No response

LironEr commented 6 months ago

Thanks @Phx155