After a Jest 30 suite switched coverageProvider from babel to v8, every percentage moved and the threshold broke; what differs between the two providers?
answer
- rewrite the code or ask the engine
- counters in the transform step
- byte ranges converted to Istanbul
- different ignore comments
- re-baseline after switching
basics
~20 sThe default babel provider inserts Istanbul counters into each file during the transform; the v8 provider converts the engine's own block coverage. They count statements and branches differently, so figures and ignore comments do not carry over.
solid answer
~40 sWith `coverageProvider: 'babel'`, the default, Jest instruments each file in the transform step with the Istanbul Babel plugin, so counts come from the source's syntax tree. With `'v8'`, Jest leaves the code alone, collects the JavaScript engine's own block coverage while the tests run, and converts those ranges to Istanbul data, using the transformer's source map when there is one. Both feed the same reporters, but the figures differ: in one run the same file read 50% branches under babel and 33.33% under v8, and v8 counted closing braces as uncovered lines. Untested files differ too, and each provider honours its own hint: `/* istanbul ignore next */` for babel, `/* c8 ignore next */` for v8. After switching, re-baseline `coverageThreshold` and convert the ignore comments.
code
javascript · 12 linesfunction classify(res) {
if (res.status >= 500) {
throw new Error('upstream failed');
}
if (res.cached) {
return 'cached';
}
return 'fresh';
}
module.exports = {classify};
// test: expect(classify({status: 200, cached: true})).toBe('cached');go deeper
Recall that coverageProvider accepts babel, the default, or v8, and that both produce the same kinds of report.
Explain that babel inserts counters during the transform while v8 reads the engine's block coverage and converts it.
Show you expect different figures and ignore hints after a switch, and re-baseline thresholds from one commit before flipping it.
Decide one provider for the whole repository and own the migration cost, since mixed providers make every comparison meaningless.
## Two ways to measure the same run Jest 30 ships two **coverage providers**, chosen with `coverageProvider` (or `--coverageProvider`). The allowed values are `babel`, the default, and `v8`. Both end up producing Istanbul coverage data, so the same `coverageReporters` and the same `coverageThreshold` work with either. What differs is how the counts are gathered, and that changes the numbers. ## How the babel provider works With `babel`, Jest **instruments the code** before running it: 1. The configured transformer runs first (for example `babel-jest` or a TypeScript transformer). 2. Jest then runs the Istanbul Babel plugin over the result, unless the transformer instruments by itself, as `babel-jest` does. The plugin inserts counter increments for every statement, function and branch it finds in the syntax tree. 3. As the tests execute, the counters fill a coverage object; at the end, source maps translate positions back to your original file when the transformer produced a map. Because the counts come from a syntax tree, an `if` has two branches even when there is no `else`, a ternary has two, and a `try`/`catch` block is not a branch at all: an unexecuted `catch` body shows up as uncovered statements and lines. ## How the v8 provider works With `v8`, Jest **does not rewrite the code for coverage**. It asks the JavaScript engine to record precise block coverage while a test file runs, then converts the recorded byte ranges into Istanbul data with a converter that reads the transformer's source map when one exists. The ranges describe executed blocks, not syntax nodes, so: - statements and lines track each other closely, and lines holding only a closing brace can be counted as uncovered; - branches are derived from block ranges, so their number and the percentage differ from the babel count for the same code; - the HTML report shows uncovered branches as highlighted ranges, without the babel provider's small `I` and `E` markers for an untaken `if` or `else` path. ## The same file under both providers The file below was run with one test, the one that takes the cached path: | Metric | babel | v8 | |---|---|---| | `% Stmts` | 66.66 | 60 | | `% Branch` | 50 | 33.33 | | `% Lines` | 66.66 | 60 | | `Uncovered Line #s` | 3,8 | 3-4,8-9 | The tests did not change; only the measurement did. A `coverageThreshold` tuned to the babel figures can fail the moment the provider switches, which is exactly the broken build in the question. ## Untested files and ignore hints Files that `collectCoverageFrom` adds without any test loading them are also handled differently. Under babel, Jest instruments the file without running it, so its real statements, branches and functions are counted at zero. Under v8, Jest records the whole file as one unexecuted range, so the totals for that file differ from the babel totals. Ignore comments are provider-specific, and the documentation says so: the babel provider uses `/* istanbul ignore next */` and the v8 provider uses `/* c8 ignore next */`. Each provider disregards the other's hint, so a code base that relies on `istanbul ignore` comments sees that code reappear as uncovered after a switch to v8. ## Checklist for a switch - Record both sets of figures from the same commit before changing the default. - Re-baseline every `coverageThreshold` value, global and per path, against the new provider. - Convert ignore comments to the new provider's form, and remove any that hid code which should be tested. - Expect line numbers in `Uncovered Line #s` to widen to ranges under v8. - Keep one provider per repository, so figures from different packages or runs stay comparable. ## What each one is good at - **babel** needs no setup, counts from the syntax tree, shows the `I` and `E` markers that make an untaken `if` or `else` easy to spot, and honours the `istanbul ignore` hints most existing code bases already carry. - **v8** leaves the code under test free of inserted counters, because the instrumentation step in the transform is skipped, and it reports what the engine actually executed, mapped back through source maps. The choice between them is mostly about which counting model the team trusts and keeps stable; mixing the two in one set of numbers is the real mistake.
- With the babel provider and a TypeScript transformer, why does the report still point at the .ts lines?Jest instruments the transformer's JavaScript output and keeps the source map the transformer produced, then maps the coverage positions back to the original file. Without a source map from the transformer, Jest cannot relate instrumented code to the original source, so the report would not line up.
- A team switched to v8 and code under istanbul ignore comments now counts as uncovered. Why?Ignore hints are read by the provider, and each one reads its own. The babel provider honours `/* istanbul ignore next */`; the v8 provider honours `/* c8 ignore next */`. Rewrite the hints, or better, test or delete the code they were hiding.
- The two coverage providers report identical numbers for the same tests.
- The v8 provider honours istanbul ignore comments as well.
- Switching to v8 needs no threshold changes because both produce Istanbul reports.
- The babel provider only works when the project transforms its code with babel-jest.
- An uncovered catch block is reported as an uncovered branch by the babel provider.