forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-repl-user-error-handler.js
More file actions
84 lines (76 loc) · 2.76 KB
/
test-repl-user-error-handler.js
File metadata and controls
84 lines (76 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
const common = require('../common');
const { start } = require('node:repl');
const assert = require('node:assert');
const { PassThrough } = require('node:stream');
const { once } = require('node:events');
const test = require('node:test');
const { spawn } = require('node:child_process');
function* generateCases() {
for (const async of [false, true]) {
for (const handleErrorReturn of ['ignore', 'print', 'unhandled', 'badvalue']) {
if (handleErrorReturn === 'badvalue' && async) {
// Handled through a separate test using a child process
continue;
}
yield { async, handleErrorReturn };
}
}
}
for (const { async, handleErrorReturn } of generateCases()) {
test(`async: ${async}, handleErrorReturn: ${handleErrorReturn}`, async () => {
let err;
const options = {
input: new PassThrough(),
output: new PassThrough().setEncoding('utf8'),
handleError: common.mustCall((e) => {
err = e;
queueMicrotask(() => repl.emit('handled-error'));
return handleErrorReturn;
})
};
let uncaughtExceptionEvent;
if (handleErrorReturn === 'unhandled' && async) {
process.removeAllListeners('uncaughtException'); // Remove the test runner's handler
uncaughtExceptionEvent = once(process, 'uncaughtException');
}
const repl = start(options);
const inputString = async ?
'setImmediate(() => { throw new Error("testerror") })\n42\n' :
'throw new Error("testerror")\n42\n';
if (handleErrorReturn === 'badvalue') {
assert.throws(() => options.input.end(inputString), /ERR_INVALID_STATE/);
return;
}
options.input.end(inputString);
await once(repl, 'handled-error');
assert.strictEqual(err.message, 'testerror');
const outputString = options.output.read();
assert.match(outputString, /42/);
if (handleErrorReturn === 'print') {
assert.match(outputString, /testerror/);
} else {
assert.doesNotMatch(outputString, /testerror/);
}
if (uncaughtExceptionEvent) {
const [uncaughtErr] = await uncaughtExceptionEvent;
assert.strictEqual(uncaughtErr, err);
}
});
}
test('async: true, handleErrorReturn: badvalue', async () => {
// Can't test this the same way as the other combinations
// since this will take the process down in a way that
// cannot be caught.
const proc = spawn(process.execPath, ['-e', `
require('node:repl').start({
handleError: () => 'badvalue'
})
`], { encoding: 'utf8', stdio: 'pipe' });
proc.stdin.end('throw new Error("foo");');
let stderr = '';
proc.stderr.setEncoding('utf8').on('data', (data) => stderr += data);
const [exit] = await once(proc, 'close');
assert.strictEqual(exit, 1);
assert.match(stderr, /ERR_INVALID_STATE.+badvalue/);
});