Cypress: `Error: The handler for the event `task` must be an object` after upgrade to 4.2.0

Created on 26 Mar 2020  路  5Comments  路  Source: cypress-io/cypress

Current behavior:

cypress run fails with following error after Cypress upgrade 4.1.0 -> 4.2.0

The following validation error was thrown by your plugins file (`/Users/andrejs/.../cypress/plugins/index.js`).

 Error: The handler for the event `task` must be an object
    at createErrorResult (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/validate_event.js:3:71)
    at validate (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/validate_event.js:7:46)
    at isObject (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/validate_event.js:15:10)
    at validateEvent (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/validate_event.js:42:10)
    at register (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:35:32)
    at module.exports (/Users/andrejs/Development/4finance/consent-management-service/frontend/cypress/plugins/index.js:16:3)
    at /Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:75:12
    at tryCatcher (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:23)
    at Function.Promise.attempt.Promise.try (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/bluebird/js/release/method.js:39:29)
    at load (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:72:7)
    at EventEmitter.<anonymous> (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_plugins.js:196:5)
    at EventEmitter.emit (events.js:210:5)
    at process.<anonymous> (/Users/andrejs/Library/Caches/Cypress/4.2.0/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:25:29)
    at process.emit (events.js:210:5)
    at emit (internal/child_process.js:876:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
error Command failed with exit code 1.

Desired behavior:

No failures or at least mention in release notes about breaking changes and how to update code to match changes implemetation.

Test code to reproduce

cypress/plugins/index.js

module.exports = (on, config) => {
  on('task', require('@cypress/code-coverage/task'));
  on('task', require('cypress-plugin-retries/lib/plugin'));
  on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'));

  return config;
};

Versions

Cypress 4.2.0, MacOS 10.15.3

Most helpful comment

I will update the code coverage documentation page, it is out of date with the actual plugin's code. See update-to-date instructions in https://github.com/cypress-io/code-coverage

module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config)
  // IMPORTANT to return the config object
  // with the any changed environment variables
  return config
}

All 5 comments

I do see this error in 4.2.0 and not happening in 4.1.0. This error is legitimate and was not being properly caught in 4.1.0 before. This is not a breaking change, but a bug fix. https://github.com/cypress-io/cypress/issues/6570

plugins/index.js

module.exports = (on) => {
  on('task', require('cypress-plugin-retries/lib/plugin'))
}

@tlpbu I'm not sure why you are requiring this on task. This is not the documented way for using cypress-plugin-retries nor the documented way to use task https://on.cypress.io/task

The cypress-plugin-retries/lib/plugin returns a function shown below, so this code just will not functionally work. The error is giving the proper error: Error: The handler for the eventtaskmust be an object - the task handler must be an Object, not a function.

function (on) {
  on('task', {
    logRetry ([title, currentRetry, nestedDepth]) {
      // eslint-disable-next-line no-console
      console.log(`${chalk.yellow(`${'  '.repeat(nestedDepth)}(retry ${currentRetry})`)} ${chalk.dim(title)}`)
    },
  })
}

i got the same error folowing https://docs.cypress.io/guides/tooling/code-coverage.html#Install-the-plugin

Error: The handler for the event task must be an object

module.exports = (on, config) => {
    on('task', require('@cypress/code-coverage/task'));
    on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'));
    on('file:preprocessor', cucumber());

    on('task', {
        readFileMaybe(filename) {
            if (fs.existsSync(filename)) {
                return fs.readFileSync(filename, 'utf8')
            }

            return null
        }
    });
    addMatchImageSnapshotPlugin(on, config);
    initPlugin(on, config);
    return config;
};

oh i had "cypress": "4.1.0", now im on "cypress": "4.4.1",
Worked fine on 4.1.0

Hello,
Im having this issue as well
I'm using:

  "cypress": "4.5.0"
  "@cypress/code-coverage": "3.7.2"

It's is failed on the single line -

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

I will update the code coverage documentation page, it is out of date with the actual plugin's code. See update-to-date instructions in https://github.com/cypress-io/code-coverage

module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config)
  // IMPORTANT to return the config object
  // with the any changed environment variables
  return config
}
Was this page helpful?
0 / 5 - 0 ratings