11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst child_process = require('child_process');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_citmpdir.refresh();
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Tests that exceptions from the PromiseRejectCallback are printed to stderr
111cb0ef41Sopenharmony_ci// when they occur as a best-effort way of handling them, and that calling
121cb0ef41Sopenharmony_ci// `console.log()` works after that. Earlier, the latter did not work because
131cb0ef41Sopenharmony_ci// of the exception left lying around by the PromiseRejectCallback when its JS
141cb0ef41Sopenharmony_ci// part exceeded the call stack limit, and when the inspector/built-in coverage
151cb0ef41Sopenharmony_ci// was enabled, it resulted in a hard crash.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifor (const NODE_V8_COVERAGE of ['', tmpdir.path]) {
181cb0ef41Sopenharmony_ci  // NODE_V8_COVERAGE does not work without the inspector.
191cb0ef41Sopenharmony_ci  // Refs: https://github.com/nodejs/node/issues/29542
201cb0ef41Sopenharmony_ci  if (!process.features.inspector && NODE_V8_COVERAGE !== '') continue;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const { status, signal, stdout, stderr } =
231cb0ef41Sopenharmony_ci    child_process.spawnSync(process.execPath,
241cb0ef41Sopenharmony_ci                            [path.join(__dirname, 'test-ttywrap-stack.js')],
251cb0ef41Sopenharmony_ci                            { env: { ...process.env, NODE_V8_COVERAGE } });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert(stdout.toString('utf8')
281cb0ef41Sopenharmony_ci         .startsWith('RangeError: Maximum call stack size exceeded'),
291cb0ef41Sopenharmony_ci         `stdout: <${stdout}>`);
301cb0ef41Sopenharmony_ci  assert(stderr.toString('utf8')
311cb0ef41Sopenharmony_ci         .startsWith('Exception in PromiseRejectCallback'),
321cb0ef41Sopenharmony_ci         `stderr: <${stderr}>`);
331cb0ef41Sopenharmony_ci  assert.strictEqual(status, 0);
341cb0ef41Sopenharmony_ci  assert.strictEqual(signal, null);
351cb0ef41Sopenharmony_ci}
36