Skip to content

Commit 8ccfcb5

Browse files
lib: fix sourcemaps with ts module mocking
PR-URL: #58193 Fixes: #58119 Reviewed-By: Pietro Marchini <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
1 parent e38ce27 commit 8ccfcb5

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

lib/internal/test_runner/coverage.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ class TestCoverage {
408408
}
409409
const mappedStartOffset = this.entryToOffset(startEntry, mappedLines);
410410
const mappedEndOffset = this.entryToOffset(endEntry, mappedLines) + 1;
411+
if (mappedStartOffset < 0 || mappedEndOffset < 1) {
412+
// The range is not mappable. Skip it.
413+
continue;
414+
}
411415
for (let l = startEntry.originalLine; l <= endEntry.originalLine; l++) {
412416
mappedLines[l].count = count;
413417
}
@@ -432,7 +436,12 @@ class TestCoverage {
432436

433437
entryToOffset(entry, lines) {
434438
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);
436445
}
437446

438447
mergeCoverage(merged, coverage) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function bar() {
2+
return 'original bar';
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import bar from './bar.mts';
2+
3+
export const foo = () => bar();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

test/parallel/test-runner-output.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ const tests = [
313313
flags: ['--test-reporter=tap', '--test-coverage-exclude=../output/**'],
314314
cwd: fixtures.path('test-runner/coverage-snap'),
315315
} : 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,
316325
]
317326
.filter(Boolean)
318327
.map(({ flags, name, tty, transform, cwd }) => ({

0 commit comments

Comments
 (0)