ChristianAlexander

ChristianAlexander

Hi there!

Is there a way to include heex files in code coverage reporting? It appears that ~H sections within .ex files such as views and components are reported, but when the templates are in .html.heex files, they are not considered. This can lead to a situation where if statements exist within a template that is not fully exercised by tests, but 100% test coverage is “achieved”.

I also recognize that having many conditionals or branches in a template might be an anti-pattern, but sometimes it seems like a quick way to implement some functionality. Ideally, I could rely on code coverage metrics to ensure those branches are hit.

I’ve observed this behavior using the basic mix test --cover, as well as using ExCoveralls as the project’s test_coverage.tool, in case that helps!

I hope there’s just some configuration option that I’ve missed.

Thanks!

Showing Posts 1 to 4

thbar

thbar

I have the same question today ; the use case is as follows: I detected small errors introduced while migrating our templates to HEEX, and wanted to verify which heex files were ran through one test.

I will try to figure this out, but if you already managed to achieve that, I’m interested!

thbar

thbar

I have created an issue here Is it possible to include HEEX files in the coverage? · Issue #288 · parroty/excoveralls · GitHub to give this problem a bit more visibility, since it looks important from a maintenance point of view.

LostKobrakai

LostKobrakai

By my understanding cover works with modules. And templates are not modules, but external files converted into functions on a module. So I don’t think you can get line-by-line coverage for templates out of it. The other problem is that cover works based on compiled .beam files for elixir and it’s still an erlang tool, so it’s completely unaware of macros and what they do. So coverage around macro’ed code can be rather strange.

You could manually add lines like this to your view modules to get a per template coverage though:

def render("index.html", assigns), do: render_template("index.html", assigns)
[…]
def render(other, _assigns) do
  raise RuntimeError, "trying to render template #{other}, which has no explicitly defined render implementation"
end

That’s basically what phoenix generates for you, just explicitly defined for cover to deal with functions not generated by a macro. That does also only help for code calling render/2 and not render_template/2 directly. Phoenix by default calls render_template/2 only from render/2 and render_template is a private function, so it should be fine, but something to be aware of.

You could probably even put the raising function head to be generated by a macro, so it’s automatically part of your view modules and therefore forces people to explicitly define render functions.

nivanson

nivanson

Keeping the template code in a render function and checking to see if functional components gets called will give you some idea of coverage. This is useful for complicated template logic.

See the difference when no functional components are used in the code branching.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews