• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const ArrayStream = require('../common/arraystream');
4const assert = require('assert');
5const repl = require('repl');
6
7let count = 0;
8
9function run({ command, expected, useColors = false }) {
10  let accum = '';
11
12  const output = new ArrayStream();
13  output.write = (data) => accum += data.replace('\r', '');
14
15  const r = repl.start({
16    prompt: '',
17    input: new ArrayStream(),
18    output,
19    terminal: false,
20    useColors
21  });
22
23  r.write(`${command}\n`);
24  if (typeof expected === 'string') {
25    assert.strictEqual(accum, expected);
26  } else {
27    assert(expected.test(accum), accum);
28  }
29
30  // Verify that the repl is still working as expected.
31  accum = '';
32  r.write('1 + 1\n');
33  // eslint-disable-next-line no-control-regex
34  assert.strictEqual(accum.replace(/\u001b\[[0-9]+m/g, ''), '2\n');
35  r.close();
36  count++;
37}
38
39const tests = [
40  {
41    useColors: true,
42    command: 'x',
43    expected: 'Uncaught ReferenceError: x is not defined\n'
44  },
45  {
46    useColors: true,
47    command: 'throw { foo: "test" }',
48    expected: "Uncaught { foo: \x1B[32m'test'\x1B[39m }\n"
49  },
50  {
51    command: 'process.on("uncaughtException", () => console.log("Foobar"));\n',
52    expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
53  },
54  {
55    command: 'x;\n',
56    expected: 'Uncaught ReferenceError: x is not defined\n'
57  },
58  {
59    command: 'process.on("uncaughtException", () => console.log("Foobar"));' +
60             'console.log("Baz");\n',
61    expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
62  },
63  {
64    command: 'console.log("Baz");' +
65             'process.on("uncaughtException", () => console.log("Foobar"));\n',
66    expected: /^Baz\nUncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]:.*uncaughtException/
67  },
68];
69
70process.on('exit', () => {
71  // To actually verify that the test passed we have to make sure no
72  // `uncaughtException` listeners exist anymore.
73  process.removeAllListeners('uncaughtException');
74  assert.strictEqual(count, tests.length);
75});
76
77tests.forEach(run);
78