Skip to content

Commit 5e04b2e

Browse files
author
Sean Milligan
committed
test(NODE-7493): enable source maps for accurate unit test debugging
Pass --enable-source-maps to Node for all mocha configs so V8 resolves inline source maps emitted by ts-node, making the debugger show correct TypeScript file/line locations. Also add sourceMap: true to test/tsconfig.json and ensure bundled test runs reset test/mongodb.ts so stale VM-context state does not affect subsequent debug sessions.
1 parent 24e5705 commit 5e04b2e

7 files changed

Lines changed: 85 additions & 7 deletions

File tree

.mocharc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ module.exports = {
1818
reporter: 'test/tools/reporter/mongodb_reporter.js',
1919
sort: true,
2020
color: true,
21-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
21+
'node-option':
22+
Number(major) >= 23
23+
? ['enable-source-maps', 'no-experimental-strip-types']
24+
: ['enable-source-maps']
2225
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@
141141
"check:dts": "npm run build:bundle && node ./node_modules/typescript/bin/tsc --target es2023 --module commonjs --noEmit mongodb.d.ts && tsd",
142142
"check:search-indexes": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/manual/search-index-management.prose.test.ts",
143143
"check:test": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/integration",
144-
"check:test-bundled": "MONGODB_BUNDLED=true npm run check:test",
144+
"check:test-bundled": "MONGODB_BUNDLED=true npm run check:test; npm run build:runtime-barrel",
145145
"check:unit": "npm run build:bundle && nyc mocha test/unit",
146-
"check:unit-bundled": "MONGODB_BUNDLED=true npm run check:unit",
146+
"check:unit-bundled": "MONGODB_BUNDLED=true npm run check:unit; npm run build:runtime-barrel",
147147
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
148148
"check:atlas": "npm run build:bundle && nyc mocha --config test/manual/mocharc.js test/manual/atlas_connectivity.test.ts",
149149
"check:drivers-atlas-testing": "npm run build:bundle && nyc mocha --config test/mocha_mongodb.js test/atlas/drivers_atlas_testing.test.ts",

test/manual/mocharc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ module.exports = {
1414
failZero: true,
1515
color: true,
1616
timeout: 10000,
17-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
17+
'node-option': Number(major) >= 23
18+
? ['enable-source-maps', 'no-experimental-strip-types']
19+
: ['enable-source-maps']
1820
};

test/mocha_lambda.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ module.exports = {
1717
reporter: 'test/tools/reporter/mongodb_reporter.js',
1818
sort: true,
1919
color: true,
20-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
20+
'node-option': Number(major) >= 23
21+
? ['enable-source-maps', 'no-experimental-strip-types']
22+
: ['enable-source-maps']
2123
};

test/mocha_mongodb.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ module.exports = {
3131
'test/integration/node-specific/examples/transactions.test.js',
3232
'test/integration/node-specific/examples/versioned_api.js'
3333
],
34-
'node-option': Number(major) >= 23 ? ['no-experimental-strip-types'] : undefined
34+
'node-option': Number(major) >= 23
35+
? ['enable-source-maps', 'no-experimental-strip-types']
36+
: ['enable-source-maps']
3537
};

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"strict": false,
55
"allowJs": true,
6-
"checkJs": false
6+
"checkJs": false,
7+
"sourceMap": true
78
},
89
"include": [
910
"../node_modules/@types/mocha/index.d.ts",

test/unit/sourcemap_demo.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { expect } from 'chai';
2+
3+
// These import-type lines are completely erased by the TypeScript compiler
4+
// (no blank-line placeholder is left behind), so the compiled JS has fewer
5+
// lines than this source file. That shifts every subsequent line upward in
6+
// the JS output.
7+
import type { Document as _Doc } from 'bson';
8+
import type { Collection as _Col } from '../../mongodb';
9+
import type { MongoClient as _MC } from '../../mongodb';
10+
import type { Db as _Db } from '../../mongodb';
11+
import type { AggregationCursor as _AC } from '../../mongodb';
12+
import type { FindCursor as _FC } from '../../mongodb';
13+
import type { ChangeStream as _CS } from '../../mongodb';
14+
import type { GridFSBucket as _GB } from '../../mongodb';
15+
import type { ClientSession as _Sess } from '../../mongodb';
16+
import type { AbstractCursor as _AbC } from '../../mongodb';
17+
18+
// ─── This Error is created at line 18 in the TypeScript source. ───────────
19+
// The ten `import type` lines above are erased without replacement, so in
20+
// the compiled JS this falls on line 18 − 10 = line 8.
21+
//
22+
// source-map-support patches Error.prepareStackTrace so `error.stack`
23+
// already shows the correct TS line. The demo below DISABLES that patch
24+
// temporarily so we can see what V8 reports natively.
25+
//
26+
// OLD commit (no --enable-source-maps): V8 says line 8 ← WRONG
27+
// NEW commit ( --enable-source-maps): V8 says line 18 ← correct
28+
const TS_SOURCE_LINE = 18; // must match the line below
29+
const errorAtKnownLine = new Error('source-map probe'); // ← line 18
30+
31+
/**
32+
* Capture a raw V8 stack frame by temporarily removing
33+
* source-map-support's prepareStackTrace override so we see exactly what
34+
* V8 reports — with or without its own source-map awareness.
35+
*/
36+
function rawV8FrameOf(err: Error): { file: string | null; line: number | null; col: number | null } {
37+
const saved = Error.prepareStackTrace;
38+
// Null → V8 uses its built-in formatter (honours --enable-source-maps).
39+
Error.prepareStackTrace = undefined as unknown as typeof Error.prepareStackTrace;
40+
const raw = err.stack ?? ''; // triggers V8 formatting with no hook
41+
Error.prepareStackTrace = saved;
42+
43+
// First "at …" line: " at Object.<anonymous> (/abs/path/file.ts:LINE:COL)"
44+
const match = raw.split('\n')[1]?.match(/\((.+):(\d+):(\d+)\)$/);
45+
if (!match) return { file: null, line: null, col: null };
46+
return { file: match[1], line: Number(match[2]), col: Number(match[3]) };
47+
}
48+
49+
describe('Source map verification (NODE-7493)', function () {
50+
it('V8 native stack frame reports correct TypeScript line number', function () {
51+
const frame = rawV8FrameOf(errorAtKnownLine);
52+
53+
console.log('\n ── raw V8 frame (prepareStackTrace bypassed) ──');
54+
console.log(` file : ${frame.file}`);
55+
console.log(` line : ${frame.line} (TypeScript source line is ${TS_SOURCE_LINE})`);
56+
console.log(` col : ${frame.col}`);
57+
console.log(
58+
` ${frame.line === TS_SOURCE_LINE ? '✔ line matches TS source' : `✘ line ${frame.line} ≠ TS source line ${TS_SOURCE_LINE} — source maps not applied by V8`}`
59+
);
60+
61+
expect(frame.line).to.equal(
62+
TS_SOURCE_LINE,
63+
`V8 reported line ${frame.line} but TypeScript source line is ${TS_SOURCE_LINE}. ` +
64+
`This means --enable-source-maps is absent and V8 is reading the compiled-JS ` +
65+
`line number instead of the original TypeScript line.`
66+
);
67+
});
68+
});

0 commit comments

Comments
 (0)