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: "cy.task() must only be invoked from the spec file or support file" #667

Closed
CamilleDrapier opened this issue Jun 21, 2023 · 21 comments

Comments

@CamilleDrapier
Copy link

Versions

  • What is this plugin's version? If this is NOT the latest released version can you try the latest version, please? 3.10.7

  • What is Cypress version? 12.15.0, the problem doesn't seem to happen with 12.14.0

  • What is your operating system? Linux Manjaro

  • What is the shell? zsh

  • What is the Node version? v18.16.0

  • What is the NPM version? 9.6.6

  • How do you instrument your application? istanbul

  • When running tests, if you open the web application in regular browser, and open DevTools, do you see window.__coverage__ object? Can you paste a screenshot?

    Screenshot_20230621_104538

  • Is there .nyc_output folder? Is there .nyc_output/out.json file. Is it empty? Can you paste at least part of it so we can see the keys and file paths?
    All entries seem to looks the same with the form:

     "/home/camille/xdev/xxx/src/components/somefolder/SomeComponent.vue": {
       "path": "/home/camille/xdev/xxx/src/components/somefolder/SomeComponent.vue",
       "statementMap": {},
       "fnMap": {},
       "branchMap": {},
       "s": {},
       "f": {},
       "b": {}
     },
    
  • Do you have any custom NYC settings in package.json (nyc object) or in other NYC config files

     {
       "extends": "@istanbuljs/nyc-config-typescript",
       "all": true,
       "include": [
         "src/**/*.js",
         "src/**/*.ts",
         "src/**/*.vue"
       ],
       "exclude": [
         "**/types/*.ts",
         "**/types.ts",
         "**/*.d.ts",
         "**/*.spec.js",
         "**/*.spec.ts",
         "**/fixtures/*",
         "src/boot/*"
       ],
       "sourceMap": false,
       "instrument": false,
       "extension": [".js", ".ts", ".vue"],
       "report-dir": "test/cypress/coverage",
       "temp-dir": "test/cypress/coverage-cypress-output",
       "reporter": ["json", "text-summary", "lcov", "cobertura"]
     }
    
  • Do you run Cypress tests in a Docker container? no

Describe the bug
When running any test on my computer, even if the test seems to conclude properly, I get a failure and the following log:

CypressError
cy.task() must only be invoked from the spec file or support file.

Because this error occurred during a after each hook we are skipping all of the remaining tests.
node_modules/@cypress/code-coverage/support.js:20:1
  18 | 
  19 |   // stringify coverage object for speed
> 20 |   cy.task('combineCoverage', JSON.stringify(totalCoverage), {
     | ^
  21 |     log: false
  22 |   })
  23 | }

This works fine with cypress version 12.14; is there some specific thing to change, I have not seen anything specific related to this in the cypress changelog

This seems to fail both with chrome and chrome headless mode.

Link to the repo
I'm not sure this is entirely necessary, but if it is let me know and I will try to provide something.

@samixchoumi
Copy link

samixchoumi commented Jun 21, 2023

I confirm, I got the same error when using cypress 12.15.0, it work using cypress 12.14.0.

@viktorgogulenko
Copy link

Just updated to 12.5.0 from 12.4.0 and coverage failed with the same error above.

@samixchoumi
Copy link

samixchoumi commented Jun 21, 2023 via email

@RMJana
Copy link

RMJana commented Jun 22, 2023

We are also facing the same problem.

@atmullan
Copy link

atmullan commented Jun 22, 2023

Also seeing this error in my tests, is there a change made to cy.task (don't see it documented) or is it regression introduced in 12.15.0 Also similar issue with cy.writeFile() must only be invoked from the spec file or support file

@samixchoumi
Copy link

samixchoumi commented Jun 22, 2023 via email

@wrengames
Copy link

wrengames commented Jun 22, 2023

Also suffering this problem in our project. Works on 12.14.0 but breaks on 12.15.0.

@Slawek-Zolnierczyk
Copy link

Same problem on my side. On 12.15.0 I got this:
image

On 12.14.0 works like a charm.

@aurelijusrozenas
Copy link

Same for me on 12.15.0 except with exec command:
image

@kaydoc
Copy link

kaydoc commented Jun 26, 2023

Same here, can confirm code coverage works with 12.14.0 but breaks with 12.15.0. Especially with a wired payload error message on the console output. Could first really encounter the issue when having cypress run in headed mode.

@rakeshnambiar
Copy link

I am also blocked with the version upgrade due to this error. Wondering, the Cypress team could prioritize this issue as we all could take the advantage from the latest version.

@mike-plummer
Copy link

Cypress v12.16.0 has just been released with a fix for the issue with cy.task, apologies for the oversight on that bug.

Thanks for using Cypress, everyone!

@juliaklimovsky
Copy link

Problem is that I still have this error for cy.exec() even in 12.16.0 version
image

Should I create a new issue for this?

@mariejoechahine
Copy link

mariejoechahine commented Jun 27, 2023

I still have the issue too with cy.task

image

@mike-plummer
Copy link

Hi everyone, sorry to hear folks are still seeing issues. We identified and fixed an issue that caused this sort of error in 12.16.0 but it appears there are some edge cases that still exist. This error doesn't seem to be specifically related to the @cypress/code-coverage plugin but rather an issue with core Cypress so I'm going to leave this issue closed. If you are able to provide a reproduction case I would ask you to link it in cypress-io/cypress#27097 or cypress-io/cypress#27099 (depending on the specific error you're able to reproduce) so we can troubleshoot, or open a new issue if you're seeing a different symptom.

@rakeshnambiar
Copy link

Perhaps the new version helped me. I was using Cy.task('log', 'my logs') and with version 12,15.0 it was failing there.

@anilpujaraofficial
Copy link

``
Screenshot from 2023-07-12 14-53-35
this version => cypress 12.17.1

@nagash77
Copy link
Contributor

@anilpujaraofficial can you share the test in which you are using cy.writeFile()?

@adrienharnay
Copy link

Hi @mike-plummer, I can confirm the issue persists with the latest release (13.5.0).

@mike-plummer
Copy link

@adrienharnay This issue has been closed for some time now. If you have an example of it still occurring with the latest version of Cypress please open a new issue with a reproducible example that we can use to diagnose your problem.

@adrienharnay
Copy link

Here you ho: cypress-io/cypress#28336

@cypress-io cypress-io locked and limited conversation to collaborators Nov 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests