tools icon indicating copy to clipboard operation
tools copied to clipboard

Returns marked as uncoverable

Open FMorschel opened this issue 4 months ago • 3 comments

If you have a code like this:

void f(int? i) {
  if (i == null) {
    return;
  }
  // work...
}

And you want to make sure you test the null case, you can't actually be sure it is covered, because the return is marked as uncoverable.

This is a request to start tracking it as coverable.

CC @liamappelbe

FMorschel avatar Oct 07 '25 16:10 FMorschel

I found this odd too - you can't see which path was taken in code like this:

Image

Although IIRC the same is true for things like var a = x ? 1 : 2 because coverage is all line-based right now.

I don't know whether the limitation is in pkg:coverage or in the data the VM collects though (I suspect the latter, but @liamappelbe would know for sure).

DanTup avatar Oct 07 '25 17:10 DanTup

Yeah, it's a limitation in the VM. By default coverage is only tracked for function calls. If your return statement had a function call in it, it would be marked as coverable.

You can fix this by passing the --branch-coverage flag to the coverage:test_with_coverage script. The flag is plumbed through to both the VM and package:coverage. If you're using dart test you have to pass it to both the VM and test, so it looks like dart --branch-coverage test --branch-coverage 😅

In Dart 3.10 this flag is enabled by default in the VM, though I probably need to give a little more thought to whether I enable it by default in the test tools as well.

How are you running your tests, and what version of Dart are you using?

liamappelbe avatar Oct 07 '25 23:10 liamappelbe

How are you running your tests, and what version of Dart are you using?

Was this question for me or @FMorschel? For my example above, I was using flutter test. We don't support coverage for Dart yet (we need a pkg:test release with your recent changes for that). We do pass --branch-coverage there already.

DanTup avatar Oct 08 '25 13:10 DanTup