GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lib: fix sourcemaps with ts module mocking · nodejs/node@868e72e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/test_runner
    • coverage.js
  • test
    • fixtures/test-runner
      • coverage
        • bar.mts
        • foo.mts
      • output
        • typescript-coverage.mts
        • typescript-coverage.snapshot
    • parallel
      • test-runner-output.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/test_runner/coverage.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/coverage.js
+10-1Lines changed: 10 additions & 1 deletion
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) {
Collapse file

‎test/fixtures/test-runner/coverage/bar.mts‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function bar() {
2+
return 'original bar';
3+
}
Collapse file

‎test/fixtures/test-runner/coverage/foo.mts‎

Copy file name to clipboard
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import bar from './bar.mts';
2+
3+
export const foo = () => bar();
Collapse file

‎test/fixtures/test-runner/output/typescript-coverage.mts‎

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
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+
});
Collapse file

‎test/fixtures/test-runner/output/typescript-coverage.snapshot‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
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
Collapse file

‎test/parallel/test-runner-output.mjs‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-output.mjs
+9Lines changed: 9 additions & 0 deletions
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.