Skip to content

Commit

Permalink
debugger: improve validations and documents for watch and unwatch
Browse files Browse the repository at this point in the history
- debugger: add string validation for watch(expr)
- debugger: add help document for watch(index)
- test: add test for watch(index) command
- doc: add information on unwatch(index) option

PR-URL: #46947
Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com>
  • Loading branch information
Dailyscat authored and targos committed Mar 13, 2023
1 parent e5ffba0 commit 3a24f7b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
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 @@ -1078,6 +1079,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

0 comments on commit 3a24f7b

Please sign in to comment.