cypress-mochawesome-reporter icon indicating copy to clipboard operation
cypress-mochawesome-reporter copied to clipboard

[solution] Cypress & with Yarn PnP

Open OleksandrKucherenko opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

The cause:

Error loading the reporter: cypress-mochawesome-reporter

We searched for the reporter in these paths:

 - ~/workspace/telekom.de/hvc-csp/cypress-mochawesome-reporter
 - ~/workspace/telekom.de/hvc-csp/node_modules/cypress-mochawesome-reporter

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

Describe the solution you'd like

Step #1: Add mocha a required yarn package, so yarn PnP can resolve it for Cypress.

yarn add --dev mocha

Step #2: create in the root of the project file cypress-mochawesome-reporter.js with content:

const path = require("node:path");
const cwd = process.cwd();

// ref: https://yarnpkg.com/advanced/pnpapi
console.log("cypress-mochawesome-reporter.js: cwd =", cwd);
console.log("cypress-mochawesome-reporter.js: PnP =", path.join(cwd, ".pnp.cjs"));

const pnp = require(path.join(cwd, ".pnp.cjs"));
pnp.setup();
console.log("cypress-mochawesome-reporter.js: pnp =", process.versions.pnp);

// NOTE: at this point require('module') will be already patched by pnp and
// will have findPnpApi method; also `require('pnpapi')` will start to work.

let instance = {};

try {
  // ref: https://yarnpkg.com/advanced/pnpapi#requiremodule
  const { createRequire, findPnpApi } = require(`module`);
  const targetModule = cwd + "/";
  console.log("cypress-mochawesome-reporter.js: targetModule =", targetModule);

  const targetPnp = findPnpApi(targetModule);
  // console.log("cypress-mochawesome-reporter.js: targetPnp =", targetPnp);

  const targetRequire = createRequire(targetModule);
  // console.log("cypress-mochawesome-reporter.js: targetRequire =", targetRequire);

  const resolved = targetPnp.resolveRequest("cypress-mochawesome-reporter", targetModule);
  console.log("cypress-mochawesome-reporter.js: resolved =", resolved);

  instance = targetRequire(resolved);

  console.log("cypress-mochawesome-reporter.js: reporter loaded");
} catch (error) {
  console.error("cypress-mochawesome-reporter.js: error =", error);
}

module.exports = instance;

Follow the original configuration steps.

Describe alternatives you've considered

Refs:

  • https://github.com/cypress-io/cypress/issues/8008
  • https://github.com/cypress-io/cypress/issues/18922
  • https://docs.cypress.io/guides/tooling/reporters#Custom-reporter

Additional context

Cypress reporter executed in "isolated" node instance that does not have enabled P'n'P logic.

OleksandrKucherenko avatar Oct 05 '22 11:10 OleksandrKucherenko