cypress-io / code-coverage

Saves the code coverage collected during Cypress tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

CamilleDrapier opened this issue · comments

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.

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

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

commented

We are also facing the same problem.

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

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

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

On 12.14.0 works like a charm.

Same for me on 12.15.0 except with exec command:
image

commented

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.

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.

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!

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?

I still have the issue too with cy.task

image

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.

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

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

commented

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

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

@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.