Skip to content

Commit

Permalink
test_runner: account for newline in source maps
Browse files Browse the repository at this point in the history
This commit updates the source mapping logic in the test runner
to account for newline characters that are not included in line
length calculations.

Co-authored-by: Simon Chan <1330321+yume-chan@users.noreply.github.com>
Fixes: #54240
PR-URL: #54444
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
  • Loading branch information
2 people authored and RafaelGSS committed Aug 25, 2024
1 parent 336496b commit 45b0250
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 2 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 @@ -340,8 +340,8 @@ class TestCoverage {
const { data, lineLengths } = sourceMapCache[url];
let offset = 0;
const executedLines = ArrayPrototypeMap(lineLengths, (length, i) => {
const coverageLine = new CoverageLine(i + 1, offset, null, length);
offset += length;
const coverageLine = new CoverageLine(i + 1, offset, null, length + 1);
offset += length + 1;
return coverageLine;
});
if (data.sourcesContent != null) {
Expand Down
77 changes: 77 additions & 0 deletions test/fixtures/test-runner/source-map-line-lengths/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions test/fixtures/test-runner/source-map-line-lengths/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
1;
function a() {
console.log(1);
}
a();
13 changes: 13 additions & 0 deletions test/parallel/test-runner-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,16 @@ test('coverage with included and excluded files', skipIfNoInspector, () => {
assert.strictEqual(result.status, 0);
assert(!findCoverageFileForPid(result.pid));
});

test('properly accounts for line endings in source maps', skipIfNoInspector, () => {
const fixture = fixtures.path('test-runner', 'source-map-line-lengths', 'index.js');
const args = [
'--test', '--experimental-test-coverage', '--test-reporter', 'tap',
fixture,
];
const result = spawnSync(process.execPath, args);
const report = 'index.ts | 100.00 | 100.00 | 100.00 |';
assert.strictEqual(result.stderr.toString(), '');
assert(result.stdout.toString().includes(report));
assert.strictEqual(result.status, 0);
});

0 comments on commit 45b0250

Please sign in to comment.