cypress-io / code-coverage

Saves the code coverage collected during Cypress tests
MIT License
432 stars 107 forks source link

Support should not send coverage unless plugins task has been registered #179

Closed bahmutov closed 4 years ago

bahmutov commented 4 years ago

in a situation where a support file has been registered BUT there is no task in the plugins file, we should not try to call the coverage report task

Example situation

{
  "pluginsFile": false,
  "supportFile": "node_modules/cypress-react-unit-test/support"
}
bahmutov commented 4 years ago
Screen Shot 2020-04-06 at 9 08 19 AM

Maybe we should use window.__coverage__ to know?

bahmutov commented 4 years ago

Maybe if plugins can set a variable when registering a task we could do

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

inside the task

module.exports = (on, config) => {
  on('task', tasks)
  config.env.codeCoverageTasksRegistered = true
  return config
}
bahmutov commented 4 years ago

:tada: This issue has been resolved in version 3.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

quisido commented 2 years ago

Is it possible for this to throw an error? I've been debugging for days why I'm not getting any coverage files in my CI workflow. Local tests didn't give me anything either, and this warning was simply not visible: image It was sheer happenstance that I ran a one-liner test on a local Cypress instance and saw this banner.

The end problem was that my plugins file's default export wasn't returning config after I mutated it with @cypress/code-coverage:

import task from '@cypress/code-coverage';
export default function(on, config) {
  task(on, config);
  return config; // <-- REQUIRED
}

This would have been so much easier to debug if CI had failed with an error that the code coverage task had not registered instead of failing silently.

taymoork2 commented 2 years ago

For those who run into the edge case of trying to add process.env to config.env (i.e. config.env = process.env) This comment from issue #405 helped resolved the issue of code coverage tasks not registering

https://github.com/cypress-io/code-coverage/issues/405#issuecomment-882820159

TLDR:

require('@cypress/code-coverage/task')(on, config)
config.env = {
  ...process.env,
  ...config.env, // Don't overwrite `codeCoverageTasksRegistered` set by `@cypress/code-coverage/task`
};
gwvt commented 2 years ago

How do you keep the coverage tasks registered when using multiple config files as documented at https://docs.cypress.io/api/plugins/configuration-api#Customize-available-browsers?

In plugins/index.js as documented there:

module.exports = (on, config) => {
  const environment = config.env.configFile || "development";
  const configurationForEnvironment = fetchConfigurationByFile(environment);

  return configurationForEnvironment || config;
};

How do you incorporate

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

to register the coverage tasks on the returned config object? Config by file works but without coverage. Coverage works without config by file (i.e., just returning config). I can't get both to work together...