test_runner: Add option to exclude empty lines from coverage report

未关闭
#60,996 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
领域
cli, testing-qa

调研方向

从 Node.js 测试运行器的覆盖率报告器入手,查看相关 issue #54753、#55228 和 #55339,包括已回滚的方法。使用 issue 中的命令重现空行报告,然后定义测试,证明新的 CLI 选项能够排除不可执行行,同时不会降低覆盖率的准确性。

由索引模型根据 Issue 内容生成。

描述

test_runner

Feature Request: Add --test-coverage-exclude-empty-lines option to test runner

Summary

The Node.js test runner's coverage reporter (--experimental-test-coverage) reports empty lines and certain non-executable lines as "uncovered", leading to inaccurate coverage percentages. Other V8-based coverage tools like Vitest have solved this with an ignoreEmptyLines / excludeEmptyLines option.

Problem

When running tests with coverage using the native test runner:

node --test --experimental-test-coverage tests/**/*.test.ts

Empty lines between statements are reported as uncovered:

# file             | line % | branch % | funcs % | uncovered lines
#   parse.ts       |  99.18 |    78.49 |  100.00 | 118 183-184

Looking at line 118:

116:  // Find the element declaration for this root
117:  const elementEntry = findElementByName(schema, rootLocalName);
118:  
119:  if (!elementEntry) {

Line 118 is empty - it contains no executable code, yet it's reported as uncovered.

Expected Behavior

Empty lines, comments, and TypeScript type-only lines should be excluded from coverage calculations, or there should be an option to exclude them.

How Other Tools Solve This

Vitest (@vitest/coverage-v8)

Vitest uses v8-to-istanbul with the excludeEmptyLines option:

v8-to-istanbul

PR implementing this feature: https://github.com/istanbuljs/v8-to-istanbul/pull/244

The approach:

When source maps are available, use @jridgewell/trace-mapping to figure out which lines contain runtime code. If a line is not present in source maps, consider it as empty line. This will exclude TypeScript typings, comments, empty lines and any other special syntax that transpiled languages exclude from source maps.

c8

c8 also uses v8-to-istanbul and can leverage the same excludeEmptyLines option.

Previous Attempts

  • PR #55228 attempted to add "ignore unmapped lines for coverage"
  • PR #55339 reverted it due to test accuracy issues
  • Issue #54753 tracks this feature request

Proposed Solution

Add a CLI flag --test-coverage-exclude-empty-lines (or similar) that:

  1. Uses source map information to identify non-executable lines
  2. Excludes empty lines from coverage calculations
  3. Optionally excludes comments (when source maps indicate they're not runtime code)

This could be implemented by:

  1. Integrating v8-to-istanbul's excludeEmptyLines logic
  2. Or using @jridgewell/trace-mapping directly to filter coverage data

Reproduction

# Create a simple TypeScript file with empty lines
cat > test.ts << 'EOF'
export function add(a: number, b: number): number {
  const result = a + b;
  
  return result;
}
EOF

cat > test.test.ts << 'EOF'
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { add } from './test.ts';

describe('add', () => {
  it('should add numbers', () => {
    assert.equal(add(1, 2), 3);
  });
});
EOF

# Run with coverage (using tsx for TypeScript)
npx tsx --test --experimental-test-coverage test.test.ts

The empty line (line 3) will show as uncovered despite 100% of executable code being tested.

Environment

  • Node.js: v24.10.0
  • OS: Linux

Related Issues

  • #54753 - Original feature request
  • #55228 - Previous implementation attempt
  • #55339 - Revert of #55228
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。