Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 0 additions & 87 deletions test/parallel/test-repl-user-error-handler.js

This file was deleted.

88 changes: 88 additions & 0 deletions test/parallel/test-repl-user-error-handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as common from '../common/index.mjs';
import { start } from 'node:repl';
import assert from 'node:assert';
import { PassThrough } from 'node:stream';
import { once } from 'node:events';
import test from 'node:test';
import { spawn } from 'node:child_process';

common.skipIfInspectorDisabled();

function* generateCases() {
for (const async of [false, true]) {
for (const handleErrorReturn of ['ignore', 'print', 'unhandled', 'badvalue']) {
if (handleErrorReturn === 'badvalue') {
// Handled through a separate test using a child process
continue;
}
yield { async, handleErrorReturn };
}
}
}

test('repl handleError', async (t) => {
for (const { async, handleErrorReturn } of generateCases()) {
await t.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);

let outputString = '';
options.output.on('data', (chunk) => { outputString += chunk; });

const inputString = async ?
'setImmediate(() => { throw new Error("testerror") })\n42\n' :
'throw new Error("testerror")\n42\n';
options.input.end(inputString);

await once(repl, 'handled-error');
assert.strictEqual(err.message, 'testerror');

Check failure on line 54 in test/parallel/test-repl-user-error-handler.mjs

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stdout --- Test failure: 'async: true, handleErrorReturn: ignore' Location: test/parallel/test-repl-user-error-handler.mjs:25:13 AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'Inspector error -32000: Promise was collected' - 'testerror' at TestContext.<anonymous> (file:///home/runner/work/node/node/node/test/parallel/test-repl-user-error-handler.mjs:54:14) at async Test.run (node:internal/test_runner/test:1404:7) at async TestContext.<anonymous> (file:///home/runner/work/node/node/node/test/parallel/test-repl-user-error-handler.mjs:25:5) at async Test.run (node:internal/test_runner/test:1404:7) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:387:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'Inspector error -32000: Promise was collected', expected: 'testerror', operator: 'strictEqual', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/parallel/test-repl-user-error-handler.mjs

Check failure on line 54 in test/parallel/test-repl-user-error-handler.mjs

View workflow job for this annotation

GitHub Actions / x86_64-linux: with shared libraries / build

--- stdout --- Test failure: 'async: true, handleErrorReturn: ignore' Location: test/parallel/test-repl-user-error-handler.mjs:25:13 AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'Inspector error -32000: Promise was collected' - 'testerror' at TestContext.<anonymous> (file:///home/runner/work/_temp/node-v27.0.0-nightly2026-07-195283197f6c-slim/test/parallel/test-repl-user-error-handler.mjs:54:14) at async Test.run (node:internal/test_runner/test:1404:7) at async TestContext.<anonymous> (file:///home/runner/work/_temp/node-v27.0.0-nightly2026-07-195283197f6c-slim/test/parallel/test-repl-user-error-handler.mjs:25:5) at async Test.run (node:internal/test_runner/test:1404:7) at async startSubtestAfterBootstrap (node:internal/test_runner/harness:387:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'Inspector error -32000: Promise was collected', expected: 'testerror', operator: 'strictEqual', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v27.0.0-nightly2026-07-195283197f6c-slim/test/parallel/test-repl-user-error-handler.mjs
while (!/42/.test(outputString)) {
await once(options.output, 'data');
}

if (handleErrorReturn === 'print') {
assert.match(outputString, /testerror/);
} else {
assert.doesNotMatch(outputString, /testerror/);
}

if (uncaughtExceptionEvent) {
const [uncaughtErr] = await uncaughtExceptionEvent;
assert.strictEqual(uncaughtErr, err);
}
});
}

await t.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/);
});
});
Loading