vue-jest: Coverage report incorrect

I’ve been trying to get jest to work properly with a Vue 2 + Vite project. Jest reports pass/fail as I would expect, but the coverage report seems incorrect. Link to the repro here: https://github.com/qkjosh/vue-jest-coverage

package.json

"devDependencies": {
    "@babel/core": "7.18.5",
    "@babel/preset-env": "^7.18.2",
    "@vue/test-utils": "^1.2.2",
    "@vue/vue2-jest": "^28.0.0",
    "babel-jest": "28.1.1",
    "jest": "28.1.1",
    "jest-environment-jsdom": "28.1.1",
    "vite": "2.9.12",
    "vite-plugin-vue2": "^2.0.1"
  }

Given an incomplete test suite, the coverage report indicates 100% coverage and seems to treat the whole SFC component as one statement. Do I have something completely misconfigured, or is the coverage report broken?

image

image

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 23
  • Comments: 37 (1 by maintainers)

Most upvoted comments

Is there any other workaround? Downgrading @babel/core and @babel/generator didn’t work for me, the coverage is still incorrect. Downgrading jest and @vue/vue2-jest to versions 28 solves it partly, it generates valid coverage, but some files are missing for some reason…

EDIT: Installing jest@28 and @vue/vue2-jest@28 along with adding coverageProvider: v8 to jest config did the trick for me.

Using yarn we were able to fix this by locking both versions of @babel/core and @babel/generator to 7.17.9 (thanks to the suggestion from @udebella )

"devDependencies": {
  "@babel/core": "7.17.9"
},
"resolutions": {
  "@babel/generator": "7.17.9"
},

We only use babel for jest so this doesn’t have any other side effects for us, YMMV.

Why Im I seeing this error message TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions') It makes no sense, because I installed jest-environment-jsdom

image

This issue is not relevant to this topic at all !!!

This solved my issue https://github.com/vuejs/vue-jest/issues/522, probably worth trying here as well?

Upgrading couple of packages and editing jest.config.ts fixed the issue with coverage.

package.json

"@types/jest": "^29.2.2",
"jest": "^29.3.1",
"jest-environment-jsdom": "29.3.1",
"ts-jest": "^29.0.3",

jest.config.ts

moduleNameMapper: {
  uuid: require.resolve("uuid"),
},
coverageProvider: "v8",
"overrides": {
  "@babel/core": "7.17.9",
  "@babel/generator": "7.17.9",
  "istanbul-lib-instrument": "5.2.0"
}

works on vue3 too. thx @GrRivero @rklos

My coworker noticed this issue still persists with "@vue/vue3-jest": "^29.2.6". Our team was able to fix the odd coverage behavior by adding "coverageProvider": "v8" to our app’s Jest config file

Using yarn we were able to fix this by locking both versions of @babel/core and @babel/generator to 7.17.9 (thanks to the suggestion from @udebella )

"devDependencies": {
  "@babel/core": "7.17.9"
},
"resolutions": {
  "@babel/generator": "7.17.9"
},

We only use babel for jest so this doesn’t have any other side effects for us, YMMV.

This fix has stopped working quite time ago. With step by step method I have found that the recent update of the istanbul-lib-instrument had broken something (there is an issue: https://github.com/istanbuljs/istanbuljs/issues/707).

So the next iteration of the workaround is (for npm >= 8):

"overrides": {
  "@babel/core": "7.17.9",
  "@babel/generator": "7.17.9",
  "istanbul-lib-instrument": "5.2.0"
}

My coworker noticed this issue still persists with "@vue/vue3-jest": "^29.2.6". Our team was able to fix the odd coverage behavior by adding "coverageProvider": "v8" to our app’s Jest config file

Yea, v8 works when it comes to coverage, but it is a huge tradeoff in favor of test speed and coverage report accuracy… would be really great to fix babel provider.

@Envoy49 This topic talks about fixes for coverage reports. After following one of many fixes from this topic, the coverage reports issue was gone. I updgraded Jest from v27 to v28 || v29, and this error TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions') started. If it happened to me, it will surelly happen to other people. I had to rollback Jest to v27 because installing jest-environment-json did not fix the issue, as explained in jest’s official migration doc.

Why Im I seeing this error message TypeError: Cannot read properties of undefined (reading 'testEnvironmentOptions') It makes no sense, because I installed jest-environment-jsdom

image

update jest and @vue/vue2-jest to version 28.1.0 also did the trick for me image

Waiting for the fix, in the package.json:

  • Add this into scripts section: “preinstall”: “npx npm-force-resolutions”
  • Add this section to force babel generator version: “resolutions”: { “@babel/generator”: “7.17.9” }, On my side it worked and it will be fine while waiting for the version upgrade with the fixes

Any update on it?

I’m also having this issue. Is the above PR being considered for a resolution here?

I’ve also been investigating this, in our case we do not use vite, but it seems to be related to babel 7 switching from source-map to @jridgewell/gen-mapping here: https://github.com/babel/babel/blob/main/CHANGELOG.md#v71710-2022-04-29

I have downgraded my @babel/core to 7.17.9 just before switched to @jridgewell/gen-mapping on 7.17.10 but it seems no luck

Anyone with a proposed solution much appreciated

Downgrading to 7.17.9 does not work because in @babel/core they did not freeze their dependencies. If you have a project that work, just make sure you don’t upgrade @babel/core dependencies: removing and recreating your package-lock.json leads to issue. 😦

My really ugly workaround is to to prevent @babel/generator to update to a buggy version. So I added inside my package.json:

  "overrides": {
    "@babel/generator": "<7.17.9"
  }

Then doing an npm install fixes broken package-lock.json. Be careful with this fix, because it forces a version of one dependency inside @babel/core which seems to rely on consistent versions : this workaround could not work or break your project: don’t forget to remove the override part from your package.json when this issue is fixed.

Maybe knowing where the issue comes from more precisely will help maintainers?

Edit: This solution works only for npm > 8.3.0 as overrides were introduced in this version. If you still use an older version, you can use @AnthonyRuelle solution If you use yarn, you can use @BenShelton solution

I’ve also been investigating this, in our case we do not use vite, but it seems to be related to babel 7 switching from source-map to @jridgewell/gen-mapping here:

https://github.com/babel/babel/blob/main/CHANGELOG.md#v71710-2022-04-29

I have downgraded my @babel/core to 7.17.9 just before switched to @jridgewell/gen-mapping on 7.17.10 but it seems no luck

Anyone with a proposed solution much appreciated