11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This verifies that adding an `uncaughtException` listener in an REPL instance
41cb0ef41Sopenharmony_ci// does not suppress errors in the whole application. Adding such listener
51cb0ef41Sopenharmony_ci// should throw.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cirequire('../common');
81cb0ef41Sopenharmony_ciconst ArrayStream = require('../common/arraystream');
91cb0ef41Sopenharmony_ciconst repl = require('repl');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cilet accum = '';
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst output = new ArrayStream();
151cb0ef41Sopenharmony_cioutput.write = (data) => accum += data.replace('\r', '');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciconst r = repl.start({
181cb0ef41Sopenharmony_ci  prompt: '',
191cb0ef41Sopenharmony_ci  input: new ArrayStream(),
201cb0ef41Sopenharmony_ci  output,
211cb0ef41Sopenharmony_ci  terminal: false,
221cb0ef41Sopenharmony_ci  useColors: false,
231cb0ef41Sopenharmony_ci  global: false
241cb0ef41Sopenharmony_ci});
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cir.write(
271cb0ef41Sopenharmony_ci  'process.nextTick(() => {\n' +
281cb0ef41Sopenharmony_ci  '  process.on("uncaughtException", () => console.log("Foo"));\n' +
291cb0ef41Sopenharmony_ci  '  throw new TypeError("foobar");\n' +
301cb0ef41Sopenharmony_ci  '});\n'
311cb0ef41Sopenharmony_ci);
321cb0ef41Sopenharmony_cir.write(
331cb0ef41Sopenharmony_ci  'setTimeout(() => {\n' +
341cb0ef41Sopenharmony_ci  '  throw new RangeError("abc");\n' +
351cb0ef41Sopenharmony_ci  '}, 1);console.log()\n'
361cb0ef41Sopenharmony_ci);
371cb0ef41Sopenharmony_cir.close();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cisetTimeout(() => {
401cb0ef41Sopenharmony_ci  const len = process.listenerCount('uncaughtException');
411cb0ef41Sopenharmony_ci  process.removeAllListeners('uncaughtException');
421cb0ef41Sopenharmony_ci  assert.strictEqual(len, 0);
431cb0ef41Sopenharmony_ci  assert.match(accum, /ERR_INVALID_REPL_INPUT.*(?!Type)RangeError: abc/s);
441cb0ef41Sopenharmony_ci}, 2);
45