jestjs/jest
View on GitHub[Bug]: Untested code is not transformed when using v8 coverage collector
Open
#14422 opened on Aug 17, 2023
: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
-
clone repro branch:
git clone --branch jest/v8-coverage-untested-ts-files git@github.com:spalger/reproduce-issues.git -
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
types.tsis not imported during test execution because TypeScript strips type-only imports by default- when coverage is being calculated the source of the file is not transformed using the configured transformer
types.tsis 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