summaryrefslogtreecommitdiff
path: root/test/sequential/test-inspector-bindings.js
blob: f5583c1d7bc6d6279b12ef6162a5e2393d99beaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Flags: --expose-internals
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
const assert = require('assert');
const inspector = require('inspector');
const path = require('path');
const { pathToFileURL } = require('url');

// This test case will set a breakpoint 4 lines below
function debuggedFunction() {
  let i;
  let accum = 0;
  for (i = 0; i < 5; i++) {
    accum += i;
  }
  return accum;
}

let scopeCallback = null;

function checkScope(session, scopeId) {
  session.post('Runtime.getProperties', {
    'objectId': scopeId,
    'ownProperties': false,
    'accessorPropertiesOnly': false,
    'generatePreview': true
  }, scopeCallback);
}

function debuggerPausedCallback(session, notification) {
  const params = notification.params;
  const callFrame = params.callFrames[0];
  const scopeId = callFrame.scopeChain[0].object.objectId;
  checkScope(session, scopeId);
}

function waitForWarningSkipAsyncStackTraces(resolve) {
  process.once('warning', function(warning) {
    if (warning.code === 'INSPECTOR_ASYNC_STACK_TRACES_NOT_AVAILABLE') {
      waitForWarningSkipAsyncStackTraces(resolve);
    } else {
      resolve(warning);
    }
  });
}

async function testNoCrashWithExceptionInCallback() {
  // There is a deliberate exception in the callback
  const session = new inspector.Session();
  session.connect();
  const error = new Error('We expect this');
  console.log('Expecting warning to be emitted');
  const promise = new Promise(waitForWarningSkipAsyncStackTraces);
  session.post('Console.enable', () => { throw error; });
  assert.strictEqual(await promise, error);
  session.disconnect();
}

function testSampleDebugSession() {
  let cur = 0;
  const failures = [];
  const expects = {
    i: [0, 1, 2, 3, 4],
    accum: [0, 0, 1, 3, 6]
  };
  scopeCallback = function(error, result) {
    const i = cur++;
    let v, actual, expected;
    for (v of result.result) {
      actual = v.value.value;
      expected = expects[v.name][i];
      if (actual !== expected) {
        failures.push(`Iteration ${i} variable: ${v.name} ` +
          `expected: ${expected} actual: ${actual}`);
      }
    }
  };
  const session = new inspector.Session();
  session.connect();
  session.on('Debugger.paused',
             (notification) => debuggerPausedCallback(session, notification));
  let cbAsSecondArgCalled = false;
  assert.throws(() => {
    session.post('Debugger.enable', function() {}, function() {});
  }, TypeError);
  session.post('Debugger.enable', () => cbAsSecondArgCalled = true);
  session.post('Debugger.setBreakpointByUrl', {
    'lineNumber': 14,
    'url': pathToFileURL(path.resolve(__dirname, __filename)).toString(),
    'columnNumber': 0,
    'condition': ''
  });

  debuggedFunction();
  assert.deepStrictEqual(cbAsSecondArgCalled, true);
  assert.deepStrictEqual(failures, []);
  assert.strictEqual(cur, 5);
  scopeCallback = null;
  session.disconnect();
  assert.throws(() => session.post('Debugger.enable'), (e) => !!e);
}

async function testNoCrashConsoleLogBeforeThrow() {
  const session = new inspector.Session();
  session.connect();
  let attempt = 1;
  process.on('warning', common.mustCall(3));
  session.on('inspectorNotification', () => {
    if (attempt++ > 3)
      return;
    console.log('console.log in handler');
    throw new Error('Exception in handler');
  });
  session.post('Runtime.enable');
  console.log('Did not crash');
  session.disconnect();
}

async function doTests() {
  await testNoCrashWithExceptionInCallback();
  testSampleDebugSession();
  let breakpointHit = false;
  scopeCallback = () => (breakpointHit = true);
  debuggedFunction();
  assert.strictEqual(breakpointHit, false);
  testSampleDebugSession();
  await testNoCrashConsoleLogBeforeThrow();
}

doTests();