summaryrefslogtreecommitdiff
path: root/test/parallel/test-v8-take-coverage.js
blob: b5e99ac30a57175687cf4a592c823a4e1957ca44 (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
'use strict';

if (!process.features.inspector) return;

require('../common');
const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');

tmpdir.refresh();
const intervals = 40;
// Outputs coverage when v8.takeCoverage() is invoked.
{
  const output = spawnSync(process.execPath, [
    '-r',
    fixtures.path('v8-coverage', 'take-coverage'),
    fixtures.path('v8-coverage', 'interval'),
  ], {
    env: {
      ...process.env,
      NODE_V8_COVERAGE: tmpdir.path,
      NODE_DEBUG_NATIVE: 'INSPECTOR_PROFILER',
      TEST_INTERVALS: intervals
    },
  });
  console.log(output.stderr.toString());
  assert.strictEqual(output.status, 0);
  const coverageFiles = fs.readdirSync(tmpdir.path);

  let coverages = [];
  for (const coverageFile of coverageFiles) {
    const coverage = require(path.join(tmpdir.path, coverageFile));
    for (const result of coverage.result) {
      if (result.url.includes('/interval')) {
        coverages.push({
          file: coverageFile,
          func: result.functions.find((f) => f.functionName === 'interval'),
          timestamp: coverage.timestamp
        });
      }
    }
  }

  coverages = coverages.sort((a, b) => { return a.timestamp - b.timestamp; });
  // There should be two coverages taken, one triggered by v8.takeCoverage(),
  // the other by process exit.
  console.log('Coverages:', coverages);
  assert.strictEqual(coverages.length, 3);

  let blockHitsTotal = 0;
  for (let i = 0; i < coverages.length; ++i) {
    const { ranges } = coverages[i].func;
    console.log('coverage', i, ranges);

    if (i !== coverages.length - 1) {
      // When the first two coverages are taken:
      assert.strictEqual(ranges.length, 2);
      const blockHits = ranges[0].count;
      // The block inside interval() should be hit at least once.
      assert.notStrictEqual(blockHits, 0);
      blockHitsTotal += blockHits;
      // The else branch should not be hit.
      const elseBranchHits = ranges[1].count;
      assert.strictEqual(elseBranchHits, 0);
    } else {
      // At process exit:
      assert.strictEqual(ranges.length, 3);
      const blockHits = ranges[0].count;
      // The block inside interval() should be hit at least once more.
      assert.notStrictEqual(blockHits, 0);
      blockHitsTotal += blockHits;
      // The else branch should be hit exactly once.
      const elseBranchHits = ranges[2].count;
      assert.strictEqual(elseBranchHits, 1);
      const ifBranchHits = ranges[1].count;
      assert.strictEqual(ifBranchHits, blockHits - elseBranchHits);
    }
  }

  // The block should be hit `intervals` times in total.
  assert.strictEqual(blockHitsTotal, intervals);
}