forked from cenfun/monocart-coverage-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-node-ins.js
107 lines (78 loc) · 2.89 KB
/
test-node-ins.js
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
const fs = require('fs');
const { Session } = require('inspector');
const { promisify } = require('util');
const { fileURLToPath } = require('url');
const EC = require('eight-colors');
const MCR = require('../');
const checkSnapshot = require('./check-snapshot.js');
const coverageOptions = {
// logging: 'debug',
// watermarks: [60, 90],
reports: ['v8', 'v8-json'],
name: 'My V8 Node ins Coverage Report',
assetsPath: '../assets',
// lcov: true,
outputDir: './docs/node-ins',
onEnd: function(coverageResults) {
checkSnapshot(coverageResults);
}
};
// ==================================================================
// start node.js coverage
const startV8Coverage = async () => {
const session = new Session();
session.connect();
const postSession = promisify(session.post.bind(session));
await postSession('Profiler.enable');
await postSession('Profiler.startPreciseCoverage', {
callCount: true,
detailed: true
});
return postSession;
};
const takeV8Coverage = async (postSession) => {
const { result } = await postSession('Profiler.takePreciseCoverage');
return result;
};
const stopV8Coverage = async (postSession) => {
await postSession('Profiler.stopPreciseCoverage');
await postSession('Profiler.disable');
};
// ==================================================================
const collectV8Coverage = async (postSession) => {
let coverageList = await takeV8Coverage(postSession);
if (!coverageList) {
return;
}
// filter node internal files
coverageList = coverageList.filter((entry) => entry.url && entry.url.startsWith('file:'));
// console.log(coverageList);
coverageList = coverageList.filter((entry) => entry.url.includes('test/mock/node'));
// attach source content
coverageList.forEach((item) => {
const filePath = fileURLToPath(item.url);
if (fs.existsSync(filePath)) {
item.source = fs.readFileSync(filePath).toString('utf8');
} else {
EC.logRed('not found file', filePath);
}
});
// console.log(coverageList);
console.log('add node.js coverage ...');
await MCR(coverageOptions).add(coverageList);
};
const generate = async () => {
// clean cache first
await MCR(coverageOptions).cleanCache();
// =====================================================
const postSession = await startV8Coverage();
// =====================================================
require('./specs/node.test.js');
// =====================================================
await collectV8Coverage(postSession);
await stopV8Coverage(postSession);
// =====================================================
const coverageResults = await MCR(coverageOptions).generate();
console.log('test-node-ins coverage reportPath', EC.magenta(coverageResults.reportPath));
};
generate();