@@ -16,6 +16,7 @@ var path = require('path');
16
16
const { pathToFileURL } = require ( 'url' ) ;
17
17
var bufferFrom = Buffer . from ;
18
18
const semver = require ( 'semver' ) ;
19
+ const { once, mapValues, flow } = require ( 'lodash' ) ;
19
20
20
21
// Helper to create regular expressions from string templates, to use interpolation
21
22
function re ( ...args ) {
@@ -34,9 +35,9 @@ function namedExportDeclaration() {
34
35
* This varies across CJS / ESM and node versions.
35
36
* Example: ` at Module.exports.test (`...
36
37
*/
37
- function stackFrameAtTest ( sourceMapSupportInstalled = true ) {
38
+ function stackFrameAtTest ( ) {
38
39
if ( semver . gte ( process . versions . node , '18.0.0' ) ) {
39
- return extension === 'mjs' ? 'Module\\.test' : sourceMapSupportInstalled ? ' Module\\.exports\\.test' : ' exports\\.test';
40
+ return extension === 'mjs' ? 'Module\\.test' : '(?: Module\\.)? exports\\.test';
40
41
} else {
41
42
return extension === 'mjs' ? 'Module\\.test' : 'Module\\.exports\\.test' ;
42
43
}
@@ -194,7 +195,7 @@ async function compareStackTrace(sourceMap, source, expected) {
194
195
fs . writeFileSync ( `.generated-${ id } -separate.${ extension } ` , `${ header } ${ namedExportDeclaration ( ) } = function() {` +
195
196
source . join ( '\n' ) + `};//@ sourceMappingURL=.generated-${ id } -separate.${ extension } .map` ) ;
196
197
try {
197
- ( await import ( `./.generated-${ id } -separate.${ extension } ` ) ) . test ( ) ;
198
+ await ( await import ( `./.generated-${ id } -separate.${ extension } ` ) ) . test ( ) ;
198
199
} catch ( e ) {
199
200
compareLines ( e . stack . split ( / \r \n | \n / ) , rewriteExpectation ( expected , `.generated-${ id } ` , `.generated-${ id } -separate` ) ) ;
200
201
}
@@ -204,7 +205,7 @@ async function compareStackTrace(sourceMap, source, expected) {
204
205
source . join ( '\n' ) + '};//@ sourceMappingURL=data:application/json;base64,' +
205
206
bufferFrom ( sourceMap . toString ( ) ) . toString ( 'base64' ) ) ;
206
207
try {
207
- ( await import ( `./.generated-${ id } -inline.${ extension } ` ) ) . test ( ) ;
208
+ await ( await import ( `./.generated-${ id } -inline.${ extension } ` ) ) . test ( ) ;
208
209
} catch ( e ) {
209
210
compareLines ( e . stack . split ( / \r \n | \n / ) , rewriteExpectation ( expected , `.generated-${ id } ` , `.generated-${ id } -inline` ) ) ;
210
211
}
@@ -239,6 +240,8 @@ function installSms() {
239
240
emptyCacheBetweenOperations : true // Needed to be able to test for failure
240
241
} ) ;
241
242
}
243
+ const installSmsOnce = once ( installSms ) ;
244
+
242
245
243
246
function getTestMacros ( sourceMapConstructors ) {
244
247
return { normalThrow, normalThrowWithoutSourceMapSupportInstalled} ;
@@ -255,7 +258,7 @@ async function normalThrowWithoutSourceMapSupportInstalled() {
255
258
'throw new Error("test");'
256
259
] , [
257
260
'Error: test' ,
258
- re `^ at ${ stackFrameAtTest ( false ) } \(${ stackFramePathStartsWith ( ) } (?:.*[/\\])?\.generated-${ id } \.${ extension } :1:123\)$`
261
+ re `^ at ${ stackFrameAtTest ( ) } \(${ stackFramePathStartsWith ( ) } (?:.*[/\\])?\.generated-${ id } \.${ extension } :1:123\)$`
259
262
] ) ;
260
263
}
261
264
}
@@ -318,15 +321,14 @@ describe('sourcemap style: file urls with absolute paths and sourceRoot removed,
318
321
319
322
function tests ( sourceMapPostprocessor ) {
320
323
// let createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap })
321
- const sourceMapConstructors = sourceMapCreators ( ) ;
322
- for ( const [ key , value ] of Object . entries ( sourceMapConstructors ) ) {
323
- sourceMapConstructors [ key ] = ( ...args ) => sourceMapPostprocessor ( value ( ...args ) ) ;
324
- }
324
+ const sourceMapConstructors = mapValues ( sourceMapCreators ( ) , v => flow ( v , sourceMapPostprocessor ) ) ;
325
325
const { createEmptySourceMap, createMultiLineSourceMap, createMultiLineSourceMapWithSourcesContent, createSecondLineSourceMap, createSingleLineSourceMap, createSourceMapWithGap} = sourceMapConstructors ;
326
326
const { normalThrow} = getTestMacros ( sourceMapConstructors ) ;
327
327
328
+ // Run as a hook to ensure it runs even when we execute a subset of tests
329
+ before ( installSmsOnce ) ;
330
+
328
331
it ( 'normal throw' , async function ( ) {
329
- installSms ( ) ;
330
332
await normalThrow ( ) ;
331
333
} ) ;
332
334
@@ -459,6 +461,18 @@ it('function constructor', async function() {
459
461
] ) ;
460
462
} ) ;
461
463
464
+ it ( 'async stack frames' , async function ( ) {
465
+ await compareStackTrace ( createMultiLineSourceMap ( ) , [
466
+ 'async function foo() { await bar(); }' ,
467
+ 'async function bar() { await null; throw new Error("test"); }' ,
468
+ 'return foo()'
469
+ ] , [
470
+ 'Error: test' ,
471
+ re `^ at bar \(${ stackFramePathStartsWith ( ) } (?:.*[/\\])?line2.js:1002:102\)$` ,
472
+ re `^ at async foo \(${ stackFramePathStartsWith ( ) } (?:.*[/\\])?line1.js:1001:101\)$`
473
+ ] ) ;
474
+ } ) ;
475
+
462
476
it ( 'throw with empty source map' , async function ( ) {
463
477
await compareStackTrace ( createEmptySourceMap ( ) , [
464
478
'throw new Error("test");'
@@ -861,6 +875,7 @@ it('supports multiple instances', function(done) {
861
875
}
862
876
863
877
describe ( 'redirects require() of "source-map-support" to this module' , function ( ) {
878
+ before ( installSmsOnce ) ;
864
879
it ( 'redirects' , async function ( ) {
865
880
assert . strictEqual ( require . resolve ( 'source-map-support' ) , require . resolve ( '.' ) ) ;
866
881
assert . strictEqual ( require . resolve ( 'source-map-support/register' ) , require . resolve ( './register' ) ) ;
@@ -898,6 +913,7 @@ describe('uninstall', function() {
898
913
const sourceMapConstructors = sourceMapCreators ( ) ;
899
914
const { normalThrow, normalThrowWithoutSourceMapSupportInstalled} = getTestMacros ( sourceMapConstructors ) ;
900
915
916
+ before ( installSmsOnce ) ;
901
917
this . beforeEach ( function ( ) {
902
918
underTest . uninstall ( ) ;
903
919
process . emit = priorProcessEmit ;
0 commit comments