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

debugger: improve validations and documents for watch and unwatch #46947

Merged
Merged
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
1 change: 1 addition & 0 deletions doc/api/debugger.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ debug>
after)
* `watch(expr)`: Add expression to watch list
* `unwatch(expr)`: Remove expression from watch list
* `unwatch(index)`: Remove expression at specific index from watch list
* `watchers`: List all watchers and their values (automatically listed on each
breakpoint)
* `repl`: Open debugger's repl for evaluation in debugging script's context
Expand Down
2 changes: 2 additions & 0 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ breakOnNone Don't pause on exceptions (this is the default)

watch(expr) Start watching the given expression
unwatch(expr) Stop watching an expression
unwatch(index) Stop watching an expression at specific index from watch list
watchers Print all watched expressions and their current values

exec(expr), p(expr), exec expr, p expr
Expand Down Expand Up @@ -1068,6 +1069,7 @@ function createRepl(inspector) {
},

watch(expr) {
validateString(expr, 'expression');
ArrayPrototypePush(watchedExpressions, expr);
},

Expand Down
20 changes: 20 additions & 0 deletions test/sequential/test-debugger-watch-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'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/break.js')]);

(async () => {
await cli.waitForInitialBreak();
await cli.command('watch()');
await cli.waitFor(/ERR_INVALID_ARG_TYPE/);
assert.match(cli.output, /TypeError \[ERR_INVALID_ARG_TYPE\]: The "expression" argument must be of type string\. Received undefined/);
})()
.finally(() => cli.quit())
.then(common.mustCall());
15 changes: 11 additions & 4 deletions test/sequential/test-debugger-watchers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,25 @@ try {
await cli.command('watchers');

assert.match(cli.output, /x is not defined/);

assert.match(cli.output, /1: "Hello" = 'Hello'/);
assert.match(cli.output, /2: 42 = 42/);
assert.match(cli.output, /3: NaN = NaN/);
assert.match(cli.output, /4: true = true/);
assert.match(cli.output, /5: \[1, 2\] = \[ 1, 2 \]/);
assert.match(cli.output, /6: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
'shows "..." for process.env');

await cli.command('unwatch(4)');
await cli.command('unwatch("42")');
await cli.stepCommand('n');

assert.match(cli.output, /0: x = 10/);
assert.match(cli.output, /1: "Hello" = 'Hello'/);
assert.match(cli.output, /2: NaN = NaN/);
assert.match(cli.output, /3: true = true/);
assert.match(cli.output, /4: \[1, 2\] = \[ 1, 2 \]/);
assert.match(cli.output, /3: \[1, 2\] = \[ 1, 2 \]/);
assert.match(
cli.output,
/5: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
/4: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
'shows "..." for process.env'
);

Expand Down