cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.44k stars 3.2k forks source link

Cypress + Reporter Plugin in nested projects. Could not load reporter by name: reporter #4536

Open danyfoo opened 5 years ago

danyfoo commented 5 years ago

Current behavior:

Based on the project: https://github.com/cypress-io/cypress-test-nested-projects I'm trying to use a reporter (In these case mochawesome)to generate report logs of my runs in cypress, but it always expected the reporter was installed inside the nested project so cypress never run the tests.

Image with the error that throw: image

Here is a example repo where you can test this behavior: https://github.com/danyfu/cy_mocha_awesome_nested_folders_tests

Desired behavior:

Cypress runs with the desired reporter without the nested level of the project.

Steps to reproduce: (app code and test code)

Execute npm run cy:run:foo

This is what nested project foo/cypress.json has:

{
    "reporter": "mochawesome",
    "reporterOptions": {
        "mochaFile": "cypress/results/results-[hash].xml"
    }
}

Versions

cypress: ^3.3.1 Chrome: Version 74.0.3729.169 (Official Build) (64-bit) OS: MacOS 10.14.5 (Mojave)

pgroot91 commented 5 years ago

Also stumbled on the same issue!

Joelasaur commented 5 years ago

Same issue here.

My cypress.json file:

"reporter": "mochawesome", "reporterOptions": { "overwrite": false, "html": false, "json": true }

Doing npm install mocha "fixes" the issue, but I shouldn't have to install mocha to make this work, seeing as Cypress already has it.

Actually installing mocha doesn't fix the issue at all. Halfway through the test run, I'm getting mocha related errors.

TypeError: Cannot read property 'passes' of undefined

Is this because the version of mocha I am installing is independent from what Cypress and/or Mochawesome is using?

This issue has been listed as fixed as of 3.0.0, but I'm on 3.4.0 and still getting it. https://github.com/cypress-io/cypress/issues/1192

Alright I'm going to try the fix authored by YOU54F, see if it works https://github.com/cypress-io/cypress/issues/3537

Actually just downgrading to Mocha 5.2 fixed the issue for me

danyfoo commented 5 years ago

@Joelasaur the problem is because cypress tries to execute Mochawesome inside foo/node_modules.

So I just write a workaround script to run cypress, to define the Mochawesome configuration on the script:

const cypress = require('cypress');
const fs_extra = require('fs-extra');
const { merge } = require('mochawesome-merge');
const generator = require('mochawesome-report-generator');

const project = process.env.PROJECT;

const reportDir = `./results/${project}/reports`;

const mochawesome_options = {
    'timestamp': false,
    'reportDir': reportDir,
    'overwrite': false,
    'html': false,
    'json': true
};

const cypress_options = {
    config: {
        'fileServerFolder': `./source/${project}`,
        'fixturesFolder': `./source/${project}/cypress/fixtures`,
        'integrationFolder': `./source/${project}/cypress/integration`,
        'pluginsFile': `./source/${project}/cypress/plugins/index.js`,
        'supportFile': `./source/${project}/cypress/support/index.js`,
        'screenshotsFolder': `./results/${project}/screenshots`,
        'videosFolder': `./results/${project}/videos`,
        'video': false,
        'reporter': 'mochawesome',
        'reporterOptions': mochawesome_options
    },
    // spec: './source/foo/cypress/integration/examples/actions*.spec.js'
};

async function runTests() {
    try {
        await fs_extra.emptyDir(reportDir);
        await cypress.run(cypress_options);
        const jsonReport = await merge({ reportDir: reportDir });
        await generator.create(jsonReport, mochawesome_options);
    } catch (error) {
        console.error(error);
    } finally {
        process.exit(0);
    }
}

runTests();

The workaround also needs to have a cypress.json on the root of the project with empty object written on the file, to make these approach work.

And you can execute the project using PROJECT=foo node ./run_scripts/run.js

This workaround solution is pushed on the project I written before https://github.com/danyfu/cy_mocha_awesome_nested_folders_tests

adamtay commented 5 years ago

Thanks for sharing @danyfu but this workaround would mean having to rely on a run script and losing the ability to run default cli commands.

Would be great if cypress could also search for node_modules at the project root directory instead of just looking at the nested project directory.

danyfoo commented 5 years ago

@adamtay, You're right!. That's why I need to select the project to run cypress via node environment parameter.

Having a node modules for each project can be an excessive amount of storage.

heaven commented 5 years ago

We had the same issue with the cypress-teamcity-reporter. The problem was resolved by adding that package to nohoist array of the global package.json:

{
  "workspaces": {
    "packages": [
      "packages/*"
    ],
    "nohoist": [
      "**/cypress-teamcity-reporter",
      "**/cypress-teamcity-reporter/**"
    ]
  },
}
mihalcan commented 5 years ago

Setting the relative path to mochawesome.js worked fine for me:


"reporter": "../../node_modules/mochawesome/src/mochawesome.js",
"reporterOptions": {
    ...
 }`
steinybot commented 11 months ago

Cypress is doing something special here which is messing it up. The loader by default searches upwards and so naturally should work in nested projects. Looks as though it was the fix for https://github.com/cypress-io/cypress/issues/658 which breaks this.

steinybot commented 11 months ago

Actually no it was there much longer than that. It looks as though it has been that way for a long time, at least as far back as https://github.com/cypress-io/cypress/commit/98c4538ddda568a294f665600ff527a0db86b3ab