diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index 4b11023554e268..20cc394e234078 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -635,6 +635,9 @@ function createRepl(inspector) { // List source code function list(delta = 5) { + if (!selectedFrame) { + throw new ERR_DEBUGGER_ERROR('Requires execution to be paused'); + } return selectedFrame.list(delta).then(null, (error) => { print("You can't list source code right now"); throw error; diff --git a/test/sequential/test-debugger-list.js b/test/sequential/test-debugger-list.js new file mode 100644 index 00000000000000..b5fe3143535ed7 --- /dev/null +++ b/test/sequential/test-debugger-list.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common'); + +common.skipIfInspectorDisabled(); + +const fixtures = require('../common/fixtures'); +const startCLI = require('../common/debugger'); + +const assert = require('assert'); + +const cli = startCLI([fixtures.path('debugger/three-lines.js')]); + +cli.waitForInitialBreak() + .then(() => cli.waitForPrompt()) + .then(() => cli.command('list(0)')) + .then(() => { + assert.match(cli.output, /> 1 let x = 1;/); + }) + .then(() => cli.command('list(1)')) + .then(() => { + assert.match(cli.output, /> 1 let x = 1;\n {2}2 x = x \+ 1;/); + }) + .then(() => cli.command('list(10)')) + .then(() => { + assert.match(cli.output, /> 1 let x = 1;\n {2}2 x = x \+ 1;\n {2}3 module\.exports = x;\n {2}4 /); + }) + .then(() => cli.command('c')) + .then(() => cli.waitFor(/disconnect/)) + .then(() => cli.waitForPrompt()) + .then(() => cli.command('list()')) + .then(() => { + assert.match(cli.output, /Uncaught Error \[ERR_DEBUGGER_ERROR\]: Requires execution to be paused/); + }) + .finally(() => cli.quit()) + .then(common.mustCall());