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

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.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 11
  • Comments: 21 (1 by maintainers)

Most upvoted comments

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!

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.

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