From 45b02506924686ac8cb9ce63aa1a260ab5fb6562 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Tue, 20 Aug 2024 22:38:55 -0400 Subject: [PATCH] test_runner: account for newline in source maps 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: https://github.com/nodejs/node/issues/54240 PR-URL: https://github.com/nodejs/node/pull/54444 Reviewed-By: Jake Yuesong Li Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Moshe Atlow --- lib/internal/test_runner/coverage.js | 4 +- .../source-map-line-lengths/index.js | 77 +++++++++++++++++++ .../source-map-line-lengths/index.js.map | 7 ++ .../source-map-line-lengths/index.ts | 76 ++++++++++++++++++ test/parallel/test-runner-coverage.js | 13 ++++ 5 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/test-runner/source-map-line-lengths/index.js create mode 100644 test/fixtures/test-runner/source-map-line-lengths/index.js.map create mode 100644 test/fixtures/test-runner/source-map-line-lengths/index.ts diff --git a/lib/internal/test_runner/coverage.js b/lib/internal/test_runner/coverage.js index b97965235e7d47..7d78bd69886566 100644 --- a/lib/internal/test_runner/coverage.js +++ b/lib/internal/test_runner/coverage.js @@ -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) { diff --git a/test/fixtures/test-runner/source-map-line-lengths/index.js b/test/fixtures/test-runner/source-map-line-lengths/index.js new file mode 100644 index 00000000000000..ff63423bfb3c8b --- /dev/null +++ b/test/fixtures/test-runner/source-map-line-lengths/index.js @@ -0,0 +1,77 @@ +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(); +//# sourceMappingURL=index.js.map diff --git a/test/fixtures/test-runner/source-map-line-lengths/index.js.map b/test/fixtures/test-runner/source-map-line-lengths/index.js.map new file mode 100644 index 00000000000000..ace65dc3ccdd14 --- /dev/null +++ b/test/fixtures/test-runner/source-map-line-lengths/index.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["index.ts"], + "sourcesContent": ["1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\n1;\nfunction a() {\n console.log(1);\n}\na();\n"], + "mappings": "AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,IAAI;AACX,UAAQ,IAAI,CAAC;AACf;AACA,EAAE;", + "names": [] +} diff --git a/test/fixtures/test-runner/source-map-line-lengths/index.ts b/test/fixtures/test-runner/source-map-line-lengths/index.ts new file mode 100644 index 00000000000000..0eee24b0c75e45 --- /dev/null +++ b/test/fixtures/test-runner/source-map-line-lengths/index.ts @@ -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(); diff --git a/test/parallel/test-runner-coverage.js b/test/parallel/test-runner-coverage.js index 1283232a867246..8a6cb392de2585 100644 --- a/test/parallel/test-runner-coverage.js +++ b/test/parallel/test-runner-coverage.js @@ -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); +});