LironEr / cypress-mochawesome-reporter

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

Reporter not found when being use in NPM Workspaces #153

Closed callistusasir closed 1 year ago

callistusasir commented 1 year ago

Environment

What happened?

Upon following example in the repo /examples/simple-typescript started run of my cypress tests and following error was thrown

I think is due to the fact that I am using npm workspaces so all node_modules packages get hoisted into the root directory.

So projects is located in /projects/e2e, so i think the reporter is expected to be in that folder or a node modules in the folder. This is not the case.

If it were possible to specify the location of the module then that would be easier. There may be a config but I could not see it.

Config file

import { defineConfig } from 'cypress';
import setupNodeEvents from './cypress/helpers/setupNodeEvents';
import path from 'path';

export default defineConfig({
  reporter: 'cypress-mochawesome-reporter',
  reporterOptions: {
    reportDir: 'test-report',
    charts: true,
    reportPageTitle: 'custom-title',
    embeddedScreenshots: true,
    videoOnFailOnly: false,
    inlineAssets: true,
    saveAllAttempts: true
  },
  e2e: {
    video: true,
    fixturesFolder: path.resolve(__dirname, 'cypress/fixtures'),
    screenshotsFolder: path.resolve(__dirname, 'cypress/screenshots'),
    videosFolder: path.resolve(__dirname, 'cypress/videos'),
    downloadsFolder: path.resolve(__dirname, 'cypress/downloads'),
    port: 8900,

    specPattern: '**/*.feature',
    setupNodeEvents: setupNodeEvents as (
      // eslint-disable-next-line no-unused-vars
      on: Cypress.PluginEvents,
      // eslint-disable-next-line no-unused-vars
      config: Cypress.PluginConfigOptions
    ) => Promise<Cypress.PluginConfigOptions> // Typecasting for ESlint
  }
});

Setup Node Events file too for reference

import { addCucumberPreprocessorPlugin } from '@badeball/cypress-cucumber-preprocessor';
import createEsbuildPlugin from '@badeball/cypress-cucumber-preprocessor/esbuild';
import createBundler from '@bahmutov/cypress-esbuild-preprocessor';
import EnvironmentConfiguration from '../models/environmentConfig';

async function setupNodeEvents(
  on: Cypress.PluginEvents,
  config: Omit<Cypress.PluginConfigOptions, 'env'> & {
    env: EnvironmentConfiguration;
  }
): Promise<Cypress.PluginConfigOptions> {
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  require('cypress-mochawesome-reporter/plugin')(on);
  // This is required for the preprocessor to be able to generate JSON reports after each run, and more,
  await addCucumberPreprocessorPlugin(on, config);

  on(
    'before:browser:launch',
    (
      browser: Cypress.Browser,
      browserLaunchOptions: Cypress.BrowserLaunchOptions
    ) => {
      if (browser.name === 'electron') {
        browserLaunchOptions.args.push('--remote-debugging-port=9222');
        // whatever you return here becomes the new args
        return browserLaunchOptions;
      }

      return browserLaunchOptions;
    }
  );

  on(
    'file:preprocessor',
    createBundler({
      define: {
        // replaces every instance of "process.env.NODE_ENV" string
        // in the spec with the string "development"
        'process.env.NODE_ENV': '"development"'
      },
      plugins: [createEsbuildPlugin(config)]
    })
    // createBundler()
  );

  // Make sure to return the config object as it might have been modified by the plugin.
  const baseUrl = config.env.baseUrl;

  if (!baseUrl) {
    throw new Error(
      `missing 'baseUrl' environment variable please check terminal command`
    );
  }

  return config;
}

export default setupNodeEvents;

Relevant log output

Error loading the reporter: cypress-mochawesome-reporter

We searched for the reporter in these paths:

 - /home/node/app-workspace-esbuild/projects/e2e/cypress-mochawesome-reporter
 - /home/node/app-workspace-esbuild/projects/e2e/node_modules/cypress-mochawesome-reporter

Learn more at https://on.cypress.io/reporters

Error: Cannot find module '/home/node/app-workspace-esbuild/projects/e2e/node_modules/cypress-mochawesome-reporter'
Require stack:
- /home/node/.cache/Cypress/12.14.0/Cypress/resources/app/packages/server/lib/reporter.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:940:15)
    at n._resolveFilename (node:electron/js2c/browser_init:249:1105)
    at PackherdModuleLoader._tryResolveFilename (evalmachine.<anonymous>:1:743978)
    at PackherdModuleLoader._resolvePaths (evalmachine.<anonymous>:1:740799)
    at PackherdModuleLoader.tryLoad (evalmachine.<anonymous>:1:738842)
    at Function.<anonymous> (evalmachine.<anonymous>:1:749631)
    at d._load (<embedded>:4588:29304)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at E.loadReporter (<embedded>:4593:19480)
    at C.initializeReporter (<embedded>:4640:426060)
    at C.startWebsockets (<embedded>:4640:426364)
    at C.open (<embedded>:4640:424693)
    at async v.create (<embedded>:4640:437103)
    at async O (<embedded>:4640:513450)
    at async H (<embedded>:4640:526407)

Anything else?

Reference to NPM workspaces https://docs.npmjs.com/cli/v7/using-npm/workspaces

LironEr commented 1 year ago

sorry but it's more a cypress issue than the reporter https://github.com/cypress-io/cypress/issues/4536

you can try to use the relative path to the reporter