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

nyc out.json is empty #78

Closed
nweiser1 opened this issue Sep 26, 2019 · 22 comments
Closed

nyc out.json is empty #78

nweiser1 opened this issue Sep 26, 2019 · 22 comments

Comments

@nweiser1
Copy link

nweiser1 commented Sep 26, 2019

vue/node js project running with cypress tests, trying to add coverage
the nyc_output folder is there also the processinto folder
The out.json file is either empty or not created, depending on how i play with the setting files
I also see in the cypress run that the after all task - covergereport runs, but the after each task - combine coverge does not run

my plugins index file has the

on('task', require('@cypress/code-coverage/task'))

my babel

"babel": {
    "plugins": ["istanbul"],
    "presets": [
      [
        "@babel/preset-env",
        {
          "modules": "commonjs",
          "targets": {
            "node": "current"
          }
        }
      ]
    ]

nycrc
{
        "extends": "@istanbuljs/nyc-config-babel",
        "extension": [".js", ".vue"],
        "all": true
}

my command line

nyc --all npm run test:open

my json has

"devDependencies": {
    "@babel/core": "^7.5.0",
    "@babel/preset-env": "^7.5.2",
    "@cypress/code-coverage": "^1.10.1",
    "@vue/cli-plugin-babel": "^3.8.0",
    "@vue/cli-plugin-eslint": "^3.8.0",
    "@vue/cli-plugin-unit-jest": "^3.7.0",
    "@vue/cli-service": "^3.8.0",
    "@vue/eslint-config-standard": "^4.0.0",
    "@vue/test-utils": "^1.0.0-beta.29",
    "axios-mock-adapter": "^1.17.0",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^22.4.3",
    "babel-plugin-component": "^1.1.1",
    "babel-plugin-istanbul": "^5.2.0",
    "cypress": "^3.4.1",
    "cypress-failed-log": "^2.5.1",
    "eslint": "^5.16.0",
    "eslint-plugin-cypress": "^2.0.1",
    "eslint-plugin-cypress-dev": "2.1.0",
    "eslint-plugin-jest": "^22.7.2",
    "eslint-plugin-mocha": "5.3.0",
    "eslint-plugin-vue": "^5.0.0",
    "gulp-babel": "^8.0.0",
    "istanbul-lib-coverage": "^2.0.5",
    "jest": "^24.8.0",
    "jest-html-reporter": "^2.5.0",
    "jest-junit": "^6.4.0",
    "jest-transform-stub": "^2.0.0",
    "nodemon": "^1.19.1",
    "nyc": "^14.1.1",
    "sass": "^1.18.0",
    "sass-loader": "^7.1.0",
    "start-server-and-test": "^1.10.0",
    "stylus": "^0.54.5",
    "stylus-loader": "^3.0.1",
    "supertest": "^4.0.2",
    "supertest-as-promised": "^4.0.2",
    "vue-cli-plugin-vuetify": "^0.5.0",
    "vue-jest": "^3.0.4",
    "vue-template-compiler": "^2.6.10",
    "vue-test-utils": "^1.0.0-beta.11",
    "vuetify-loader": "^1.0.5",
    "vuex-mock-store": "0.0.7",
    "wait-on": "^3.3.0"

what am i missing?

@leorigon
Copy link

leorigon commented Oct 1, 2019

We have the same problem here :(

@stanleylu3216
Copy link

same here.

@3590Duncan
Copy link

Also experiencing this issue

@albertodeago
Copy link

same here

@peterbabic
Copy link

Mine is also empty, followed the guide https://docs.cypress.io/guides/tooling/code-coverage.htm step by step until fullstack.

@ronaiza-cardoso
Copy link

same here

@leorigon
Copy link

leorigon commented Nov 13, 2019

I fixed this issue on our project (we using TypeScript) re-installing the dependencies and putting these codes:

package.json

"scripts": {
   "cy:run:report": "nyc --reporter=html cypress run"
},
"devDependencies": {
    "@istanbuljs/nyc-config-typescript": "0.1.3",
    "babel-plugin-istanbul": "5.2.0",
    "source-map-support": "0.5.16",
    "@cypress/code-coverage": "1.10.1",
    "istanbul-lib-coverage": "2.0.5",
    "nyc": "14.1.1"
},
"nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "extension": [
      ".tsx"
    ],
    "include": [
      "src/**/*.tsx"
    ],
    "exclude": [
      "cypress/",
      "assets/",
      "test/"
    ],
    "all": true
  }

.babelrc

"plugins": ["istanbul"]

added in plugins/index.js module.exports:

on('task', require('@cypress/code-coverage/task'))

and support/index.ts

import '@cypress/code-coverage/support'

It's works for now, but I can improve these.

@bahmutov
Copy link
Contributor

Can I please have a reproducible repo with the problem? Otherwise I have no idea what the possible problems could be

@elaco77
Copy link

elaco77 commented Nov 21, 2019

@bahmutov here's a repo where you can reproduce the issue. However I run the test, the nyc/out.json remains empty.

Feel free to contribute on the repo if you feel so. Also, let me know if there's anything else I can do.

https://github.com/elaco77/cypress-code-coverage-showcase

@albertodeago
Copy link

albertodeago commented Nov 22, 2019

Oh god, in my case I missed a babel configuration thing... I forgot to add

"plugins": ["transform-class-properties", "istanbul"] 

to my babel config

@elaco77
Copy link

elaco77 commented Dec 12, 2019

Dear Cypress team, any update on this one?

@Zaid-Ajaj
Copy link

Same issue here

@harishkrishnan24
Copy link

Same issue here... +

@kbhutiya
Copy link

kbhutiya commented Jan 23, 2020

Same issue ! .nyc-output/out.json is always empty no matter what settings you keep and do! Tried everything there is on internet! Either its an issue with code-coverage or its the problem with nyc!Cant really figure whats causing this issue ! (Also we are not using babel)

@HeckCas
Copy link

HeckCas commented Jan 24, 2020

Me and my team are experiencing the same here
👀

We have tried but still did not find what is it for 😅 😬

@Ozymandias9000
Copy link

Same problem.

@bahmutov
Copy link
Contributor

bahmutov commented Feb 3, 2020

@elaco77 I have copied your example - you are not instrumenting your code, once you follow https://github.com/cypress-io/instrument-cra or https://github.com/bahmutov/testing-react#installation instructions your source code will be instrumented on the fly, and code coverage will be saved

Screen Shot 2020-02-03 at 2 49 37 PM

@bahmutov
Copy link
Contributor

bahmutov commented Feb 3, 2020

I think what got lost in README is that this plugin DOES NOT instrument your application's code - you have to do this. I added a strong note in https://github.com/cypress-io/code-coverage#instrument-your-application with examples. There are many examples linked from the readme https://github.com/cypress-io/code-coverage#examples to copy instrumentation, so I will close this issue for now. If you still are experiencing an empty coverage object, even if the window.__coverage__ has the info, please open a new issue with a link to the reproduction.

@bahmutov bahmutov closed this as completed Feb 3, 2020
@KarimElKhashab
Copy link

I'm facing a problem where the window.coverage is always coming "undefined" even after following all the Cypress code coverage documentation steps with various instrumentation options mentioned... and when starting my server with the cmd

  "scripts": {
    "start": "PORT=7777 react-app-rewired -r @cypress/instrument-cra start",
}

I get the following error

EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline'

Appreciate your help on this @bahmutov

@ngokevin
Copy link

ngokevin commented Jun 2, 2022

The issue for me was during production build mode, I had to force my plugin to do instrumentation (vite-plugin-istanbul + forceBuildInstrumentation option).

@giagara
Copy link

giagara commented Feb 1, 2024

The issue for me was during production build mode, I had to force my plugin to do instrumentation (vite-plugin-istanbul + forceBuildInstrumentation option).

how come? i am not able to use code coverage with vite

@mayursonawale2601
Copy link

mayursonawale2601 commented Mar 27, 2024

Thanks, @bahmutov for your valuable comment 🙌
After multiple trials and errors, I was finally able to generate a code coverage report.

First, I tried everything as per the cypress code coverage doc

Then, I installed the @cypress/instrument-cra package as @bahmutov mentioned in his comment.
npm install --save-dev @cypress/instrument-cra
and update my npm script as "start": "react-scripts -r @cypress/instrument-cra start",

I made a lot of changes to multiple files. I hope the following package, and file information will help you all to debug your issues.

cypress/support/commands.js
import '@cypress/code-coverage/support' // At the top of the file

cypress.config.js:

const { defineConfig } = require('cypress')`
module.exports = defineConfig({
 fixturesFolder: false,
 env: {
    codeCoverage: {
     url: 'http://localhost:3000/__coverage__',
    exclude: 'cypress/**/*.*'
   }
  },
  e2e: {
    setupNodeEvents(on, config) {
      require('@cypress/code-coverage/task')(on, config)
     return config;
    },
  },
})

.babelrc:

{
  "plugins": ["istanbul"]
}

you can also use

{
  "plugins": ["@babel/transform-class-properties", "istanbul"]
}

It should be @babel/transform-class-properties

package.json: package.json

I hope this solution will work for everyone! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests