Skip to content

Commit

Permalink
test_runner: fix typescript coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MoLow committed Sep 4, 2023
1 parent 8dfe424 commit 3ed2c63
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function mapRangeToLines(range, lines) {
mid = MathFloor((start + end) / 2);
let line = lines[mid];

if (startOffset >= line.startOffset && startOffset <= line.endOffset) {
if (startOffset >= line?.startOffset && startOffset <= line?.endOffset) {
while (endOffset > line?.startOffset) {
// If the range is not covered, and the range covers the entire line,
// then mark that line as not covered.
Expand All @@ -363,7 +363,7 @@ function mapRangeToLines(range, lines) {
}

break;
} else if (startOffset >= line.endOffset) {
} else if (startOffset >= line?.endOffset) {
start = mid + 1;
} else {
end = mid - 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/test_runner/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,8 @@ class Test extends AsyncResource {
this.reported = true;
reporter.plan(nesting, loc, harness.counters.topLevel);

// Call this harness.coverage() before collecting diagnostics, since failure to collect coverage is a diagnostic.
const coverage = harness.coverage();
for (let i = 0; i < diagnostics.length; i++) {
reporter.diagnostic(nesting, loc, diagnostics[i]);
}
Expand All @@ -750,8 +752,6 @@ class Test extends AsyncResource {
reporter.diagnostic(nesting, loc, `todo ${harness.counters.todo}`);
reporter.diagnostic(nesting, loc, `duration_ms ${this.duration()}`);

const coverage = harness.coverage();

if (coverage) {
reporter.coverage(nesting, loc, coverage);
}
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/test-runner/output/coverage_failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Flags: --expose-internals --experimental-test-coverage

'use strict';
require('../../../common');
const { setupCoverage } = require('internal/test_runner/coverage');
const { test } = require('node:test');
const TestCoverage = setupCoverage().__proto__.constructor;

TestCoverage.prototype.summary = function() {
throw new Error('Failed to collect coverage');
};

test('ok');

16 changes: 16 additions & 0 deletions test/fixtures/test-runner/output/coverage_failure.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
TAP version 13
# Subtest: ok
ok 1 - ok
---
duration_ms: *
...
1..1
# Warning: Could not report code coverage. Error: Failed to collect coverage
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
1 change: 1 addition & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const tests = [
replaceTestDuration,
),
},
{ name: 'test-runner/output/coverage_failure.js' },
]
.filter(Boolean)
.map(({ name, tty, transform }) => ({
Expand Down

0 comments on commit 3ed2c63

Please sign in to comment.