monocart-reporter 1.7.0 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +8 -12
  2. package/lib/cli.js +1 -1
  3. package/lib/common.js +2 -3
  4. package/lib/default/options.js +4 -3
  5. package/lib/generate-data.js +2 -7
  6. package/lib/generate-report.js +10 -11
  7. package/lib/index.d.ts +2 -9
  8. package/lib/index.js +6 -0
  9. package/lib/merge-data.js +7 -6
  10. package/lib/plugins/audit/audit.js +4 -2
  11. package/lib/plugins/comments.js +2 -3
  12. package/lib/plugins/coverage/converter/collect-source-maps.js +194 -0
  13. package/lib/plugins/coverage/converter/converter.js +547 -0
  14. package/lib/plugins/coverage/converter/decode-mappings.js +49 -0
  15. package/lib/plugins/coverage/{v8 → converter}/dedupe.js +8 -1
  16. package/lib/plugins/coverage/converter/find-original-range.js +576 -0
  17. package/lib/plugins/coverage/converter/info-branch.js +30 -0
  18. package/lib/plugins/coverage/converter/info-function.js +29 -0
  19. package/lib/plugins/coverage/converter/info-line.js +20 -0
  20. package/lib/plugins/coverage/converter/position-mapping.js +183 -0
  21. package/lib/plugins/coverage/{coverage-utils.js → converter/source-path.js} +26 -42
  22. package/lib/plugins/coverage/coverage.js +61 -57
  23. package/lib/plugins/coverage/istanbul/istanbul.js +21 -174
  24. package/lib/plugins/coverage/v8/v8.js +22 -30
  25. package/lib/plugins/network/network.js +4 -13
  26. package/lib/plugins/state/client.js +3 -4
  27. package/lib/plugins/state/state.js +6 -3
  28. package/lib/runtime/monocart-code-viewer.js +1 -1
  29. package/lib/runtime/monocart-coverage.js +13 -14
  30. package/lib/runtime/monocart-formatter.js +1 -1
  31. package/lib/runtime/monocart-network.js +1 -1
  32. package/lib/runtime/monocart-reporter.js +1 -1
  33. package/lib/runtime/monocart-v8.js +1 -1
  34. package/lib/runtime/monocart-vendor.js +13 -13
  35. package/lib/utils/util.js +97 -3
  36. package/package.json +5 -6
  37. package/lib/plugins/coverage/v8/position-mapping.js +0 -92
  38. package/lib/plugins/coverage/v8/source-map.js +0 -464
@@ -1,47 +1,22 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const EC = require('eight-colors');
4
3
 
5
4
  const istanbulReports = require('istanbul-reports');
6
5
 
7
6
  const Util = require('../../../utils/util.js');
8
7
  const IstanbulSummary = require('./istanbul-summary.js');
9
8
 
10
- const {
11
- V8toIstanbul,
12
- istanbulLibReport,
13
- istanbulLibCoverage,
14
- convertSourceMap
15
- } = require('../../../runtime/monocart-coverage.js');
16
-
17
- const { initSourceMapRootAndUrl } = require('../coverage-utils.js');
18
-
19
- // const V8toIstanbul = require('v8-to-istanbul');
20
- // const istanbulLibCoverage = require('istanbul-lib-coverage');
21
- // const istanbulLibReport = require('istanbul-lib-report');
9
+ const { istanbulLibReport, istanbulLibCoverage } = require('../../../runtime/monocart-coverage.js');
22
10
 
23
11
  const saveIstanbulReport = (coverageData, fileSources, options) => {
24
12
 
25
- // source path handler
26
- let data = coverageData;
27
- if (typeof options.sourcePath === 'function') {
28
- data = {};
29
- Object.keys(coverageData).forEach((sourcePath) => {
30
- const d = coverageData[sourcePath];
31
- const s = fileSources[sourcePath];
32
- const newSourcePath = options.sourcePath(sourcePath);
33
- if (newSourcePath && newSourcePath !== sourcePath) {
34
- sourcePath = newSourcePath;
35
- // related updates
36
- fileSources[sourcePath] = s;
37
- d.path = sourcePath;
38
- }
39
- data[sourcePath] = d;
40
- });
41
- }
42
-
13
+ const coverageMap = istanbulLibCoverage.createCoverageMap(coverageData);
43
14
 
44
- const coverageMap = istanbulLibCoverage.createCoverageMap(data);
15
+ const { watermarks, defaultSummarizer } = options;
16
+ const istanbulOptions = {
17
+ watermarks,
18
+ defaultSummarizer
19
+ };
45
20
 
46
21
  // https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-lib-report
47
22
  const contextOptions = {
@@ -55,7 +30,7 @@ const saveIstanbulReport = (coverageData, fileSources, options) => {
55
30
  // values can be nested/flat/pkg. Defaults to 'pkg'
56
31
  defaultSummarizer: 'nested',
57
32
 
58
- ... options,
33
+ ... istanbulOptions,
59
34
 
60
35
  dir: options.htmlDir,
61
36
  sourceFinder: (filePath) => {
@@ -90,16 +65,24 @@ const saveIstanbulReport = (coverageData, fileSources, options) => {
90
65
  // create a context for report generation
91
66
  const context = istanbulLibReport.createContext(contextOptions);
92
67
 
93
- const htmlReport = istanbulReports.create('html', {});
94
- htmlReport.execute(context);
95
-
96
- const htmlPath = Util.relativePath(path.resolve(options.htmlDir, 'index.html'));
68
+ // html
69
+ if (options.toIstanbul) {
70
+ let reportName = 'html-spa';
71
+ if (options.toIstanbul === 'html') {
72
+ reportName = 'html';
73
+ }
74
+ const htmlReport = istanbulReports.create(reportName, {});
75
+ htmlReport.execute(context);
76
+ }
97
77
 
78
+ // lcov
98
79
  if (options.lcov) {
99
80
  const lcovReport = istanbulReports.create('lcovonly', {});
100
81
  lcovReport.execute(context);
101
82
  }
102
83
 
84
+ const htmlPath = Util.relativePath(path.resolve(options.htmlDir, 'index.html'));
85
+
103
86
  // add watermarks and color
104
87
  const coverageReport = new IstanbulSummary();
105
88
  coverageReport.execute(context);
@@ -113,142 +96,6 @@ const saveIstanbulReport = (coverageData, fileSources, options) => {
113
96
  return report;
114
97
  };
115
98
 
116
- // ===================================================================================================
117
-
118
- const getConversionSources = (item, fileSources) => {
119
-
120
- const { source, sourceMap } = item;
121
-
122
- fileSources[item.sourcePath] = source;
123
-
124
- const sources = {
125
- // remove map file
126
- source: convertSourceMap.removeComments(source)
127
- };
128
-
129
- if (!sourceMap) {
130
- return sources;
131
- }
132
-
133
- // =======================================================
134
- // append sourceMap
135
-
136
- // 'webpack://monocart-v8/external umd "monocart-code-viewer"'
137
- // format the url to sourcePath
138
-
139
- const fileUrls = {};
140
- initSourceMapRootAndUrl(sourceMap, fileUrls, fileSources);
141
-
142
- // console.log(sourceMap.sources);
143
-
144
- // =======================================================
145
- sources.sourceMap = {
146
- sourcemap: sourceMap
147
- };
148
-
149
- return sources;
150
- };
151
-
152
- const convertV8ToIstanbul = async (v8list, options) => {
153
-
154
- // console.log('v8list before', v8list.map((it) => it.url));
155
-
156
- // only js with source (without css)
157
- v8list = v8list.filter((item) => item.type === 'js');
158
-
159
- // console.log('v8list after', v8list.map((it) => it.url));
160
- // console.log('has map', v8list.filter((it) => it.sourceMap));
161
-
162
- // sourceFilter to excludePath
163
- const excludePath = typeof options.sourceFilter === 'function' ? (sourceName) => {
164
- return !options.sourceFilter(sourceName);
165
- } : () => {
166
- return false;
167
- };
168
-
169
- // file sources for istanbul html report sourceFinder in memory
170
- const fileSources = {};
171
-
172
- // https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-lib-coverage
173
- const coverageMap = istanbulLibCoverage.createCoverageMap();
174
-
175
- for (const item of v8list) {
176
-
177
- const sources = getConversionSources(item, fileSources);
178
-
179
- // console.log('sources', Object.keys(sources));
180
-
181
- const v8toIstanbul = new V8toIstanbul(item.sourcePath, 0, sources, excludePath);
182
- // stop resolve source path, already resolved
183
- v8toIstanbul._resolveSource = (m, p) => {
184
- return p;
185
- };
186
-
187
- let failed = false;
188
- await v8toIstanbul.load().catch((e) => {
189
- EC.logRed(`[MCR] ${item.sourcePath} v8toIstanbul.load:`, e.message);
190
- failed = true;
191
- });
192
-
193
- if (failed) {
194
- continue;
195
- }
196
-
197
- try {
198
- v8toIstanbul.applyCoverage(item.functions);
199
- } catch (e) {
200
- EC.logRed(`[MCR] ${item.sourcePath} v8toIstanbul.applyCoverage:`, e.message);
201
- failed = true;
202
- }
203
-
204
- if (failed) {
205
- continue;
206
- }
207
-
208
- const istanbulData = v8toIstanbul.toIstanbul();
209
- // console.log('istanbulData', istanbulData);
210
-
211
- coverageMap.merge(istanbulData);
212
-
213
- }
214
-
215
- const coverageData = coverageMap.toJSON();
216
-
217
- // console.log('coverageData', Object.keys(coverageData));
218
-
219
- return {
220
- coverageData,
221
- fileSources
222
- };
223
-
224
- };
225
-
226
- // ===================================================================================================
227
-
228
- const mergeIstanbulList = (istanbulList) => {
229
-
230
- const fileSources = {};
231
-
232
- const coverageMap = istanbulLibCoverage.createCoverageMap();
233
- for (const item of istanbulList) {
234
- Object.assign(fileSources, item.fileSources);
235
- coverageMap.merge(item.coverageData);
236
- }
237
-
238
- const coverageData = coverageMap.toJSON();
239
-
240
- // console.log('merged data', coverageData);
241
-
242
- return {
243
- coverageData,
244
- fileSources
245
- };
246
-
247
- };
248
-
249
-
250
99
  module.exports = {
251
- saveIstanbulReport,
252
- convertV8ToIstanbul,
253
- mergeIstanbulList
100
+ saveIstanbulReport
254
101
  };
@@ -1,14 +1,9 @@
1
- const EC = require('eight-colors');
2
-
3
1
  const Util = require('../../../utils/util.js');
4
2
  const { getV8Summary } = require('./v8-summary.js');
5
- const { dedupeRanges } = require('./dedupe.js');
6
- const { getSourcePath, convertFunctionsToRanges } = require('../coverage-utils.js');
7
- const { collectSourceMaps, unpackSourceMaps } = require('./source-map.js');
3
+ const { dedupeRanges } = require('../converter/dedupe.js');
4
+ const { getSourcePath } = require('../converter/source-path.js');
8
5
  const { mergeScriptCovs } = require('../../../runtime/monocart-coverage.js');
9
-
10
-
11
- // ========================================================================================================
6
+ const collectSourceMaps = require('../converter/collect-source-maps.js');
12
7
 
13
8
  const initV8ListAndSourcemap = async (v8list, options, inlineSourceMap) => {
14
9
 
@@ -30,13 +25,15 @@ const initV8ListAndSourcemap = async (v8list, options, inlineSourceMap) => {
30
25
  return true;
31
26
  }
32
27
  // 404 css, text will be empty
33
- // EC.logRed(`[MCR] Invalid source: ${item.url}`);
28
+ // Util.logError(`Invalid source: ${item.url}`);
34
29
  // console.log(item);
35
30
  });
36
31
 
37
32
 
38
33
  // do not change original v8list, to work for multiple APIs
39
34
 
35
+ const sourcePathHandler = options.sourcePath;
36
+
40
37
  // init id, sourcePath
41
38
  v8list = v8list.map((item, i) => {
42
39
 
@@ -57,35 +54,32 @@ const initV8ListAndSourcemap = async (v8list, options, inlineSourceMap) => {
57
54
  }
58
55
 
59
56
  data.id = Util.calculateSha1(data.url + data.source);
60
- data.sourcePath = getSourcePath(data.url, i + 1, data.type);
57
+
58
+ let sourcePath = getSourcePath(data.url, i + 1, data.type);
59
+ if (typeof sourcePathHandler === 'function') {
60
+ const newSourcePath = sourcePathHandler(sourcePath);
61
+ if (typeof newSourcePath === 'string' && newSourcePath) {
62
+ sourcePath = newSourcePath;
63
+ }
64
+ }
65
+
66
+ data.sourcePath = sourcePath;
61
67
 
62
68
  return data;
63
69
  });
64
70
 
65
71
  // collect sourcemap first
66
- await collectSourceMaps(v8list, options, inlineSourceMap);
72
+ const time_start = Date.now();
73
+ const count = await collectSourceMaps(v8list, options, inlineSourceMap);
74
+ if (count) {
75
+ Util.logTime(`loaded ${count} sourcemaps`, time_start);
76
+ }
67
77
 
68
78
  return v8list;
69
79
  };
70
80
 
71
81
  // ========================================================================================================
72
82
 
73
- const unpackV8List = async (v8list, options) => {
74
- v8list.forEach((item, i) => {
75
- if (item.type === 'js') {
76
- item.ranges = convertFunctionsToRanges(item.functions);
77
- delete item.functions;
78
- }
79
- });
80
-
81
- // requires ranges before unpack
82
- await unpackSourceMaps(v8list, options);
83
-
84
- // console.log(v8list.length);
85
- };
86
-
87
- // ========================================================================================================
88
-
89
83
  // force to async
90
84
  const mergeCssRanges = (itemList) => {
91
85
  return new Promise((resolve) => {
@@ -162,7 +156,7 @@ const mergeV8Coverage = async (dataList, options) => {
162
156
  item.source = json.source;
163
157
  item.sourceMap = json.sourceMap;
164
158
  } else {
165
- EC.logRed(`[MCR] failed to read source: ${item.url}`);
159
+ Util.logError(`failed to read source: ${item.url}`);
166
160
  item.source = '';
167
161
  }
168
162
  }
@@ -187,7 +181,6 @@ const saveV8HtmlReport = async (reportData, _options) => {
187
181
 
188
182
  outputDir,
189
183
  reportDataFile: 'coverage-data.js',
190
- assetsName: 'assets',
191
184
  assetsRelative: '../'
192
185
  };
193
186
 
@@ -262,7 +255,6 @@ const saveV8Report = async (v8list, options) => {
262
255
 
263
256
  module.exports = {
264
257
  initV8ListAndSourcemap,
265
- unpackV8List,
266
258
  mergeV8Coverage,
267
259
  saveV8Report
268
260
  };
@@ -1,6 +1,5 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const EC = require('eight-colors');
4
3
  const Util = require('../../utils/util.js');
5
4
 
6
5
  const getHarData = (har) => {
@@ -121,9 +120,12 @@ const saveNetworkHtmlReport = async (reportData, _options) => {
121
120
 
122
121
  const attachNetworkReport = async (har, testInfo, options = {}) => {
123
122
 
123
+ const reporterOptions = Util.resolveReporterOptions(testInfo);
124
+ Util.initLoggingLevel(reporterOptions.logging, 'network');
125
+
124
126
  const harData = getHarData(har);
125
127
  if (!harData || !harData.log) {
126
- EC.logRed(`[MCR] failed to load HAR: ${har}`);
128
+ Util.logError(`failed to load HAR: ${har}`);
127
129
  return;
128
130
  }
129
131
 
@@ -177,17 +179,6 @@ const attachNetworkReport = async (har, testInfo, options = {}) => {
177
179
  return report;
178
180
  };
179
181
 
180
- // TODO
181
- const addNetworkReport = async () => {
182
-
183
- };
184
-
185
- const generateGlobalNetworkReport = async () => {
186
-
187
- };
188
-
189
182
  module.exports = {
190
- addNetworkReport,
191
- generateGlobalNetworkReport,
192
183
  attachNetworkReport
193
184
  };
@@ -1,4 +1,3 @@
1
- const EC = require('eight-colors');
2
1
  const { WebSocket } = require('../../runtime/monocart-vendor.js');
3
2
  const Util = require('../../utils/util.js');
4
3
 
@@ -29,7 +28,7 @@ class Client {
29
28
  });
30
29
  this.resolves = null;
31
30
  } else {
32
- EC.logRed(`[SWS] websocket error: ${err.message}`);
31
+ Util.logError(`websocket error: ${err.message}`);
33
32
  }
34
33
 
35
34
  });
@@ -63,7 +62,7 @@ class Client {
63
62
  reject
64
63
  });
65
64
  } else {
66
- reject(new Error('[SWS] an error occurs when connecting websocket server'));
65
+ reject(new Error('an error occurs when connecting websocket server'));
67
66
  }
68
67
  });
69
68
  }
@@ -108,7 +107,7 @@ class Client {
108
107
  const timeout = this.options.timeout;
109
108
  const timeout_id = setTimeout(() => {
110
109
  this.requests.delete(id);
111
- reject(new Error(`[SWS] Timed out receiving message from websocket server: ${timeout}ms`));
110
+ reject(new Error(`timed out receiving message from websocket server: ${timeout}ms`));
112
111
  }, timeout);
113
112
 
114
113
  this.requests.set(id, {
@@ -40,6 +40,9 @@ const useState = (options = {}) => {
40
40
  ... options
41
41
  };
42
42
 
43
+ // when used in global-setup will be override reporter (main process)
44
+ Util.initLoggingLevel(clientOptions.logging, 'state');
45
+
43
46
  const clientKey = Object.keys(defaultClientOptions).map((k) => clientOptions[k]).join('-');
44
47
  // console.log('client key', clientKey);
45
48
 
@@ -142,10 +145,10 @@ const createStateServer = (stateOptions) => {
142
145
  const wss = new WebSocketServer(serverOptions);
143
146
 
144
147
  wss.on('error', (e) => {
145
- EC.logRed(`[SWS] websocket server error: ${e.message}`);
148
+ Util.logError(`websocket server error: ${e.message}`);
146
149
  });
147
150
  wss.on('wsClientError', (e) => {
148
- EC.logRed(`[SWS] websocket client error: ${e.message}`);
151
+ Util.logError(`websocket client error: ${e.message}`);
149
152
  });
150
153
 
151
154
  wss.on('connection', (ws) => {
@@ -159,7 +162,7 @@ const createStateServer = (stateOptions) => {
159
162
 
160
163
  wss.on('listening', () => {
161
164
  const serverUrl = getServerUrl(serverOptions);
162
- console.log(`[SWS] state websocket server listening on ${EC.cyan(serverUrl)}`);
165
+ Util.logInfo(`state websocket server listening on ${EC.cyan(serverUrl)}`);
163
166
  });
164
167
 
165
168
  return {