11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciif (!process.features.inspector) return;
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { dirname } = require('path');
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst path = require('path');
101cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
111cb0ef41Sopenharmony_ciconst { pathToFileURL } = require('url');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet dirc = 0;
171cb0ef41Sopenharmony_cifunction nextdir() {
181cb0ef41Sopenharmony_ci  return process.env.NODE_V8_COVERAGE ||
191cb0ef41Sopenharmony_ci    path.join(tmpdir.path, `source_map_${++dirc}`);
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Outputs source maps when event loop is drained, with no async logic.
231cb0ef41Sopenharmony_ci{
241cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
251cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
261cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/basic'),
271cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
281cb0ef41Sopenharmony_ci  if (output.status !== 0) {
291cb0ef41Sopenharmony_ci    console.log(output.stderr.toString());
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
321cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
331cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache('basic.js', coverageDirectory);
341cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.url, 'https://ci.nodejs.org/418');
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Outputs source maps when process.kill(process.pid, "SIGINT"); exits process.
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
401cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
411cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/sigint'),
421cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
431cb0ef41Sopenharmony_ci  if (!common.isWindows) {
441cb0ef41Sopenharmony_ci    if (output.signal !== 'SIGINT') {
451cb0ef41Sopenharmony_ci      console.log(output.stderr.toString());
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci    assert.strictEqual(output.signal, 'SIGINT');
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
501cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache('sigint.js', coverageDirectory);
511cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.url, 'https://ci.nodejs.org/402');
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci// Outputs source maps when source-file calls process.exit(1).
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
571cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
581cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/exit-1'),
591cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
601cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
611cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache('exit-1.js', coverageDirectory);
621cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.url, 'https://ci.nodejs.org/404');
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci// Outputs source-maps for esm module.
661cb0ef41Sopenharmony_ci{
671cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
681cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
691cb0ef41Sopenharmony_ci    '--no-warnings',
701cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/esm-basic.mjs'),
711cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
721cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
731cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache('esm-basic.mjs', coverageDirectory);
741cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.url, 'https://ci.nodejs.org/405');
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci// Loads source-maps with relative path from .map file on disk.
781cb0ef41Sopenharmony_ci{
791cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
801cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
811cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/disk-relative-path'),
821cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
831cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
841cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
851cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
861cb0ef41Sopenharmony_ci    'disk-relative-path.js',
871cb0ef41Sopenharmony_ci    coverageDirectory
881cb0ef41Sopenharmony_ci  );
891cb0ef41Sopenharmony_ci  // Source-map should have been loaded from disk and sources should have been
901cb0ef41Sopenharmony_ci  // rewritten, such that they're absolute paths.
911cb0ef41Sopenharmony_ci  assert.strictEqual(
921cb0ef41Sopenharmony_ci    dirname(pathToFileURL(
931cb0ef41Sopenharmony_ci      require.resolve('../fixtures/source-map/disk-relative-path')).href),
941cb0ef41Sopenharmony_ci    dirname(sourceMap.data.sources[0])
951cb0ef41Sopenharmony_ci  );
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci// Loads source-maps from inline data URL.
991cb0ef41Sopenharmony_ci{
1001cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
1011cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1021cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/inline-base64.js'),
1031cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
1041cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
1051cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
1061cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
1071cb0ef41Sopenharmony_ci    'inline-base64.js',
1081cb0ef41Sopenharmony_ci    coverageDirectory
1091cb0ef41Sopenharmony_ci  );
1101cb0ef41Sopenharmony_ci  // base64 JSON should have been decoded, and paths to sources should have
1111cb0ef41Sopenharmony_ci  // been rewritten such that they're absolute:
1121cb0ef41Sopenharmony_ci  assert.strictEqual(
1131cb0ef41Sopenharmony_ci    dirname(pathToFileURL(
1141cb0ef41Sopenharmony_ci      require.resolve('../fixtures/source-map/inline-base64')).href),
1151cb0ef41Sopenharmony_ci    dirname(sourceMap.data.sources[0])
1161cb0ef41Sopenharmony_ci  );
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci// base64 encoding error does not crash application.
1201cb0ef41Sopenharmony_ci{
1211cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
1221cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1231cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/inline-base64-type-error.js'),
1241cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
1251cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
1261cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
1271cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
1281cb0ef41Sopenharmony_ci    'inline-base64-type-error.js',
1291cb0ef41Sopenharmony_ci    coverageDirectory
1301cb0ef41Sopenharmony_ci  );
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.data, null);
1331cb0ef41Sopenharmony_ci}
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci// JSON error does not crash application.
1361cb0ef41Sopenharmony_ci{
1371cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
1381cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1391cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/inline-base64-json-error.js'),
1401cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
1411cb0ef41Sopenharmony_ci  assert.strictEqual(output.status, 0);
1421cb0ef41Sopenharmony_ci  assert.strictEqual(output.stderr.toString(), '');
1431cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
1441cb0ef41Sopenharmony_ci    'inline-base64-json-error.js',
1451cb0ef41Sopenharmony_ci    coverageDirectory
1461cb0ef41Sopenharmony_ci  );
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.data, null);
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci// Does not apply source-map to stack trace if --experimental-modules
1521cb0ef41Sopenharmony_ci// is not set.
1531cb0ef41Sopenharmony_ci{
1541cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1551cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/uglify-throw.js'),
1561cb0ef41Sopenharmony_ci  ]);
1571cb0ef41Sopenharmony_ci  assert.strictEqual(
1581cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*uglify-throw-original\.js:5:9/),
1591cb0ef41Sopenharmony_ci    null
1601cb0ef41Sopenharmony_ci  );
1611cb0ef41Sopenharmony_ci  assert.strictEqual(
1621cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*uglify-throw-original\.js:9:3/),
1631cb0ef41Sopenharmony_ci    null
1641cb0ef41Sopenharmony_ci  );
1651cb0ef41Sopenharmony_ci}
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci// Applies source-maps generated by uglifyjs to stack trace.
1681cb0ef41Sopenharmony_ci{
1691cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1701cb0ef41Sopenharmony_ci    '--enable-source-maps',
1711cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/uglify-throw.js'),
1721cb0ef41Sopenharmony_ci  ]);
1731cb0ef41Sopenharmony_ci  assert.match(
1741cb0ef41Sopenharmony_ci    output.stderr.toString(),
1751cb0ef41Sopenharmony_ci    /.*uglify-throw-original\.js:5:9/
1761cb0ef41Sopenharmony_ci  );
1771cb0ef41Sopenharmony_ci  assert.match(
1781cb0ef41Sopenharmony_ci    output.stderr.toString(),
1791cb0ef41Sopenharmony_ci    /.*uglify-throw-original\.js:9:3/
1801cb0ef41Sopenharmony_ci  );
1811cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /at Hello/);
1821cb0ef41Sopenharmony_ci}
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci// Applies source-maps generated by tsc to stack trace.
1851cb0ef41Sopenharmony_ci{
1861cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1871cb0ef41Sopenharmony_ci    '--enable-source-maps',
1881cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/typescript-throw.js'),
1891cb0ef41Sopenharmony_ci  ]);
1901cb0ef41Sopenharmony_ci  assert.ok(output.stderr.toString().match(/.*typescript-throw\.ts:18:11/));
1911cb0ef41Sopenharmony_ci  assert.ok(output.stderr.toString().match(/.*typescript-throw\.ts:24:1/));
1921cb0ef41Sopenharmony_ci}
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci// Applies source-maps generated by babel to stack trace.
1951cb0ef41Sopenharmony_ci{
1961cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
1971cb0ef41Sopenharmony_ci    '--enable-source-maps',
1981cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/babel-throw.js'),
1991cb0ef41Sopenharmony_ci  ]);
2001cb0ef41Sopenharmony_ci  assert.ok(
2011cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*babel-throw-original\.js:18:31/)
2021cb0ef41Sopenharmony_ci  );
2031cb0ef41Sopenharmony_ci}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci// Applies source-maps generated by nyc to stack trace.
2061cb0ef41Sopenharmony_ci{
2071cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
2081cb0ef41Sopenharmony_ci    '--enable-source-maps',
2091cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/istanbul-throw.js'),
2101cb0ef41Sopenharmony_ci  ]);
2111cb0ef41Sopenharmony_ci  assert.ok(
2121cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*istanbul-throw-original\.js:5:9/)
2131cb0ef41Sopenharmony_ci  );
2141cb0ef41Sopenharmony_ci  assert.ok(
2151cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*istanbul-throw-original\.js:9:3/)
2161cb0ef41Sopenharmony_ci  );
2171cb0ef41Sopenharmony_ci}
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci// Applies source-maps in esm modules to stack trace.
2201cb0ef41Sopenharmony_ci{
2211cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
2221cb0ef41Sopenharmony_ci    '--enable-source-maps',
2231cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/babel-esm.mjs'),
2241cb0ef41Sopenharmony_ci  ]);
2251cb0ef41Sopenharmony_ci  assert.ok(
2261cb0ef41Sopenharmony_ci    output.stderr.toString().match(/.*babel-esm-original\.mjs:9:29/)
2271cb0ef41Sopenharmony_ci  );
2281cb0ef41Sopenharmony_ci}
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci// Does not persist url parameter if source-map has been parsed.
2311cb0ef41Sopenharmony_ci{
2321cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
2331cb0ef41Sopenharmony_ci  spawnSync(process.execPath, [
2341cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/inline-base64.js'),
2351cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
2361cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
2371cb0ef41Sopenharmony_ci    'inline-base64.js',
2381cb0ef41Sopenharmony_ci    coverageDirectory
2391cb0ef41Sopenharmony_ci  );
2401cb0ef41Sopenharmony_ci  assert.strictEqual(sourceMap.url, null);
2411cb0ef41Sopenharmony_ci}
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci// Persists line lengths for in-memory representation of source file.
2441cb0ef41Sopenharmony_ci{
2451cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
2461cb0ef41Sopenharmony_ci  spawnSync(process.execPath, [
2471cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/istanbul-throw.js'),
2481cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
2491cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
2501cb0ef41Sopenharmony_ci    'istanbul-throw.js',
2511cb0ef41Sopenharmony_ci    coverageDirectory
2521cb0ef41Sopenharmony_ci  );
2531cb0ef41Sopenharmony_ci  if (common.checkoutEOL === '\r\n') {
2541cb0ef41Sopenharmony_ci    assert.deepStrictEqual(sourceMap.lineLengths, [1086, 31, 185, 649, 0]);
2551cb0ef41Sopenharmony_ci  } else {
2561cb0ef41Sopenharmony_ci    assert.deepStrictEqual(sourceMap.lineLengths, [1085, 30, 184, 648, 0]);
2571cb0ef41Sopenharmony_ci  }
2581cb0ef41Sopenharmony_ci}
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci// trace.length === 0 .
2611cb0ef41Sopenharmony_ci{
2621cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
2631cb0ef41Sopenharmony_ci    '--enable-source-maps',
2641cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/emptyStackError.js'),
2651cb0ef41Sopenharmony_ci  ]);
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci  assert.ok(
2681cb0ef41Sopenharmony_ci    output.stderr.toString().match('emptyStackError')
2691cb0ef41Sopenharmony_ci  );
2701cb0ef41Sopenharmony_ci}
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci// Does not attempt to apply path resolution logic to absolute URLs
2731cb0ef41Sopenharmony_ci// with schemes.
2741cb0ef41Sopenharmony_ci// Refs: https://github.com/webpack/webpack/issues/9601
2751cb0ef41Sopenharmony_ci// Refs: https://sourcemaps.info/spec.html#h.75yo6yoyk7x5
2761cb0ef41Sopenharmony_ci{
2771cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
2781cb0ef41Sopenharmony_ci    '--enable-source-maps',
2791cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/webpack.js'),
2801cb0ef41Sopenharmony_ci  ]);
2811cb0ef41Sopenharmony_ci  // Error in original context of source content:
2821cb0ef41Sopenharmony_ci  assert.match(
2831cb0ef41Sopenharmony_ci    output.stderr.toString(),
2841cb0ef41Sopenharmony_ci    /throw new Error\('oh no!'\)\r?\n.*\^/
2851cb0ef41Sopenharmony_ci  );
2861cb0ef41Sopenharmony_ci  // Rewritten stack trace:
2871cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /webpack:\/\/\/webpack\.js:14:9/);
2881cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /at functionD.*14:9/);
2891cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /at functionC.*10:3/);
2901cb0ef41Sopenharmony_ci}
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci// Stores and applies source map associated with file that throws while
2931cb0ef41Sopenharmony_ci// being required.
2941cb0ef41Sopenharmony_ci{
2951cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
2961cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
2971cb0ef41Sopenharmony_ci    '--enable-source-maps',
2981cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/throw-on-require-entry.js'),
2991cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
3001cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
3011cb0ef41Sopenharmony_ci    'throw-on-require.js',
3021cb0ef41Sopenharmony_ci    coverageDirectory
3031cb0ef41Sopenharmony_ci  );
3041cb0ef41Sopenharmony_ci  // Rewritten stack trace.
3051cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /throw-on-require\.ts:9:9/);
3061cb0ef41Sopenharmony_ci  // Source map should have been serialized.
3071cb0ef41Sopenharmony_ci  assert.ok(sourceMap);
3081cb0ef41Sopenharmony_ci}
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci// Does not throw TypeError when primitive value is thrown.
3111cb0ef41Sopenharmony_ci{
3121cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
3131cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
3141cb0ef41Sopenharmony_ci    '--enable-source-maps',
3151cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/throw-string.js'),
3161cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
3171cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
3181cb0ef41Sopenharmony_ci    'throw-string.js',
3191cb0ef41Sopenharmony_ci    coverageDirectory
3201cb0ef41Sopenharmony_ci  );
3211cb0ef41Sopenharmony_ci  // Original stack trace.
3221cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /goodbye/);
3231cb0ef41Sopenharmony_ci  // Source map should have been serialized.
3241cb0ef41Sopenharmony_ci  assert.ok(sourceMap);
3251cb0ef41Sopenharmony_ci}
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci// Does not throw TypeError when exception occurs as result of missing named
3281cb0ef41Sopenharmony_ci// export.
3291cb0ef41Sopenharmony_ci{
3301cb0ef41Sopenharmony_ci  const coverageDirectory = nextdir();
3311cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
3321cb0ef41Sopenharmony_ci    '--enable-source-maps',
3331cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/esm-export-missing.mjs'),
3341cb0ef41Sopenharmony_ci  ], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
3351cb0ef41Sopenharmony_ci  const sourceMap = getSourceMapFromCache(
3361cb0ef41Sopenharmony_ci    'esm-export-missing.mjs',
3371cb0ef41Sopenharmony_ci    coverageDirectory
3381cb0ef41Sopenharmony_ci  );
3391cb0ef41Sopenharmony_ci  // Module loader error displayed.
3401cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(),
3411cb0ef41Sopenharmony_ci               /does not provide an export named 'Something'/);
3421cb0ef41Sopenharmony_ci  // Source map should have been serialized.
3431cb0ef41Sopenharmony_ci  assert.ok(sourceMap);
3441cb0ef41Sopenharmony_ci}
3451cb0ef41Sopenharmony_ci
3461cb0ef41Sopenharmony_ci// Does not include null for async/await with esm
3471cb0ef41Sopenharmony_ci// Refs: https://github.com/nodejs/node/issues/42417
3481cb0ef41Sopenharmony_ci{
3491cb0ef41Sopenharmony_ci  const output = spawnSync(process.execPath, [
3501cb0ef41Sopenharmony_ci    '--enable-source-maps',
3511cb0ef41Sopenharmony_ci    require.resolve('../fixtures/source-map/throw-async.mjs'),
3521cb0ef41Sopenharmony_ci  ]);
3531cb0ef41Sopenharmony_ci  // Error in original context of source content:
3541cb0ef41Sopenharmony_ci  assert.match(
3551cb0ef41Sopenharmony_ci    output.stderr.toString(),
3561cb0ef41Sopenharmony_ci    /throw new Error\(message\)\r?\n.*\^/
3571cb0ef41Sopenharmony_ci  );
3581cb0ef41Sopenharmony_ci  // Rewritten stack trace:
3591cb0ef41Sopenharmony_ci  assert.match(output.stderr.toString(), /at Throw \([^)]+throw-async\.ts:4:9\)/);
3601cb0ef41Sopenharmony_ci}
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_cifunction getSourceMapFromCache(fixtureFile, coverageDirectory) {
3631cb0ef41Sopenharmony_ci  const jsonFiles = fs.readdirSync(coverageDirectory);
3641cb0ef41Sopenharmony_ci  for (const jsonFile of jsonFiles) {
3651cb0ef41Sopenharmony_ci    let maybeSourceMapCache;
3661cb0ef41Sopenharmony_ci    try {
3671cb0ef41Sopenharmony_ci      maybeSourceMapCache = require(
3681cb0ef41Sopenharmony_ci        path.join(coverageDirectory, jsonFile)
3691cb0ef41Sopenharmony_ci      )['source-map-cache'] || {};
3701cb0ef41Sopenharmony_ci    } catch (err) {
3711cb0ef41Sopenharmony_ci      console.warn(err);
3721cb0ef41Sopenharmony_ci      maybeSourceMapCache = {};
3731cb0ef41Sopenharmony_ci    }
3741cb0ef41Sopenharmony_ci    const keys = Object.keys(maybeSourceMapCache);
3751cb0ef41Sopenharmony_ci    for (const key of keys) {
3761cb0ef41Sopenharmony_ci      if (key.includes(fixtureFile)) {
3771cb0ef41Sopenharmony_ci        return maybeSourceMapCache[key];
3781cb0ef41Sopenharmony_ci      }
3791cb0ef41Sopenharmony_ci    }
3801cb0ef41Sopenharmony_ci  }
3811cb0ef41Sopenharmony_ci}
382