-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
test_runner: add statement coverage support #62340
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
Open
Felipeness
wants to merge
7
commits into
nodejs:main
Choose a base branch
from
Felipeness:feat/test-runner-statement-coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a95e904
feat: test_runner: add statement coverage support
Felipeness 05a5e91
fix(test_runner): fix statement coverage visitor and edge cases
Felipeness bac6828
fix: address review feedback on statement coverage
Felipeness f17a399
fix(test_runner): improve statement coverage implementation and tests
Felipeness e1cf6c7
refactor: inline visitor object in getStatements
Felipeness 5d3754e
fix(test_runner): fix lint errors, update tests for statement coverage
Felipeness ee6a76f
test_runner: address review feedback on statement coverage
Felipeness 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -30,6 +30,10 @@ const { tmpdir } = require('os'); | |
| const { join, resolve, relative } = require('path'); | ||
| const { fileURLToPath, URL } = require('internal/url'); | ||
| const { kMappings, SourceMap } = require('internal/source_map/source_map'); | ||
| const { Parser: AcornParser } = | ||
| require('internal/deps/acorn/acorn/dist/acorn'); | ||
| const { simple: acornWalkSimple } = | ||
| require('internal/deps/acorn/acorn-walk/dist/walk'); | ||
| const { | ||
| codes: { | ||
| ERR_SOURCE_MAP_CORRUPT, | ||
|
|
@@ -39,6 +43,12 @@ const { | |
| const { matchGlobPattern } = require('internal/fs/glob'); | ||
| const { constants: { kMockSearchParam } } = require('internal/test_runner/mock/loader'); | ||
|
|
||
| // Statement types excluded from coverage: containers (BlockStatement) | ||
| // and empty statements that carry no executable semantics. | ||
| const kExcludedStatementTypes = new SafeSet([ | ||
| 'BlockStatement', 'EmptyStatement', | ||
| ]); | ||
|
|
||
| const kCoverageFileRegex = /^coverage-(\d+)-(\d{13})-(\d+)\.json$/; | ||
| const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//; | ||
| const kLineEndingRegex = /\r?\n$/u; | ||
|
|
@@ -69,6 +79,64 @@ class TestCoverage { | |
| } | ||
|
|
||
| #sourceLines = new SafeMap(); | ||
| #sourceStatements = new SafeMap(); | ||
|
|
||
| getStatements(fileUrl, source) { | ||
| if (this.#sourceStatements.has(fileUrl)) { | ||
| return this.#sourceStatements.get(fileUrl); | ||
| } | ||
|
|
||
| try { | ||
| source ??= readFileSync(fileURLToPath(fileUrl), 'utf8'); | ||
| } catch { | ||
| // The file can no longer be read. Leave it out of statement coverage. | ||
| this.#sourceStatements.set(fileUrl, null); | ||
| return null; | ||
| } | ||
|
|
||
| const statements = []; | ||
|
|
||
| // acorn-walk's simple() fires a generic "Statement" visitor for every | ||
| // node dispatched in a statement position (Program body, block bodies, | ||
| // if/for/while bodies, etc.). This automatically covers all current and | ||
| // future ESTree statement types without hardcoding a list. | ||
| const visitor = { __proto__: null }; | ||
| visitor.Statement = (node) => { | ||
| if (kExcludedStatementTypes.has(node.type)) return; | ||
| ArrayPrototypePush(statements, { | ||
| __proto__: null, | ||
| startOffset: node.start, | ||
| endOffset: node.end, | ||
| count: 0, | ||
| }); | ||
| }; | ||
|
|
||
| // Parse as script with permissive flags — script mode is non-strict, | ||
| // so it handles legacy CJS (e.g. `with` statements) while the | ||
| // allow* flags enable ESM syntax (import/export/top-level await). | ||
| let ast; | ||
| try { | ||
| ast = AcornParser.parse(source, { | ||
| __proto__: null, | ||
| ecmaVersion: 'latest', | ||
| sourceType: 'script', | ||
| allowHashBang: true, | ||
| allowReturnOutsideFunction: true, | ||
| allowImportExportEverywhere: true, | ||
| allowAwaitOutsideFunction: true, | ||
| }); | ||
| } catch { | ||
| // Acorn could not parse the file (e.g. non-JS syntax, TypeScript). | ||
| // Degrade gracefully — the file will report no statement coverage. | ||
| this.#sourceStatements.set(fileUrl, null); | ||
| return null; | ||
| } | ||
|
|
||
| acornWalkSimple(ast, visitor); | ||
|
|
||
| this.#sourceStatements.set(fileUrl, statements); | ||
| return statements; | ||
| } | ||
|
|
||
| getLines(fileUrl, source) { | ||
| // Split the file source into lines. Make sure the lines maintain their | ||
|
|
@@ -145,18 +213,22 @@ class TestCoverage { | |
| totalLineCount: 0, | ||
| totalBranchCount: 0, | ||
| totalFunctionCount: 0, | ||
| totalStatementCount: 0, | ||
| coveredLineCount: 0, | ||
| coveredBranchCount: 0, | ||
| coveredFunctionCount: 0, | ||
| coveredStatementCount: 0, | ||
| coveredLinePercent: 0, | ||
| coveredBranchPercent: 0, | ||
| coveredFunctionPercent: 0, | ||
| coveredStatementPercent: 0, | ||
| }, | ||
| thresholds: { | ||
| __proto__: null, | ||
| line: this.options.lineCoverage, | ||
| branch: this.options.branchCoverage, | ||
| function: this.options.functionCoverage, | ||
| statement: this.options.statementCoverage, | ||
| }, | ||
| }; | ||
|
|
||
|
|
@@ -174,7 +246,17 @@ class TestCoverage { | |
| const functionReports = []; | ||
| const branchReports = []; | ||
|
|
||
| const lines = this.getLines(url); | ||
| // Read source once and pass to both getLines and getStatements to | ||
| // avoid double disk I/O for the same file. | ||
| let source; | ||
| try { | ||
| source = readFileSync(fileURLToPath(url), 'utf8'); | ||
| } catch { | ||
| // The file can no longer be read. Skip it entirely. | ||
| continue; | ||
| } | ||
|
|
||
| const lines = this.getLines(url, source); | ||
| if (!lines) { | ||
| continue; | ||
| } | ||
|
|
@@ -243,29 +325,88 @@ class TestCoverage { | |
| } | ||
| } | ||
|
|
||
| // Compute statement coverage by mapping V8 ranges to AST statements. | ||
| // Pass the source already read above to avoid double disk I/O. | ||
| const statements = this.getStatements(url, source); | ||
| let totalStatements = 0; | ||
| let statementsCovered = 0; | ||
| const statementReports = []; | ||
|
|
||
| if (statements) { | ||
| // Flatten all V8 coverage ranges into a single array so | ||
| // the statement loop only needs two levels of iteration. | ||
| const allRanges = []; | ||
| for (let fi = 0; fi < functions.length; ++fi) { | ||
| const { ranges } = functions[fi]; | ||
| for (let ri = 0; ri < ranges.length; ++ri) { | ||
| ArrayPrototypePush(allRanges, ranges[ri]); | ||
| } | ||
| } | ||
|
|
||
| for (let j = 0; j < statements.length; ++j) { | ||
| const stmt = statements[j]; | ||
| let bestCount = 0; | ||
| let bestSize = Infinity; | ||
|
|
||
| for (let ri = 0; ri < allRanges.length; ++ri) { | ||
| const range = allRanges[ri]; | ||
| if (doesRangeContainOtherRange(range, stmt)) { | ||
| const size = range.endOffset - range.startOffset; | ||
| if (size < bestSize) { | ||
| bestCount = range.count; | ||
| bestSize = size; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stmt.count = bestSize !== Infinity ? bestCount : 0; | ||
|
|
||
| const stmtLine = findLineForOffset(stmt.startOffset, lines); | ||
| const isIgnored = stmtLine != null && stmtLine.ignore; | ||
|
|
||
| if (!isIgnored) { | ||
| totalStatements++; | ||
| ArrayPrototypePush(statementReports, { | ||
| __proto__: null, | ||
| line: stmtLine?.line, | ||
| count: stmt.count, | ||
| }); | ||
| if (stmt.count > 0) { | ||
| statementsCovered++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| ArrayPrototypePush(coverageSummary.files, { | ||
| __proto__: null, | ||
| path: fileURLToPath(url), | ||
| totalLineCount: lines.length, | ||
| totalBranchCount: totalBranches, | ||
| totalFunctionCount: totalFunctions, | ||
| totalStatementCount: totalStatements, | ||
| coveredLineCount: coveredCnt, | ||
| coveredBranchCount: branchesCovered, | ||
| coveredFunctionCount: functionsCovered, | ||
| coveredStatementCount: statementsCovered, | ||
| coveredLinePercent: toPercentage(coveredCnt, lines.length), | ||
| coveredBranchPercent: toPercentage(branchesCovered, totalBranches), | ||
| coveredFunctionPercent: toPercentage(functionsCovered, totalFunctions), | ||
| coveredStatementPercent: toPercentage(statementsCovered, totalStatements), | ||
|
||
| functions: functionReports, | ||
| branches: branchReports, | ||
| lines: lineReports, | ||
| statements: statementReports, | ||
| }); | ||
|
|
||
| coverageSummary.totals.totalLineCount += lines.length; | ||
| coverageSummary.totals.totalBranchCount += totalBranches; | ||
| coverageSummary.totals.totalFunctionCount += totalFunctions; | ||
| coverageSummary.totals.totalStatementCount += totalStatements; | ||
| coverageSummary.totals.coveredLineCount += coveredCnt; | ||
| coverageSummary.totals.coveredBranchCount += branchesCovered; | ||
| coverageSummary.totals.coveredFunctionCount += functionsCovered; | ||
| coverageSummary.totals.coveredStatementCount += statementsCovered; | ||
| } | ||
|
|
||
| coverageSummary.totals.coveredLinePercent = toPercentage( | ||
|
|
@@ -280,6 +421,10 @@ class TestCoverage { | |
| coverageSummary.totals.coveredFunctionCount, | ||
| coverageSummary.totals.totalFunctionCount, | ||
| ); | ||
| coverageSummary.totals.coveredStatementPercent = toPercentage( | ||
| coverageSummary.totals.coveredStatementCount, | ||
| coverageSummary.totals.totalStatementCount, | ||
| ); | ||
| coverageSummary.files.sort(sortCoverageFiles); | ||
|
|
||
| return coverageSummary; | ||
|
|
@@ -695,4 +840,25 @@ function doesRangeContainOtherRange(range, otherRange) { | |
| range.endOffset >= otherRange.endOffset; | ||
| } | ||
|
|
||
| function findLineForOffset(offset, lines) { | ||
| let start = 0; | ||
| let end = lines.length - 1; | ||
|
|
||
| while (start <= end) { | ||
| const mid = MathFloor((start + end) / 2); | ||
| const line = lines[mid]; | ||
|
|
||
| if (offset >= line.startOffset && offset <= line.endOffset) { | ||
| return line; | ||
| } | ||
| if (offset > line.endOffset) { | ||
| start = mid + 1; | ||
| } else { | ||
| end = mid - 1; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| module.exports = { setupCoverage, TestCoverage }; | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should create this object inline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, inlined it!