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 = /(REPL\d+):[0-9]+:[0-9]+/g; 9 10function run({ command, expected }) { 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 }); 25 26 r.write(`${command}\n`); 27 assert.strictEqual( 28 accum.replace(stackRegExp, '$1:*:*'), 29 expected.replace(stackRegExp, '$1:*:*') 30 ); 31 r.close(); 32} 33 34const origPrepareStackTrace = Error.prepareStackTrace; 35Error.prepareStackTrace = (err, stack) => { 36 if (err instanceof SyntaxError) 37 return err.toString(); 38 stack.push(err); 39 return stack.reverse().join('--->\n'); 40}; 41 42process.on('uncaughtException', (e) => { 43 Error.prepareStackTrace = origPrepareStackTrace; 44 throw e; 45}); 46 47const tests = [ 48 { 49 // test .load for a file that throws 50 command: `.load ${fixtures.path('repl-pretty-stack.js')}`, 51 expected: 'Uncaught Error: Whoops!--->\nREPL1:*:*--->\nd (REPL1:*:*)' + 52 '--->\nc (REPL1:*:*)--->\nb (REPL1:*:*)--->\na (REPL1:*:*)\n' 53 }, 54 { 55 command: 'let x y;', 56 expected: 'let x y;\n ^\n\n' + 57 'Uncaught SyntaxError: Unexpected identifier\n' 58 }, 59 { 60 command: 'throw new Error(\'Whoops!\')', 61 expected: 'Uncaught Error: Whoops!\n' 62 }, 63 { 64 command: 'foo = bar;', 65 expected: 'Uncaught ReferenceError: bar is not defined\n' 66 }, 67 // test anonymous IIFE 68 { 69 command: '(function() { throw new Error(\'Whoops!\'); })()', 70 expected: 'Uncaught Error: Whoops!--->\nREPL5:*:*\n' 71 }, 72]; 73 74tests.forEach(run); 75