Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add coverage for repl .clear + useGlobal #10777

Closed
wants to merge 1 commit into from
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
11 changes: 5 additions & 6 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,7 @@ REPLServer.prototype.createContext = function() {

Object.defineProperty(context, '_', {
configurable: true,
get: () => {
return this.last;
},
get: () => this.last,
set: (value) => {
this.last = value;
if (!this.underscoreAssigned) {
Expand Down Expand Up @@ -1266,9 +1264,10 @@ function defineDefaultCommands(repl) {
help: 'Print this help message',
action: function() {
const names = Object.keys(this.commands).sort();
const longestNameLength = names.reduce((max, name) => {
return Math.max(max, name.length);
}, 0);
const longestNameLength = names.reduce(
(max, name) => Math.max(max, name.length),
0
);
names.forEach((name) => {
const cmd = this.commands[name];
const spaces = ' '.repeat(longestNameLength - name.length + 3);
Expand Down
27 changes: 25 additions & 2 deletions test/parallel/test-repl-underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const stream = require('stream');
testSloppyMode();
testStrictMode();
testResetContext();
testResetContextGlobal();
testMagicMode();

function testSloppyMode() {
Expand Down Expand Up @@ -131,7 +132,28 @@ function testResetContext() {
]);
}

function initRepl(mode) {
function testResetContextGlobal() {
const r = initRepl(repl.REPL_MODE_STRICT, true);

r.write(`_ = 10; // explicitly set to 10
_; // 10 from user input
.clear // No output because useGlobal is true
_; // remains 10
`);

assertOutput(r.output, [
'Expression assignment to _ now disabled.',
'10',
'10',
'10',
]);

// delete globals leaked by REPL when `useGlobal` is `true`
delete global.module;
delete global.require;
}

function initRepl(mode, useGlobal) {
const inputStream = new stream.PassThrough();
const outputStream = new stream.PassThrough();
outputStream.accum = '';
Expand All @@ -146,7 +168,8 @@ function initRepl(mode) {
useColors: false,
terminal: false,
prompt: '',
replMode: mode
replMode: mode,
useGlobal: useGlobal
});
}

Expand Down