GeezerVison is showing github.com — lib: fix sourcemaps with ts module mocking · nodejs/node@868e72e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/coverage.js +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -408,6 +408,10 @@ class TestCoverage { | 408 | 408 | } | 409 | 409 | const mappedStartOffset = this.entryToOffset(startEntry, mappedLines); | 410 | 410 | const mappedEndOffset = this.entryToOffset(endEntry, mappedLines) + 1; | | 411 | + if (mappedStartOffset < 0 || mappedEndOffset < 1) { | | 412 | + // The range is not mappable. Skip it. | | 413 | + continue; | | 414 | + } | 411 | 415 | for (let l = startEntry.originalLine; l <= endEntry.originalLine; l++) { | 412 | 416 | mappedLines[l].count = count; | 413 | 417 | } | @@ -432,7 +436,12 @@ class TestCoverage { | 432 | 436 |
| 433 | 437 | entryToOffset(entry, lines) { | 434 | 438 | const line = MathMax(entry.originalLine, 0); | 435 | | - return MathMin(lines[line].startOffset + entry.originalColumn, lines[line].endOffset); | | 439 | + const mappedLine = lines[line]; | | 440 | + if (!mappedLine) { | | 441 | + // Return -1 if the line is not mappable. | | 442 | + return -1; | | 443 | + } | | 444 | + return MathMin(mappedLine.startOffset + entry.originalColumn, mappedLine.endOffset); | 436 | 445 | } | 437 | 446 |
| 438 | 447 | mergeCoverage(merged, coverage) { | |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +export default function bar() { | | 2 | + return 'original bar'; | | 3 | +} |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import bar from './bar.mts'; | | 2 | + | | 3 | +export const foo = () => bar(); |
Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import assert from 'node:assert/strict'; | | 2 | +import { before, describe, it, mock } from 'node:test'; | | 3 | + | | 4 | +describe('foo', { concurrency: true }, () => { | | 5 | + let barMock = mock.fn(); | | 6 | + let foo; | | 7 | + | | 8 | + before(async () => { | | 9 | + const barNamedExports = await import('../coverage/bar.mts') | | 10 | + .then(({ default: _, ...rest }) => rest); | | 11 | + | | 12 | + mock.module('../coverage/bar.mts', { | | 13 | + defaultExport: barMock, | | 14 | + namedExports: barNamedExports, | | 15 | + }); | | 16 | + | | 17 | + ({ foo } = await import('../coverage/foo.mts')); | | 18 | + }); | | 19 | + | | 20 | + it('should do the thing', () => { | | 21 | + barMock.mock.mockImplementationOnce(() => 42); | | 22 | + | | 23 | + assert.equal(foo(), 42); | | 24 | + }); | | 25 | +}); |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +TAP version 13 | | 2 | +# Subtest: foo | | 3 | + # Subtest: should do the thing | | 4 | + ok 1 - should do the thing | | 5 | + --- | | 6 | + duration_ms: * | | 7 | + type: 'test' | | 8 | + ... | | 9 | + 1..1 | | 10 | +ok 1 - foo | | 11 | + --- | | 12 | + duration_ms: * | | 13 | + type: 'suite' | | 14 | + ... | | 15 | +1..1 | | 16 | +# tests 1 | | 17 | +# suites 1 | | 18 | +# pass 1 | | 19 | +# fail 0 | | 20 | +# cancelled 0 | | 21 | +# skipped 0 | | 22 | +# todo 0 | | 23 | +# duration_ms * | | 24 | +# start of coverage report | | 25 | +# ---------------------------------------------------------------------------- | | 26 | +# file | line % | branch % | funcs % | uncovered lines | | 27 | +# ---------------------------------------------------------------------------- | | 28 | +# test | | | | | | 29 | +# fixtures | | | | | | 30 | +# test-runner | | | | | | 31 | +# coverage | | | | | | 32 | +# bar.mts | 0.00 | 100.00 | 100.00 | 1-3 | | 33 | +# foo.mts | 100.00 | 100.00 | 100.00 | | | 34 | +# output | | | | | | 35 | +# typescript-coverage.mts | 100.00 | 100.00 | 100.00 | | | 36 | +# ---------------------------------------------------------------------------- | | 37 | +# all files | 85.29 | 100.00 | 85.71 | | | 38 | +# ---------------------------------------------------------------------------- | | 39 | +# end of coverage report |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-output.mjs +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -313,6 +313,15 @@ const tests = [ | 313 | 313 | flags: ['--test-reporter=tap', '--test-coverage-exclude=../output/**'], | 314 | 314 | cwd: fixtures.path('test-runner/coverage-snap'), | 315 | 315 | } : false, | | 316 | + process.features.inspector ? { | | 317 | + name: 'test-runner/output/typescript-coverage.mts', | | 318 | + flags: ['--disable-warning=ExperimentalWarning', | | 319 | + '--test-reporter=tap', | | 320 | + '--experimental-transform-types', | | 321 | + '--experimental-test-module-mocks', | | 322 | + '--experimental-test-coverage', | | 323 | + '--test-coverage-exclude=!test/**'] | | 324 | + } : false, | 316 | 325 | ] | 317 | 326 | .filter(Boolean) | 318 | 327 | .map(({ flags, name, tty, transform, cwd }) => ({ | |
|