nodejs-testing icon indicating copy to clipboard operation
nodejs-testing copied to clipboard

No branch coverage

Open coder-mike opened this issue 1 year ago • 3 comments

I'm unable to see any branch coverage in the "test coverage" pane. I only see statement and function coverage.

I've created a minimal example to reproduce this.

// main.mjs
export function foo(arg) {
  return arg ? 1 : 2; // Two branches
}
// main.test.mjs
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { foo } from './main.mjs';

test('test', () => {
  assert.equal(foo(true), 1); // Only one branch tested
});

I click "run tests with coverage" in the testing panel.

The coverage breakdown doesn't show branches:

image

If I hover over it, it says "0/0" branches covered:

image

coder-mike avatar May 26 '24 00:05 coder-mike

This is kind of a limitation of V8's coverage format. Under the hood we use v8-to-istanbul, but I plan to do away with that. I'm thinking of doing some AST smartness with that refactoring in order to accurately identify branches.

connor4312 avatar May 26 '24 20:05 connor4312

Should the 100% value be green?

image

vsDizzy avatar Jun 18 '24 01:06 vsDizzy

@connor4312 I created a tool MCR, which can convert native V8 coverage data into Statements, Branches and Functions (Similar to Istanbul), unlike v8-to-istanbul, it can analyze the AST to generate more accurate coverage data.

see demo: https://cenfun.github.io/monocart-coverage-reports/v8/

However, I am not very familiar with the test coverage interface of VSCode. Are there any documents or examples? Perhaps MCR can easily generate coverage data in VSCode format from V8, just like istanbul-to-vscode

Actually, c8 has integrated MCR as an experimental feature since v10.1.0

c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js

cenfun avatar Jul 20 '24 15:07 cenfun