jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.22k stars 6.46k forks source link

[Bug]: Untested code is not transformed when using v8 coverage collector #14422

Open spalger opened 1 year ago

spalger commented 1 year ago

Version

29.5.0

Steps to reproduce

Repro code is in a branch on my reproduce-issues repo: https://github.com/spalger/reproduce-issues/tree/jest/v8-coverage-untested-ts-files

  1. clone repro branch:

    git clone --branch jest/v8-coverage-untested-ts-files git@github.com:spalger/reproduce-issues.git
  2. Install packages and run tests

    npm i && npm run test

Expected behavior

types.ts only includes a single interface, so the file should should be ignored for the purpose of coverage calculation.

Actual behavior

  1. types.ts is not imported during test execution because TypeScript strips type-only imports by default
  2. when coverage is being calculated the source of the file is not transformed using the configured transformer
  3. types.ts is reported as missing coverage on lines 1-3

Additional context

This only happens when using the v8 coverage collection because of this condition: https://github.com/jestjs/jest/blob/main/packages/jest-reporters/src/CoverageReporter.ts#L188-L190

A workaround is available: set the "importsNotUsedAsValues": "preserve" compiler option when setting up ts-jest ensuring that all types referenced in the code base will be imported during tests.

Environment

System:
  OS: macOS 14.0
  CPU: (10) arm64 Apple M1 Pro
Binaries:
  Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
  Yarn: 1.22.19 - ~/.nvm/versions/node/v18.14.2/bin/yarn
  npm: 9.6.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
  pnpm: 8.6.0 - ~/.nvm/versions/node/v18.14.2/bin/pnpm
npmPackages:
  jest: ^29.5.0 => 29.5.0
github-actions[bot] commented 1 year ago

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

spalger commented 1 year ago

Still an issue in jest 29.7.0: https://github.com/spalger/reproduce-issues/commit/33880e7f2a39e64608118654bef19f8297318039

gehringf commented 1 year ago

Seeing this too

SimenB commented 1 year ago

Not sure how to handle this. There is no readInitialCoverage equivalent for v8 coverage. Also, it'd make a coverage run be slower as we'd need to transform the file.

I think the easiest is to just add type only files to coveragePathIgnorePatterns config