1'use strict'; 2const common = require('../common'); 3common.skipIfInspectorDisabled(); 4const assert = require('assert'); 5const { NodeInstance } = require('../common/inspector-helper.js'); 6const fixtures = require('../common/fixtures'); 7const { pathToFileURL } = require('url'); 8 9// This needs to be an ES module file to ensure that internal modules are 10// loaded before pausing. See 11// https://bugs.chromium.org/p/chromium/issues/detail?id=1246905 12const script = fixtures.path('inspector-global-function.mjs'); 13 14async function setupDebugger(session) { 15 console.log('[test]', 'Setting up a debugger'); 16 const commands = [ 17 { 'method': 'Runtime.enable' }, 18 { 'method': 'Debugger.enable' }, 19 { 'method': 'Debugger.setAsyncCallStackDepth', 20 'params': { 'maxDepth': 0 } }, 21 { 'method': 'Runtime.runIfWaitingForDebugger' }, 22 ]; 23 session.send(commands); 24 25 await session.waitForNotification('Debugger.paused', 'Initial pause'); 26 27 // NOTE(mmarchini): We wait for the second console.log to ensure we loaded 28 // every internal module before pausing. See 29 // https://bugs.chromium.org/p/chromium/issues/detail?id=1246905 30 const waitForReady = session.waitForConsoleOutput('log', 'Ready!'); 31 session.send({ 'method': 'Debugger.resume' }); 32 await waitForReady; 33} 34 35async function breakOnLine(session) { 36 console.log('[test]', 'Breaking in the code'); 37 const commands = [ 38 { 'method': 'Debugger.setBreakpointByUrl', 39 'params': { 'lineNumber': 9, 40 'url': pathToFileURL(script).toString(), 41 'columnNumber': 0, 42 'condition': '' } }, 43 { 'method': 'Runtime.evaluate', 44 'params': { 'expression': 'sum()', 45 'objectGroup': 'console', 46 'includeCommandLineAPI': true, 47 'silent': false, 48 'contextId': 1, 49 'returnByValue': false, 50 'generatePreview': true, 51 'userGesture': true, 52 'awaitPromise': false } }, 53 ]; 54 session.send(commands); 55 await session.waitForBreakOnLine(9, pathToFileURL(script).toString()); 56} 57 58async function stepOverConsoleStatement(session) { 59 console.log('[test]', 'Step over console statement and test output'); 60 session.send({ 'method': 'Debugger.stepOver' }); 61 await session.waitForConsoleOutput('log', [0, 3]); 62 await session.waitForNotification('Debugger.paused'); 63} 64 65async function runTests() { 66 // NOTE(mmarchini): Use --inspect-brk to improve avoid undeterministic 67 // behavior. 68 const child = new NodeInstance(['--inspect-brk=0'], undefined, script); 69 const session = await child.connectInspectorSession(); 70 await setupDebugger(session); 71 await breakOnLine(session); 72 await stepOverConsoleStatement(session); 73 await session.runToCompletion(); 74 assert.strictEqual((await child.expectShutdown()).exitCode, 0); 75} 76 77runTests().then(common.mustCall()); 78