cypress-io/cypress-documentation

Still remaining in firefox: cypresserror: `cy.task()` must only be invoked from the spec file or support file.

wyattpuckett opened this issue · 2 comments

Description

Same as cypress-io/cypress#27099, I am seeing this error when executing tasks within my commands.js file, but only in firefox. Currently using 13.2, but has happened since 12.15

URL of Issue(s)

NA

Steps to replicate

  1. Use cypress version 12.15 or higher
  2. Have cy.task used within your commands.js file
  3. Execute the test in firefox
  4. Notice this error: cypresserror: cy.task() must only be invoked from the spec file or support file.

Example code from commands.js:
cy.contains('Sign in').click()
cy.task("generateOTP", 'example').then(token => {
cy.get("element").type(token);
cy.get('element').click()
})

Browser

Firefox 118.0.2

Device

  • PC
  • Mac
  • iPhone
  • iPad
  • Android Phone
  • Android Tablet

Additional Information

No response

@wyattpuckett

The cy.task() > Usage example worked correctly for me on Cypress 13.2.0 and 13.3.0 running on Ubuntu 22.04 with Firefox 118

// in test
cy.task('log', 'This will be output to the terminal')

cypress.config.js

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  // setupNodeEvents can be defined in either
  // the e2e or component configuration
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        log(message) {
          console.log(message)

          return null
        },
      })
    },
  },
})

This is actually the issue list for documentation and as far as I can see, the documentation https://docs.cypress.io/api/commands/task is correct.

If you need to report a bug with Cypress itself, that would belong in the https://github.com/cypress-io/cypress/issues list.

Sounds good. My bad, didn't realize i had this in the documentation list
I'll dig in more to see what could be causing the issue, and if I find anything I'll get it over to the issues list
Thanks for the help