Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
srilakshmishankar opened this issue Feb 26, 2021 · 14 comments
Open

Comments

@srilakshmishankar
Copy link

srilakshmishankar commented Feb 26, 2021

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.

@danrocha
Copy link

danrocha commented Mar 2, 2021

Check this: #331

@srilakshmishankar
Copy link
Author

srilakshmishankar commented Mar 2, 2021

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
Copy link

danrocha commented Mar 3, 2021

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
Copy link
Author

srilakshmishankar commented Mar 3, 2021

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
Copy link
Author

Screenshot 2021-03-03 at 14 04 27

Not understanding how is this possible.

@MeanBoyCousin
Copy link

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

@rdhelms
Copy link

rdhelms commented Jul 19, 2021

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
Copy link

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
Copy link

Any updates on this?

@srankmeng
Copy link

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

@jourdanrodrigues
Copy link

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
Copy link
Contributor

quisido commented Sep 12, 2022

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

@valmirjr66
Copy link

+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
Copy link

You should have a loohk on this official configuration file.

It helped me a lot 😉

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants