node/test/parallel/test-repl-mode.js
James M Snell 8caa1dcee6 test: rely less on duplicative common test harness utilities
There are several cleanups here that are not just style nits...

1. The `common.isMainThread` was just a passthrough to the
   `isMainThread` export on the worker_thread module. It's
   use was inconsistent and just obfuscated the fact that
   the test file depend on the `worker_threads` built-in.
   By eliminating it we simplify the test harness a bit and
   make it clearer which tests depend on the worker_threads
   check.
2. The `common.isDumbTerminal` is fairly unnecesary since
   that just wraps a public API check.
3. Several of the `common.skipIf....` checks were inconsistently
   used and really don't need to be separate utility functions.

A key part of the motivation here is to work towards making more
of the tests more self-contained and less reliant on the common
test harness where possible.

PR-URL: https://github.com/nodejs/node/pull/56712
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-01-25 07:23:09 +00:00

93 lines
2.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const Stream = require('stream');
const repl = require('repl');
if (process.env.TERM === 'dumb') {
common.skip('skipping - dumb terminal');
}
const tests = [
testSloppyMode,
testStrictMode,
testAutoMode,
testStrictModeTerminal,
];
tests.forEach(function(test) {
test();
});
function testSloppyMode() {
const cli = initRepl(repl.REPL_MODE_SLOPPY);
cli.input.emit('data', 'x = 3\n');
assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
cli.output.accumulator.length = 0;
cli.input.emit('data', 'let y = 3\n');
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
}
function testStrictMode() {
const cli = initRepl(repl.REPL_MODE_STRICT);
cli.input.emit('data', 'x = 3\n');
assert.match(cli.output.accumulator.join(''),
/ReferenceError: x is not defined/);
cli.output.accumulator.length = 0;
cli.input.emit('data', 'let y = 3\n');
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
}
function testStrictModeTerminal() {
if (!process.features.inspector) {
console.warn('Test skipped: V8 inspector is disabled');
return;
}
// Verify that ReferenceErrors are reported in strict mode previews.
const cli = initRepl(repl.REPL_MODE_STRICT, {
terminal: true
});
cli.input.emit('data', 'xyz ');
assert.ok(
cli.output.accumulator.includes('\n// ReferenceError: xyz is not defined')
);
}
function testAutoMode() {
const cli = initRepl(repl.REPL_MODE_MAGIC);
cli.input.emit('data', 'x = 3\n');
assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
cli.output.accumulator.length = 0;
cli.input.emit('data', 'let y = 3\n');
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
}
function initRepl(mode, options) {
const input = new Stream();
input.write = input.pause = input.resume = () => {};
input.readable = true;
const output = new Stream();
output.write = output.pause = output.resume = function(buf) {
output.accumulator.push(buf);
};
output.accumulator = [];
output.writable = true;
return repl.start({
input: input,
output: output,
useColors: false,
terminal: false,
replMode: mode,
...options
});
}