Skip to content
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

Error using cy.task(): cy.task() must only be invoked from the spec file or support file #27784

Open
2 of 6 tasks
AndreanaCasilli opened this issue Sep 7, 2023 · 21 comments
Open
2 of 6 tasks
Labels
topic: cy.origin Problems or enhancements related to cy.origin command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: bug

Comments

@AndreanaCasilli
Copy link

AndreanaCasilli commented Sep 7, 2023

Description

When I updated the cypress version from 12.14 to 13.1 I get this error "Error: "cy.task() must only be invoked from the spec file or support file" when I try to use a command that contains the cy.task. Attached a basic error reproduction example

URL of Issue(s)

https://github.com/AndreanaCasilli/task-origin-error

Steps to replicate

within the repository the reproduction of the error

Browser

No response

Device

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

Additional Information

No response

@nagash77 nagash77 transferred this issue from cypress-io/cypress-documentation Sep 11, 2023
@chrisbreiding
Copy link
Contributor

Sorry to see you've encountered this issue and thank you for providing a simple reproduction. It appears to be a bug. We'll look into fixing it. In the meantime, you could work around it by calling cy.task() directly inside your spec file instead of in a custom command defined in a different file.

cy.origin('https://google.com',()=>{
  cy.task('showError')
})

I know it's less than ideal, especially since your actual test code is likely more complex, but it should get your tests passing while we work on getting out a fix.

@chrisbreiding chrisbreiding added topic: cy.origin Problems or enhancements related to cy.origin command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. labels Sep 11, 2023
@chrisbreiding chrisbreiding self-assigned this Sep 11, 2023
@AndreanaCasilli
Copy link
Author

First of all thanks for taking charge of this problem I hope you can find a solution because I really need it to work even with an updated version of cypress. And yes, I confirm that the case in which I use it is much more complex than the example provided. Hoping to have good news soon, I wish you the best of luck in finding a solution.

@StephaneColson
Copy link

Hi, I get the same error when using cy.task in a cy.origin with Cypress version 13.2.0 (no issue with previously older version used: 12.8.1).

Screenshot 2023-09-20 at 08 13 31

I saw previous fixes around this (#27200), but apparently it's not enough. Will that be taken into account quickly (I really need to use latest version for Cypress Cloud and Test replay new awesome feature)?
Thanks

@StephaneColson
Copy link

Same if using cy.writeFile(as a workaround) instead of cy.task...

Screenshot 2023-09-20 at 10 10 57

Too bad. Is there a kwown workaround @chrisbreiding? I just want to pass a string outside of the cy.origin

@StephaneColson
Copy link

Any news about this? (I'm now using Cypress Cloud and I'm looking forward to have Test Replay with latest version 13) 🙏

@chrisbreiding
Copy link
Contributor

@StephaneColson I plan to look into fixing this in the next week or two. I appreciate your patience and definitely want you to be able to take advantage of Test Replay!

@StephaneColson
Copy link

Awesome. Thx 👍

@AndreanaCasilli
Copy link
Author

I hope is the next 👍

@jennifer-shehane jennifer-shehane changed the title Error using cy.task() Error using cy.task(): cy.task() must only be invoked from the spec file or support file Oct 10, 2023
@AndreanaCasilli
Copy link
Author

Was it closed because a solution was found?

@trias-at-alloy
Copy link

We've seen this in our e2e tests and it prevents us from upgrading to cypress 13. The problem seems to stem from (at least in our case) missing stack traces on the error objects generated in privileged-channel.js. The only explanation i have is that the stack property may not reliably initialized on chrome because of an optimization. There is Error.captureStackTrace on chrome browsers which may be more reliable but i didn't test this.

If this doesn't work i would welcome a config option to disable this security measure.

@StephaneColson
Copy link

Hi @chrisbreiding any news about this issue please ?

Thanks

@dillon-miller2-carvana
Copy link

Any updates on this? :)

@chrisbreiding
Copy link
Contributor

@StephaneColson, @dillon-miller2-carvana Unfortunately I haven't had the time to look into this yet, but it's still a priority to fix it in the near future. I'll be sure to post any updates when there's progress.

@bilel2015
Copy link

Hi, I encounter the identical error when utilizing cy.task within a Cypress origin in version 13.2.0, no problems observed with the previously version

@firepro20
Copy link

We had this issue using Cypress 13.5.0. We were calling it from the before scope. We replaced the before with a standard it and placed it at the beginning of the file and it worked.

@enrico-calzavara
Copy link

Same problem here on 13.5.1

@StephaneColson
Copy link

@StephaneColson I plan to look into fixing this in the next week or two. I appreciate your patience and definitely want you to be able to take advantage of Test Replay!

Sorry to push this again...but when do you plan to fix this? 🙏 If not planned soon, I should probably have to work on a workaround... 😞

@chrisbreiding
Copy link
Contributor

Sorry to push this again...but when do you plan to fix this? 🙏 If not planned soon, I should probably have to work on a workaround... 😞

I did have some time to look into this, but unfortunately there doesn't seem to be a straightforward solution, so I don't think we'll have a fix out for it any time soon.

@StephaneColson
Copy link

😭 thanks for the update. I will try to find a workaround (not using cy.task() in a origin)

@AndreanaCasilli
Copy link
Author

Sorry to push this again...but when do you plan to fix this? 🙏 If not planned soon, I should probably have to work on a workaround... 😞

I did have some time to look into this, but unfortunately there doesn't seem to be a straightforward solution, so I don't think we'll have a fix out for it any time soon.

May I know if you will look for a solution or will this bug be there forever?

@StephaneColson
Copy link

If it can help, I now use a workaround with cy.task outside of the cy.origin

Previously:

cy.origin('https://url/', () => {
    cy.get(mySelector).find('a').then(($a) => {
        const href: string = $a.prop('href');
        cy.task('setItem', { name: 'reference', value: href });
    });
})

and now with the "workaround":

cy.origin('https://url/', () => {
    cy.get(mySelector).find('a').invoke('attr', 'href')
    }).then(($loc) => {
        cy.task('setItem', { name: 'reference', value: $loc });
    })
})

See https://docs.cypress.io/api/commands/origin#Yielding-a-value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: cy.origin Problems or enhancements related to cy.origin command Triaged Issue has been routed to backlog. This is not a commitment to have it prioritized by the team. type: bug
Projects
None yet
Development

No branches or pull requests

8 participants