cypress-io / code-coverage

Saves the code coverage collected during Cypress tests
MIT License
433 stars 108 forks source link

Error: Code coverage tasks not registered by the plugins file #405

Open srilakshmishankar opened 3 years ago

srilakshmishankar commented 3 years ago

Code-coverge debug logs:

code-coverage combined NYC options { 'report-dir': './coverage', reporter: [ 'lcov', 'clover', 'json', 'json-summary' ], extension: [ '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx' ], excludeAfterRemap: false } +0ms thats it and nothing else

Added in support --> index.js

import '@cypress/code-coverage/support';

In plugin index.js -->

const corePlugins = require('@company/e2e-core/plugins');

module.exports = function plugins(on, config) {
  // eslint-disable-next-line global-require
  require('@cypress/code-coverage/task')(on, config);
  corePlugins(on, config);

  return config;
};

Always get below error Screenshot 2021-02-26 at 15 26 28

tried adding this

require('@cypress/code-coverage/task')(on, config);

in e2e-core/plugins as well then it says

Warning: Multiple attempts to register the following task(s): resetCoverage, combineCoverage, coverageReport. Only the last attempt will be registered.

Versions Cypress 6.5.0 mac os catalina Node v15.8.0 NPM v7.5.3 Instrumentation: istanbul React application Code-coverage: 3.9.2 webpack-preprocessor: 5.6.0 cucumber-preprocessor: 4.0.1 Shell: xsh

We have e2e tests as feature files. Cypress is in the root where as application is as packages. We also have e2e-core package for cypress where there is also a common plugins file loaded onto cypress folder plugins-->index.js.

webpack.config.js

module.exports = {
  node: { fs: 'empty', child_process: 'empty', readline: 'empty' },
  module: {
    rules: [
      {
        test: /\.js?$/,
        exclude: [/node_modules/],
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: ['istanbul'],
          },
        }],
      },
      {
        test: /\.feature$/,
        use: [
          {
            loader: 'cypress-cucumber-preprocessor/loader',
          },
        ],
      },
    ],
  },
};

webpack index.js

const webpack = require('@cypress/webpack-preprocessor');
const webpackOptions = require('./webpack.config');

module.exports = function webpackPreprocessor(on) {
  on('file:preprocessor', webpack({
    webpackOptions,
  }));
};

Instrumentation is setup. window.coverage works Screenshot 2021-02-26 at 15 27 49

Link to the repo Not a public repo.

danrocha commented 3 years ago

Check this: https://github.com/cypress-io/code-coverage/issues/331

srilakshmishankar commented 3 years ago

I tried the solution mentioned there i.e return _.merge({}, webpack, envConfig, codeCoverage); and I still have the same issue.

In this case when I see in cypress configuration on the browser there is codeCoverageTasksRegistered:"true" along with my other environment variables. So I don't see why it is not working.

Also weirdly if I remove envConfig then i see coverage working but obviously I cannot run tests without env variables.

This is how the plugin/index.js is. Is there anything wrong with the below code?

const _ = require('lodash');
const env = require('/e2e-core/plugins/setEnv');
const corePlugin = require('/e2e-core/plugins');

module.exports = function plugins(on, config) {
const webpack = corePlugin(on, config);
const codeCoverage = require('@cypress/code-coverage/task')(on, config);
const envConfig = env(on, require('dotenv').config({ path: `${process.cwd()}/cypress/.env` }));
return _.merge({}, webpack, envConfig, codeCoverage);
};

codeCoverageTasksRegistered:"true" when this clearly present in env variables in the configuration I should not get Code coverage tasks not registered by the plugins file this error right?

danrocha commented 3 years ago

Hmmm this is how my plugins.js is looking like and working well:

module.exports = async (on, config) => {
  require('@cypress/code-coverage/task')(on, config)

  const file = config.env.configFile || 'dev'
  const envConfig = await getConfigurationByFile(file)

  const allConfig = merge({}, config, envConfig)
  return allConfig
}

This is my getConfigurationByFile function, for reference:

async function getConfigurationByFile(file) {
    const pathToConfigFile = path.resolve('cypress/config', `cypress.${file}.json`)
    return await fs.readJson(pathToConfigFile)
  }

hope it helps!

srilakshmishankar commented 3 years ago

I tried many things still have no idea.

cy.log(`env:${Cypress.env('codeCoverageTasksRegistered')}`);.  --> this is true but still not working

I think it is related to the way we are env variables

const envConfig = env(on, require('dotenv').config({ path: `${process.cwd()}/cypress/.env` }));

in env its like below

module.exports = (on, config) => {
  config.env = process.env;
  return config;
};

we have a lot of variables in .env. Is there any example with require('dotenv').config()? If I comment the env variable part I see code coverage starts working.

srilakshmishankar commented 3 years ago
Screenshot 2021-03-03 at 14 04 27

Not understanding how is this possible.

MeanBoyCousin commented 3 years ago

Did anyone manage to make any headway here? Having the exact same issues. 🙏

rdhelms commented 3 years ago

In our case, the problem was that we were doing config.env = process.env, which I suppose must have been overwriting some value that @cypress/code-coverage was setting. One way to fix it was to make sure that any existing config.env values were still kept:

    require('@cypress/code-coverage/task')(on, config)

    config.env = {
        ...process.env,
        ...config.env,
    }
MeanBoyCousin commented 3 years ago

This is exactly what I tried but it still tells me that the tasks weren't registered. If I log out the config, I can clearly see that codeCoverageTasksRegistered: true so I can't even begin to fathom why this isn't working.

MoKhajavi75 commented 3 years ago

Any updates on this?

srankmeng commented 2 years ago

add { "env": { "codeCoverageTasksRegistered": true } } in env json file for example config/dev.json

jourdanrodrigues commented 2 years ago

In my case, Cypress version 10.3.1, the setupNodeEvents runs AFTER the supportFile, so it indeed there's a problem here.

I could testify this by putting a throw new Error in the beginning of each one and running to see what breaks first.

quisido commented 2 years ago

+1 to @jourdanrodrigues

I was debugging this for ages and ended up forking this repo and throwing errors every-which-way to find out why it wasn't working. I eventually found that the tasks weren't registered and "to fix this in config's setupNodeEvents."

I made the following changes to fix this:

module.exports = {
  e2e: {
    setupNodeEvents(on, config) {
      require('@cypress/code-coverage/task')(on, config);
      return config;
    },
  },
  env: {
    codeCoverageTasksRegistered: true,
  },
};
valmirjr66 commented 1 year ago

+1 to @jourdanrodrigues

I was debugging this for ages and ended up forking this repo and throwing errors every-which-way to find out why it wasn't working. I eventually found that the tasks weren't registered and "to fix this in config's setupNodeEvents."

I made the following changes to fix this:

  • Renamed cypress.config.mjs to cypress.config.cjs (ESM to CJS).
  • Deleted the plugin from cypress/plugins/e2e.ts.
  • Added the following to cypress.config.cjs:
module.exports = {
  e2e: {
    setupNodeEvents(on, config) {
      require('@cypress/code-coverage/task')(on, config);
      return config;
    },
  },
  env: {
    codeCoverageTasksRegistered: true,
  },
};

Thanks! I don't know why exactly, but this worked nicely for me. It seems that setupNodeEvents is overwriting the plugins configuration, which I suppose is an unexpected behavior.

VivianZenika commented 3 months ago

You should have a loohk on this official configuration file.

It helped me a lot :wink:

https://github.com/cypress-io/cypress-realworld-app/blob/develop/cypress.config.ts#L89