-
-
Notifications
You must be signed in to change notification settings - Fork 32k
test_runner: report covered lines, functions and branches to reporters #49320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 6 commits into
nodejs:main
from
philnash:test-runner-coverage-covered-lines
Aug 30, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dba88a
test_runner: report covered lines, functions and branches to reporters
philnash 85e4ba5
test_runner: fixing style issues
philnash a382307
test_runner: remove console.logs and fix lint errors
philnash e0dd2d4
test_runner: addressing PR comments
philnash 81448af
test_runner: tests for coverage event
philnash 741cdf9
test_runner: skip new coverage tests if inspector is not available
philnash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ const { | |||||||||||||||
StringPrototypeIncludes, | ||||||||||||||||
StringPrototypeLocaleCompare, | ||||||||||||||||
StringPrototypeStartsWith, | ||||||||||||||||
MathMax, | ||||||||||||||||
} = primordials; | ||||||||||||||||
const { | ||||||||||||||||
copyFileSync, | ||||||||||||||||
|
@@ -43,6 +44,7 @@ class CoverageLine { | |||||||||||||||
this.startOffset = startOffset; | ||||||||||||||||
this.endOffset = startOffset + src.length - newlineLength; | ||||||||||||||||
this.ignore = false; | ||||||||||||||||
this.count = 0; | ||||||||||||||||
this.#covered = true; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -118,6 +120,8 @@ class TestCoverage { | |||||||||||||||
let totalFunctions = 0; | ||||||||||||||||
let branchesCovered = 0; | ||||||||||||||||
let functionsCovered = 0; | ||||||||||||||||
const functionReports = []; | ||||||||||||||||
const branchReports = []; | ||||||||||||||||
|
||||||||||||||||
const lines = ArrayPrototypeMap(linesWithBreaks, (line, i) => { | ||||||||||||||||
const startOffset = offset; | ||||||||||||||||
|
@@ -159,12 +163,20 @@ class TestCoverage { | |||||||||||||||
for (let j = 0; j < functions.length; ++j) { | ||||||||||||||||
const { isBlockCoverage, ranges } = functions[j]; | ||||||||||||||||
|
||||||||||||||||
let maxCountPerFunction = 0; | ||||||||||||||||
for (let k = 0; k < ranges.length; ++k) { | ||||||||||||||||
const range = ranges[k]; | ||||||||||||||||
maxCountPerFunction = MathMax(maxCountPerFunction, range.count); | ||||||||||||||||
|
||||||||||||||||
mapRangeToLines(range, lines); | ||||||||||||||||
|
||||||||||||||||
if (isBlockCoverage) { | ||||||||||||||||
ArrayPrototypePush(branchReports, { | ||||||||||||||||
__proto__: null, | ||||||||||||||||
line: range.lines[0].line, | ||||||||||||||||
count: range.count, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
if (range.count !== 0 || | ||||||||||||||||
range.ignoredLines === range.lines.length) { | ||||||||||||||||
branchesCovered++; | ||||||||||||||||
|
@@ -177,6 +189,13 @@ class TestCoverage { | |||||||||||||||
if (j > 0 && ranges.length > 0) { | ||||||||||||||||
const range = ranges[0]; | ||||||||||||||||
|
||||||||||||||||
ArrayPrototypePush(functionReports, { | ||||||||||||||||
__proto__: null, | ||||||||||||||||
name: functions[j].functionName, | ||||||||||||||||
philnash marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
count: maxCountPerFunction, | ||||||||||||||||
line: range.lines[0].line, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
if (range.count !== 0 || range.ignoredLines === range.lines.length) { | ||||||||||||||||
functionsCovered++; | ||||||||||||||||
} | ||||||||||||||||
|
@@ -186,15 +205,19 @@ class TestCoverage { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let coveredCnt = 0; | ||||||||||||||||
const uncoveredLineNums = []; | ||||||||||||||||
const lineReports = []; | ||||||||||||||||
|
||||||||||||||||
for (let j = 0; j < lines.length; ++j) { | ||||||||||||||||
const line = lines[j]; | ||||||||||||||||
|
||||||||||||||||
if (!line.ignore) { | ||||||||||||||||
ArrayPrototypePush(lineReports, { | ||||||||||||||||
__proto__: null, | ||||||||||||||||
line: line.line, | ||||||||||||||||
count: line.count, | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
if (line.covered || line.ignore) { | ||||||||||||||||
coveredCnt++; | ||||||||||||||||
} else { | ||||||||||||||||
ArrayPrototypePush(uncoveredLineNums, line.line); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -210,7 +233,9 @@ class TestCoverage { | |||||||||||||||
coveredLinePercent: toPercentage(coveredCnt, lines.length), | ||||||||||||||||
coveredBranchPercent: toPercentage(branchesCovered, totalBranches), | ||||||||||||||||
coveredFunctionPercent: toPercentage(functionsCovered, totalFunctions), | ||||||||||||||||
uncoveredLineNumbers: uncoveredLineNums, | ||||||||||||||||
functions: functionReports, | ||||||||||||||||
branches: branchReports, | ||||||||||||||||
lines: lineReports, | ||||||||||||||||
}); | ||||||||||||||||
|
||||||||||||||||
coverageSummary.totals.totalLineCount += lines.length; | ||||||||||||||||
|
@@ -320,6 +345,11 @@ function mapRangeToLines(range, lines) { | |||||||||||||||
if (count === 0 && startOffset <= line.startOffset && | ||||||||||||||||
endOffset >= line.endOffset) { | ||||||||||||||||
line.covered = false; | ||||||||||||||||
line.count = 0; | ||||||||||||||||
} | ||||||||||||||||
if (count > 0 && startOffset <= line.startOffset && | ||||||||||||||||
endOffset >= line.endOffset) { | ||||||||||||||||
Comment on lines
+350
to
+351
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches the existing style, in particular the conditional directly above it. I've tried to stay away from changing formatting too much. |
||||||||||||||||
line.count = count; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
ArrayPrototypePush(mappedLines, line); | ||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Transform } from 'node:stream'; | ||
|
||
export default class CoverageReporter extends Transform { | ||
constructor(options) { | ||
super({ ...options, writableObjectMode: true }); | ||
} | ||
|
||
_transform(event, _encoding, callback) { | ||
if (event.type === 'test:coverage') { | ||
callback(null, JSON.stringify(event.data, null, 2)); | ||
} else { | ||
callback(null); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.