summaryrefslogtreecommitdiff
path: root/test/sequential/test-inspector-async-stack-traces-promise-then.js
blob: 165db81c83d7feb90dbd400578e84b703c7833b6 (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
// Flags: --expose-internals
'use strict';
const common = require('../common');
common.skipIfInspectorDisabled();
common.skipIf32Bits();
const { NodeInstance } = require('../common/inspector-helper');
const assert = require('assert');

const script = `runTest();
function runTest() {
  const p = Promise.resolve();
  p.then(function break1() { // lineNumber 3
    debugger;
  });
  p.then(function break2() { // lineNumber 6
    debugger;
  });
}
`;

async function runTests() {
  const instance = new NodeInstance(undefined, script);
  const session = await instance.connectInspectorSession();
  await session.send([
    { 'method': 'Runtime.enable' },
    { 'method': 'Debugger.enable' },
    { 'method': 'Debugger.setAsyncCallStackDepth',
      'params': { 'maxDepth': 10 } },
    { 'method': 'Debugger.setBlackboxPatterns',
      'params': { 'patterns': [] } },
    { 'method': 'Runtime.runIfWaitingForDebugger' }
  ]);

  await session.waitForBreakOnLine(2, '[eval]');
  await session.send({ 'method': 'Debugger.resume' });

  console.error('[test] Waiting for break1');
  debuggerPausedAt(await session.waitForBreakOnLine(6, '[eval]'),
                   'break1', 'runTest:5');

  await session.send({ 'method': 'Debugger.resume' });

  console.error('[test] Waiting for break2');
  debuggerPausedAt(await session.waitForBreakOnLine(9, '[eval]'),
                   'break2', 'runTest:8');

  await session.runToCompletion();
  assert.strictEqual(0, (await instance.expectShutdown()).exitCode);
}

function debuggerPausedAt(msg, functionName, previousTickLocation) {
  assert(
    !!msg.params.asyncStackTrace,
    `${Object.keys(msg.params)} contains "asyncStackTrace" property`);

  assert.strictEqual(msg.params.callFrames[0].functionName, functionName);
  assert.strictEqual(msg.params.asyncStackTrace.description, 'Promise.then');

  const frameLocations = msg.params.asyncStackTrace.callFrames.map(
    (frame) => `${frame.functionName}:${frame.lineNumber}`);
  assertArrayIncludes(frameLocations, previousTickLocation);
}

function assertArrayIncludes(actual, expected) {
  const expectedString = JSON.stringify(expected);
  const actualString = JSON.stringify(actual);
  assert(
    actual.includes(expected),
    `Expected ${actualString} to contain ${expectedString}.`);
}

runTests();