1'use strict'; 2require('../common'); 3const ArrayStream = require('../common/arraystream'); 4const fixtures = require('../common/fixtures'); 5const assert = require('assert'); 6const repl = require('repl'); 7 8const stackRegExp = /(at .*REPL\d+:)[0-9]+:[0-9]+/g; 9 10function run({ command, expected, ...extraREPLOptions }, i) { 11 let accum = ''; 12 13 const inputStream = new ArrayStream(); 14 const outputStream = new ArrayStream(); 15 16 outputStream.write = (data) => accum += data.replace('\r', ''); 17 18 const r = repl.start({ 19 prompt: '', 20 input: inputStream, 21 output: outputStream, 22 terminal: false, 23 useColors: false, 24 ...extraREPLOptions 25 }); 26 27 r.write(`${command}\n`); 28 console.log(i); 29 assert.strictEqual( 30 accum.replace(stackRegExp, '$1*:*'), 31 expected.replace(stackRegExp, '$1*:*') 32 ); 33 r.close(); 34} 35 36const tests = [ 37 { 38 // Test .load for a file that throws. 39 command: `.load ${fixtures.path('repl-pretty-stack.js')}`, 40 expected: 'Uncaught Error: Whoops!\n at REPL1:*:*\n' + 41 ' at d (REPL1:*:*)\n at c (REPL1:*:*)\n' + 42 ' at b (REPL1:*:*)\n at a (REPL1:*:*)\n' 43 }, 44 { 45 command: 'let x y;', 46 expected: 'let x y;\n ^\n\n' + 47 'Uncaught SyntaxError: Unexpected identifier\n' 48 }, 49 { 50 command: 'throw new Error(\'Whoops!\')', 51 expected: 'Uncaught Error: Whoops!\n' 52 }, 53 { 54 command: '(() => { const err = Error(\'Whoops!\'); ' + 55 'err.foo = \'bar\'; throw err; })()', 56 expected: "Uncaught Error: Whoops!\n at REPL4:*:* {\n foo: 'bar'\n}\n", 57 }, 58 { 59 command: '(() => { const err = Error(\'Whoops!\'); ' + 60 'err.foo = \'bar\'; throw err; })()', 61 expected: 'Uncaught Error: Whoops!\n at REPL5:*:* {\n foo: ' + 62 "\u001b[32m'bar'\u001b[39m\n}\n", 63 useColors: true 64 }, 65 { 66 command: 'foo = bar;', 67 expected: 'Uncaught ReferenceError: bar is not defined\n' 68 }, 69 // Test anonymous IIFE. 70 { 71 command: '(function() { throw new Error(\'Whoops!\'); })()', 72 expected: 'Uncaught Error: Whoops!\n at REPL7:*:*\n' 73 }, 74]; 75 76tests.forEach(run); 77