jest icon indicating copy to clipboard operation
jest copied to clipboard

Missing coverage information in random lines

Open MoritzOtto opened this issue 3 years ago • 11 comments

🐛 Bug Report

Since update jest to version 27 (27.0.6) we have the problem that on random lines no coverage information are collected. Our project is using Angular. So maybe thats an problem in that combination. I made a minimal angular project with that behaviour. When I run all tests with jest --coverage I got this result: https://biga-software.de/assets/dummy.page.ts.html The method cardMessageText is tested but according to the report there is no coverage availible.

To Reproduce

Don't know just took our project and remove sources.

Expected behavior

Having correctly coverage information. Method is tested.

Link to repl or repo (highly encouraged)

https://github.com/MoritzOtto/jest_coverage_bug

envinfo

System: OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa) CPU: (8) x64 Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz Binaries: Node: 15.14.0 - ~/.nvm/versions/node/v15.14.0/bin/node npm: 7.7.6 - ~/.nvm/versions/node/v15.14.0/bin/npm

MoritzOtto avatar Jul 07 '21 10:07 MoritzOtto

Hitting this in react, in my case, somehow:

	if (company.logoUrl == null || company.logoUrl == "" || imageLoadFailed) {
-		return <BusinessName variant="body1">{company?.name || ""}</BusinessName>;
-	}
-
-	return (
-		<BusinessImage
-			onError={() => setImageLoadFailed(true)}
-			src={company.logoUrl}
-			alt={company.name || ""}
			height={40}
	  />
	);

lines marked with - are not covered. Running tests one by one produces result tables that indicate that those lines are covered, but running all tests mark those lines as not covered.

Jest: 27.3.0

MonstraG avatar Oct 18 '21 14:10 MonstraG

Here's a minimal reproduction: https://github.com/CharlesStover/monorepo-template/runs/4734914005?check_suite_focus=true 100% coverage in Jest 26. By simply upgrading to Jest 27, you get uncovered empty lines in a file that only exports.

quisido avatar Jan 07 '22 03:01 quisido

@CharlesStover It could be that in your case ts-jest is the issue. Here is what I get:

  • using jest 27 and babel Screenshot 2022-01-07 at 09 43 57

  • using jest 27 and ts-jest 27 Screenshot 2022-01-07 at 09 44 56

mrazauskas avatar Jan 07 '22 07:01 mrazauskas

@mrazauskas Nice debugging. What configuration did you use for Babel to get those results?

quisido avatar Jan 07 '22 17:01 quisido

What configuration did you use for Babel to get those results?

Here is the whole babel.config.json:

{
  "presets": [
    ["@babel/preset-env", { "targets": { "node": "current" } }],
    ["@babel/preset-react", { "runtime": "automatic" }],
    "@babel/preset-typescript"
  ]
}

To be honest, I have no experience with React. The line with @babel/preset-react is just a guess.

mrazauskas avatar Jan 07 '22 19:01 mrazauskas

@CharlesStover Found it. Including "sourceMap": true in tsconfig.json makes the jest v27 and ts-jest v27 duo work just like you would expect. Seems like ts-jest changed its defaults: https://github.com/kulshekhar/ts-jest/blob/main/CHANGELOG.md#breaking-changes

mrazauskas avatar Jan 08 '22 08:01 mrazauskas

using jest 27 and babel

@mrazauskas I am also having the same issue where index.ts is coming up 0. do you know of a config that would resolve that? also, does it effect reporting overall test coverage?

actually, even with babel, i'm seeing something similar to your ts-jest example as well image

shamseen avatar Jul 25 '22 17:07 shamseen

The same issue. Reproducible repo here - just clone & run npm i && npm test

image

In my case coverage for single file works correct. When files were 5...6 - it was ok. But now when files a lot it's wrong and different time to time (just try to run npm test several times and compare)

Yegorich555 avatar Jul 29 '22 15:07 Yegorich555

Facing this same problem, but with Jest 26.6.3 in a Node server

ftoyoshima avatar Feb 10 '23 22:02 ftoyoshima

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 10 '24 23:02 github-actions[bot]

Ping

Yegorich555 avatar Feb 12 '24 07:02 Yegorich555