jestjs/jest

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

Open

#14422 opened on Aug 17, 2023

View on GitHub
 (4 comments) (1 reaction) (0 assignees)TypeScript (45,361 stars) (6,653 forks)batch import
:bug: BugHelp WantedNeeds TriagePinned

Description

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

Contributor guide