2017-09-11 18:24:21 -04:00
|
|
|
'use strict';
|
2018-08-21 14:47:47 -07:00
|
|
|
require('../common');
|
|
|
|
const ArrayStream = require('../common/arraystream');
|
2017-09-11 18:24:21 -04:00
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const assert = require('assert');
|
|
|
|
const repl = require('repl');
|
|
|
|
|
2019-07-09 13:40:01 +02:00
|
|
|
const stackRegExp = /repl:[0-9]+:[0-9]+/g;
|
2017-09-11 18:24:21 -04:00
|
|
|
|
|
|
|
function run({ command, expected }) {
|
|
|
|
let accum = '';
|
|
|
|
|
2018-08-21 14:47:47 -07:00
|
|
|
const inputStream = new ArrayStream();
|
|
|
|
const outputStream = new ArrayStream();
|
2017-09-11 18:24:21 -04:00
|
|
|
|
|
|
|
outputStream.write = (data) => accum += data.replace('\r', '');
|
|
|
|
|
|
|
|
const r = repl.start({
|
|
|
|
prompt: '',
|
|
|
|
input: inputStream,
|
|
|
|
output: outputStream,
|
|
|
|
terminal: false,
|
|
|
|
useColors: false
|
|
|
|
});
|
|
|
|
|
|
|
|
r.write(`${command}\n`);
|
2019-07-09 13:40:01 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
accum.replace(stackRegExp, 'repl:*:*'),
|
|
|
|
expected.replace(stackRegExp, 'repl:*:*')
|
|
|
|
);
|
2017-09-11 18:24:21 -04:00
|
|
|
r.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const origPrepareStackTrace = Error.prepareStackTrace;
|
|
|
|
Error.prepareStackTrace = (err, stack) => {
|
|
|
|
if (err instanceof SyntaxError)
|
|
|
|
return err.toString();
|
|
|
|
stack.push(err);
|
|
|
|
return stack.reverse().join('--->\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
process.on('uncaughtException', (e) => {
|
|
|
|
Error.prepareStackTrace = origPrepareStackTrace;
|
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
|
|
|
// test .load for a file that throws
|
|
|
|
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!--->\nrepl:*:*--->\nd (repl:*:*)' +
|
2019-07-09 13:40:01 +02:00
|
|
|
'--->\nc (repl:*:*)--->\nb (repl:*:*)--->\na (repl:*:*)\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: 'let x y;',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'let x y;\n ^\n\n' +
|
|
|
|
'Uncaught SyntaxError: Unexpected identifier\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: 'throw new Error(\'Whoops!\')',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: 'foo = bar;',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught ReferenceError: bar is not defined\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
// test anonymous IIFE
|
|
|
|
{
|
|
|
|
command: '(function() { throw new Error(\'Whoops!\'); })()',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!--->\nrepl:*:*\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(run);
|