code-coverage icon indicating copy to clipboard operation
code-coverage copied to clipboard

Error: Code coverage tasks not registered by the plugins file

Open srilakshmishankar opened this issue 4 years ago • 14 comments

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

  • Is there .nyc_output folder? Is there .nyc_output/out.json file. Is it empty? Can you paste at least part of it so we can see the keys and file paths? no folder created Screenshot 2021-02-26 at 16 58 21

  • Do you have any custom NYC settings in package.json (nyc object) or in other NYC config files no

  • Do you run Cypress tests in a Docker container? not in this case

Link to the repo Not a public repo.

srilakshmishankar avatar Feb 26 '21 15:02 srilakshmishankar

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

danrocha avatar Mar 02 '21 10:03 danrocha

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?

srilakshmishankar avatar Mar 02 '21 16:03 srilakshmishankar

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!

danrocha avatar Mar 03 '21 10:03 danrocha

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 avatar Mar 03 '21 12:03 srilakshmishankar

Screenshot 2021-03-03 at 14 04 27

Not understanding how is this possible.

srilakshmishankar avatar Mar 03 '21 13:03 srilakshmishankar

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

MeanBoyCousin avatar Jul 19 '21 15:07 MeanBoyCousin

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,
    }

rdhelms avatar Jul 19 '21 20:07 rdhelms

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.

MeanBoyCousin avatar Jul 20 '21 07:07 MeanBoyCousin

Any updates on this?

MoKhajavi75 avatar Sep 22 '21 09:09 MoKhajavi75

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

srankmeng avatar Mar 03 '22 19:03 srankmeng

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.

jourdanrodrigues avatar Jul 31 '22 17:07 jourdanrodrigues

+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,
  },
};

quisido avatar Sep 12 '22 06:09 quisido

+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.

valmirjr66 avatar Dec 14 '22 04:12 valmirjr66