Skip to content

lib: fix sourcemaps with ts module mocking #58193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ class TestCoverage {
}
const mappedStartOffset = this.entryToOffset(startEntry, mappedLines);
const mappedEndOffset = this.entryToOffset(endEntry, mappedLines) + 1;
if (mappedStartOffset < 0 || mappedEndOffset < 1) {
// The range is not mappable. Skip it.
continue;
}
for (let l = startEntry.originalLine; l <= endEntry.originalLine; l++) {
mappedLines[l].count = count;
}
Expand All @@ -432,7 +436,12 @@ class TestCoverage {

entryToOffset(entry, lines) {
const line = MathMax(entry.originalLine, 0);
return MathMin(lines[line].startOffset + entry.originalColumn, lines[line].endOffset);
const mappedLine = lines[line];
if (!mappedLine) {
// Return -1 if the line is not mappable.
return -1;
}
return MathMin(mappedLine.startOffset + entry.originalColumn, mappedLine.endOffset);
}

mergeCoverage(merged, coverage) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/test-runner/coverage/bar.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function bar() {
return 'original bar';
}
3 changes: 3 additions & 0 deletions test/fixtures/test-runner/coverage/foo.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import bar from './bar.mts';

export const foo = () => bar();
25 changes: 25 additions & 0 deletions test/fixtures/test-runner/output/typescript-coverage.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from 'node:assert/strict';
import { before, describe, it, mock } from 'node:test';

describe('foo', { concurrency: true }, () => {
let barMock = mock.fn();
let foo;

before(async () => {
const barNamedExports = await import('../coverage/bar.mts')
.then(({ default: _, ...rest }) => rest);

mock.module('../coverage/bar.mts', {
defaultExport: barMock,
namedExports: barNamedExports,
});

({ foo } = await import('../coverage/foo.mts'));
});

it('should do the thing', () => {
barMock.mock.mockImplementationOnce(() => 42);

assert.equal(foo(), 42);
});
});
39 changes: 39 additions & 0 deletions test/fixtures/test-runner/output/typescript-coverage.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
TAP version 13
# Subtest: foo
# Subtest: should do the thing
ok 1 - should do the thing
---
duration_ms: *
type: 'test'
...
1..1
ok 1 - foo
---
duration_ms: *
type: 'suite'
...
1..1
# tests 1
# suites 1
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms *
# start of coverage report
# ----------------------------------------------------------------------------
# file | line % | branch % | funcs % | uncovered lines
# ----------------------------------------------------------------------------
# test | | | |
# fixtures | | | |
# test-runner | | | |
# coverage | | | |
# bar.mts | 0.00 | 100.00 | 100.00 | 1-3
# foo.mts | 100.00 | 100.00 | 100.00 |
# output | | | |
# typescript-coverage.mts | 100.00 | 100.00 | 100.00 |
# ----------------------------------------------------------------------------
# all files | 85.29 | 100.00 | 85.71 |
# ----------------------------------------------------------------------------
# end of coverage report
9 changes: 9 additions & 0 deletions test/parallel/test-runner-output.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ const tests = [
flags: ['--test-reporter=tap', '--test-coverage-exclude=../output/**'],
cwd: fixtures.path('test-runner/coverage-snap'),
} : false,
process.features.inspector ? {
name: 'test-runner/output/typescript-coverage.mts',
flags: ['--disable-warning=ExperimentalWarning',
'--test-reporter=tap',
'--experimental-transform-types',
'--experimental-test-module-mocks',
'--experimental-test-coverage',
'--test-coverage-exclude=!test/**']
} : false,
]
.filter(Boolean)
.map(({ flags, name, tty, transform, cwd }) => ({
Expand Down
Loading