Skip to content

feature: template coverage - #433

Open
lifeart wants to merge 1 commit into
ember-cli-code-coverage:masterfrom
lifeart:track-coverage-for-gts-hbs-files
Open

lifeart wants to merge 1 commit into
ember-cli-code-coverage:masterfrom
lifeart:track-coverage-for-gts-hbs-files

Conversation

@lifeart

@lifeart lifeart commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Template Coverage

This addon supports branch coverage for Glimmer/Handlebars templates (.hbs files and <template> tags in .gjs/.gts files). When COVERAGE=true, templates are automatically instrumented during compilation to track which branches are executed.

What is tracked

  • Block conditionals: {{#if}}, {{#unless}} (with and without {{else}})
  • Inline conditionals: {{if condition "yes" "no"}}, (if condition "a" "b"), and the unless equivalents
  • List branches: {{#each}}/{{#each-in}} with {{else}} (items vs empty list)
  • Chained conditionals: {{else if}} chains

How it works

A Glimmer AST plugin runs at build time and:

  1. Injects {{coverageInit}} at the template root to register an Istanbul-compatible coverage object in window.__coverage__
  2. Injects {{coverageMark}} at the start of each block branch body to record block branch hits
  3. Wraps inline conditional conditions with (coverageCond) to record inline branch hits reactively

The coverage data integrates seamlessly with the existing Istanbul pipeline, appearing in the same HTML/LCOV/JSON reports alongside JavaScript coverage.

Setup for .hbs files

No additional setup is needed. The AST plugin is registered automatically via setupPreprocessorRegistry when the addon is installed. Branch coverage for .hbs files works out of the box.

Setup for .gjs/.gts files (strict mode)

For .gjs/.gts files using <template> tags, coverage helpers need to be in JavaScript scope. The buildBabelPlugin() method already includes a Babel plugin that injects the necessary imports automatically. No extra configuration is required if you already have buildBabelPlugin() in your ember-cli-build.js.

@lifeart
lifeart force-pushed the track-coverage-for-gts-hbs-files branch 3 times, most recently from 316b242 to 3ca5027 Compare March 18, 2026 07:57
feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage

feature: template coverage
@lifeart
lifeart force-pushed the track-coverage-for-gts-hbs-files branch from 3cd51e7 to d6b09e8 Compare March 18, 2026 10:00
@SergeAstapov SergeAstapov mentioned this pull request Jun 18, 2026
aklkv added a commit to aklkv/ember-cli-code-coverage that referenced this pull request Sep 11, 2026
Closes the three gaps between this branch and what issue ember-cli-code-coverage#431 and PRs
ember-cli-code-coverage#433/ember-cli-code-coverage#434 set out to do.

Strict-mode template coverage (.gjs/.gts):
- createTemplateCoveragePlugin now takes `strict: true`, emitting
  camelCase helper references (coverageMark) instead of dash-cased
  ones. The two can't be shared: loose mode resolves helpers by name
  through the classic resolver, which current ember-resolver versions
  require to be dash-cased — they removed the camelCase fallback as a
  deliberate bug fix. Strict mode has no resolver at all, so every
  reference must be a JS binding, and a dash isn't a valid identifier
  character.
- New templateCoverageImportPlugin supplies those bindings, injecting
  the three helper imports from a Babel pre() hook so they exist before
  babel-plugin-ember-template-compilation validates template scope.
- Skip templates outside the project and anything under node_modules:
  Embroider rewrites classic v1 addon dependencies into v2-compatible
  template() calls through the host app's own `transforms`, and those
  never went through the import plugin, so instrumenting them broke the
  build outright.
- Derive the coverage key from `env.filename` when the preprocessor
  registry didn't set `env.meta.moduleName` (it never does on the
  strict-mode path), so entries no longer collapse onto a shared
  placeholder key.
- Suffix that key so it doesn't resolve to a real file. A .gjs file's
  own JS coverage has a real source map, and applying it to template
  positions read off the pre-compilation AST produced invalid columns
  and crashed reporting for the whole payload.

Fix adjustCoverage discarding coverage on key collision:
Two raw keys resolving to the same file overwrote rather than merged,
so one silently won. Merges via istanbul-lib-coverage now — the
template and JS halves of a .gjs file depend on it, and it was already
wrong for any other collision.

Standalone CLI:
`coverage-merge` bin runs the merge without ember-cli, for projects
with no ember-cli command layer. Takes optional --root and --config.

New /core subpath export, exposing the config, path-mapping and report
utilities without pulling in babel, vite, or testem code.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
aklkv added a commit to aklkv/ember-cli-code-coverage that referenced this pull request Sep 11, 2026
Closes the three gaps between this branch and what issue ember-cli-code-coverage#431 and PRs
ember-cli-code-coverage#433/ember-cli-code-coverage#434 set out to do.

Strict-mode template coverage (.gjs/.gts):
- createTemplateCoveragePlugin now takes `strict: true`, emitting
  camelCase helper references (coverageMark) instead of dash-cased
  ones. The two can't be shared: loose mode resolves helpers by name
  through the classic resolver, which current ember-resolver versions
  require to be dash-cased — they removed the camelCase fallback as a
  deliberate bug fix. Strict mode has no resolver at all, so every
  reference must be a JS binding, and a dash isn't a valid identifier
  character.
- New templateCoverageImportPlugin supplies those bindings, injecting
  the three helper imports from a Babel pre() hook so they exist before
  babel-plugin-ember-template-compilation validates template scope.
- Skip templates outside the project and anything under node_modules:
  Embroider rewrites classic v1 addon dependencies into v2-compatible
  template() calls through the host app's own `transforms`, and those
  never went through the import plugin, so instrumenting them broke the
  build outright.
- Derive the coverage key from `env.filename` when the preprocessor
  registry didn't set `env.meta.moduleName` (it never does on the
  strict-mode path), so entries no longer collapse onto a shared
  placeholder key.
- Suffix that key so it doesn't resolve to a real file. A .gjs file's
  own JS coverage has a real source map, and applying it to template
  positions read off the pre-compilation AST produced invalid columns
  and crashed reporting for the whole payload.

Fix adjustCoverage discarding coverage on key collision:
Two raw keys resolving to the same file overwrote rather than merged,
so one silently won. Merges via istanbul-lib-coverage now — the
template and JS halves of a .gjs file depend on it, and it was already
wrong for any other collision.

Standalone CLI:
`coverage-merge` bin runs the merge without ember-cli, for projects
with no ember-cli command layer. Takes optional --root and --config.

New /core subpath export, exposing the config, path-mapping and report
utilities without pulling in babel, vite, or testem code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant