test(@angular/build): verify coverage ignore comments are preserved during compilation#32899
test(@angular/build): verify coverage ignore comments are preserved during compilation#32899clydin wants to merge 2 commits intoangular:mainfrom
Conversation
6af4619 to
bf91489
Compare
When script optimization is disabled, set esbuild `legalComments` to 'inline' to prevent it from moving ignore comments to the end of the file. This ensures that coverage tools (like Istanbul and V8) can find the comments in place and correctly associate them with the code they are supposed to ignore.
d75be32 to
ca27cc0
Compare
…uring compilation The underlying Vitest coverage engine depends on specific developer comments like `/* istanbul ignore next */` or `/* v8 ignore next */` being present in the executing code to accurately isolate unmeasured blocks. This commit adds strict behavioral tests to assert that the Angular CLI's in-memory compilation pipeline (via esbuild) properly preserves these structural comments and forwards them reliably to Vitest's coverage processing engine.
ca27cc0 to
9275953
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces new unit tests to verify that coverage ignore comments (istanbul and v8) are correctly respected during the build process. Additionally, it updates the esbuild configuration to set legalComments to 'inline' when optimization is disabled, ensuring license comments are preserved in the output. I have suggested refactoring the repetitive coverage assertion logic in the new test file into a helper function to improve maintainability.
| const coverageMap = JSON.parse(harness.readFile('coverage/test/coverage-final.json')); | ||
| const appComponentPath = Object.keys(coverageMap).find((p) => | ||
| p.includes('app.component.ts'), | ||
| ); | ||
| expect(appComponentPath).toBeDefined(); | ||
|
|
||
| const appComponentCoverage = coverageMap[appComponentPath!]; | ||
|
|
||
| const statementCounts = Object.values(appComponentCoverage.s) as number[]; | ||
| const hasUncoveredStatements = statementCounts.some((count) => count === 0); | ||
| expect(hasUncoveredStatements) | ||
| .withContext( | ||
| 'There should be no uncovered statements as the uncalled function was ignored', | ||
| ) | ||
| .toBeFalse(); |
There was a problem hiding this comment.
The logic for parsing the coverage report and asserting that no statements are uncovered is duplicated across all test cases in this file. Extracting this into a helper function would improve maintainability and make the tests more concise.
For example, you could define a helper like this inside the describeBuilder block:
async function assertNoUncoveredStatements(contextMessage: string) {
harness.expectFile('coverage/test/coverage-final.json').toExist();
const coverageMap = JSON.parse(harness.readFile('coverage/test/coverage-final.json'));
const appComponentPath = Object.keys(coverageMap).find((p) => p.includes('app.component.ts'));
expect(appComponentPath).toBeDefined();
const appComponentCoverage = coverageMap[appComponentPath!];
const statementCounts = Object.values(appComponentCoverage.s) as number[];
expect(statementCounts.some((count) => count === 0))
.withContext(contextMessage)
.toBeFalse();
}
The underlying Vitest coverage engine depends on specific developer comments like
/* istanbul ignore next */or/* v8 ignore next */being present in the executing code to accurately isolate unmeasured blocks.This commit adds strict behavioral tests to assert that the Angular CLI's in-memory compilation pipeline (via esbuild) properly preserves these structural comments and forwards them reliably to Vitest's coverage processing engine.