cypress: Error: The 'task' event has not been registered in the plugins file. You must register it before using cy.task()

Current behavior:

I copied steps from https://github.com/cypress-io/cypress/issues/2284 with slight modification.

in my spec:

cy.task('seedRisks',{"model": "delete"})

in index.js:

module.exports = (on) => {
  on('task:requested', (event, arg) => {
    switch (event) {
      case 'seedRisks': {
        console.log('sdjdsj'); //just a placeholder
      }
      }
  })
}

When I run my tests, getting error:

CypressError: cy.task('seedRisks') failed with the following error:

The 'task' event has not been registered in the plugins file. You must register it before using cy.task()

Fix this in your plugins file here:
C:\Users\Maxim.Karpov\cypress\plugins\index.js

https://on.cypress.io/api/task

Desired behavior:

want to be able to proceed without an error

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (4 by maintainers)

Most upvoted comments

@J-QA Check your cypress.json for "pluginsFile": false. That keeps the plugins file from being loaded. That’s why the error messaging says:

Fix this in your plugins file here:
false

false is where the path to the plugins file would be displayed if it was loaded.

Remove "pluginsFile": false from cypress.json and cy.task() should work.

Looks like we could improve the error message for this case and make it clear that cy.task() won’t work because the plugins file has been disabled.

What was the issue?

I am facing same issue! total crap!

@J-QA Check your cypress.json for "pluginsFile": false. That keeps the plugins file from being loaded. That’s why the error messaging says:

Fix this in your plugins file here:
false

false is where the path to the plugins file would be displayed if it was loaded.

Remove "pluginsFile": false from cypress.json and cy.task() should work.

Looks like we could improve the error message for this case and make it clear that cy.task() won’t work because the plugins file has been disabled.

It worked like a charm, thank you @chrisbreiding for the quick response!

If you’re facing this issue:

  • Double check your code in the plugins file, it should match the task signature here: https://on.cypress.io/task#Usage
  • Double check that your plugins file is building and compiling correctly.

Also, check out our community chat, it can be helpful for debugging or answering questions on how to use Cypress.

I had a similar issue, I solved it by specifying my file in the cypress.json like so: { "pluginsFile": "./cypress/plugins/index.cjs" }

I think by default cypress looks for a .js file, and not .cjs or .ts.

I get the same error with cypress 8.2.0. My cypres.json does not have pluginsFile: false also. Not sure where I am making a mistake:

{
  "projectId": "myproject",
  "integrationFolder": "cypress/e2e",
  "supportFile": "cypress/support/index.js",
  "retries": {
    "runMode": 1,
    "openMode": 0
  },
  "reporter": "junit",
  "reporterOptions": {
    "mochaFile": "results/my-test-output-[hash].xml",
    "toConsole": true
  },
  "reporter": "mochawesome",
  "reporterOptions": {
    "reportDir": "cypress/results",
    "overwrite": false,
    "html": false,
    "json": true
  },
  "screenshotsFolder": "mochawesome-report/assets",
  "defaultCommandTimeout": 7000
}

Screen Shot 2021-08-18 at 4 25 11 PM

I was getting this issue as well when creating tasks for the first time in my project. It seems like restarting the cypress instance is needed after making some changes in the plugin file (node process side).