monocart-coverage-reports 2.5.8 → 2.5.9

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.
package/README.md CHANGED
@@ -48,15 +48,22 @@
48
48
  ## Usage
49
49
  ```js
50
50
  const MCR = require('monocart-coverage-reports');
51
- const options = {
51
+ const coverageOptions = {
52
+ name: 'My Coverage Report - 2024-02-28',
52
53
  outputDir: './coverage-reports',
53
- reports: "v8"
54
+ reports: ["v8", "console-details"]
54
55
  }
55
- const coverageReport = MCR(options);
56
+ const coverageReport = MCR(coverageOptions);
57
+ coverageReport.cleanCache();
58
+
56
59
  await coverageReport.add(coverageData1);
57
60
  await coverageReport.add(coverageData2);
58
- const coverageResults = await coverageReport.generate();
59
- console.log(coverageResults.summary);
61
+
62
+ await coverageReport.generate();
63
+
64
+ // Or
65
+ // const { CoverageReport } = require('monocart-coverage-reports');
66
+ // const coverageReport = new CoverageReport(coverageOptions);
60
67
  ```
61
68
  - [example v8](https://github.com/cenfun/monocart-coverage-reports/blob/main/test/test-v8.js)
62
69
  - [example istanbul](https://github.com/cenfun/monocart-coverage-reports/blob/main/test/test-istanbul.js)
@@ -134,7 +141,7 @@ console.log(coverageResults.summary);
134
141
  ### Multiple Reports:
135
142
  ```js
136
143
  const MCR = require('monocart-coverage-reports');
137
- const options = {
144
+ const coverageOptions = {
138
145
  outputDir: './coverage-reports',
139
146
  reports: [
140
147
  // build-in reports
@@ -153,14 +160,15 @@ const options = {
153
160
  ["custom-reporter-1"],
154
161
  ["custom-reporter-2", {
155
162
  type: "istanbul",
156
- option: "value"
163
+ key: "value"
157
164
  }],
158
165
  // Specify reporter name with local path
159
166
  ['/absolute/path/to/custom-reporter.js']
160
167
 
161
168
  ]
162
169
  }
163
- const coverageReport = MCR(options);
170
+ const coverageReport = MCR(coverageOptions);
171
+ coverageReport.cleanCache();
164
172
  ```
165
173
 
166
174
  ## Using `entryFilter` and `sourceFilter` to filter the results for V8 report
@@ -419,34 +427,40 @@ Possible solutions:
419
427
  > The data will be added to `[outputDir]/.cache`, After the generation of the report, this data will be removed unless debugging has been enabled or a raw report has been used, see [Debug for Coverage and Sourcemap](#debug-for-coverage-and-sourcemap)
420
428
  - Main process, before the start of testing
421
429
  ```js
422
- // clean previous cache before the start of testing (option)
423
430
  const MCR = require('monocart-coverage-reports');
424
- const options = require('path-to/same-options.js');
425
- MCR(options).cleanCache();
431
+ const coverageOptions = require('path-to/same-options.js');
432
+ const coverageReport = MCR(coverageOptions);
433
+ // clean previous cache before the start of testing
434
+ // unless the running environment is new and no cache
435
+ coverageReport.cleanCache();
426
436
  ```
427
437
 
428
438
  - Sub process 1, testing stage 1
429
439
  ```js
430
440
  const MCR = require('monocart-coverage-reports');
431
- const options = require('path-to/same-options.js');
432
- await MCR(options).add(coverageData1);
441
+ const coverageOptions = require('path-to/same-options.js');
442
+ const coverageReport = MCR(coverageOptions);
443
+ // do not clean cache in the stage
444
+ await coverageReport.add(coverageData1);
433
445
  ```
434
446
 
435
447
  - Sub process 2, testing stage 2
436
448
  ```js
437
449
  const MCR = require('monocart-coverage-reports');
438
- const options = require('path-to/same-options.js');
439
- await MCR(options).add(coverageData2);
450
+ const coverageOptions = require('path-to/same-options.js');
451
+ const coverageReport = MCR(coverageOptions);
452
+ // do not clean cache in the stage
453
+ await coverageReport.add(coverageData2);
440
454
  ```
441
455
 
442
456
  - Main process, after the completion of testing
443
457
  ```js
444
458
  // generate coverage reports after the completion of testing
445
459
  const MCR = require('monocart-coverage-reports');
446
- const options = require('path-to/same-options.js');
447
- const coverageReport = MCR(options);
448
- const coverageResults = await coverageReport.generate();
449
- console.log(coverageResults.summary);
460
+ const coverageOptions = require('path-to/same-options.js');
461
+ const coverageReport = MCR(coverageOptions);
462
+ // do not clean cache before generating reports
463
+ await coverageReport.generate();
450
464
  ```
451
465
 
452
466
  ## Merge Coverage Reports
@@ -473,6 +487,7 @@ const coverageOptions = {
473
487
  ```
474
488
  Then, after all the tests are completed, generate a merged report with option `inputDir`:
475
489
  ```js
490
+ // esm syntax
476
491
  import fs from "fs";
477
492
  import { CoverageReport } from 'monocart-coverage-reports';
478
493
  const coverageOptions = {
@@ -697,6 +697,9 @@ const handleOriginalBytesCoverage = (state, originalStateMap) => {
697
697
  // });
698
698
  }
699
699
 
700
+
701
+ const checkList = [];
702
+
700
703
  // check original bytes if fully in a wrapper range
701
704
  originalStateMap.forEach((originalState) => {
702
705
  const bytes = originalState.bytes;
@@ -727,6 +730,30 @@ const handleOriginalBytesCoverage = (state, originalStateMap) => {
727
730
  // console.log('===========================================================', originalState.sourcePath);
728
731
  // console.log(originalState.source.length, endMapping);
729
732
 
733
+ checkList.push({
734
+ originalState,
735
+ startOffset,
736
+ endOffset
737
+ });
738
+
739
+ });
740
+
741
+ if (!checkList.length) {
742
+ return;
743
+ }
744
+
745
+ // there is no state.bytes if not in debug
746
+ // should using coverageList to generate bytes first
747
+ handleBytesCoverage(state);
748
+
749
+ checkList.forEach((it) => {
750
+
751
+ const {
752
+ originalState, startOffset, endOffset
753
+ } = it;
754
+
755
+ // console.log('=============', 'no bytes', originalState.sourcePath);
756
+
730
757
  // only check uncovered range
731
758
  // because a uncovered range could be in a covered wrapper
732
759
  // { start: 0, end: 12137, count: 1 }, could be { start: > 0, end: < 12137, count: 0 }
@@ -735,7 +762,8 @@ const handleOriginalBytesCoverage = (state, originalStateMap) => {
735
762
  continue;
736
763
  }
737
764
  if (startOffset >= range.start && endOffset <= range.end) {
738
- bytes.push({
765
+ // console.log('------------', 'added');
766
+ originalState.bytes.push({
739
767
  start: 0,
740
768
  end: originalState.source.length,
741
769
  count: 0
@@ -746,6 +774,7 @@ const handleOriginalBytesCoverage = (state, originalStateMap) => {
746
774
 
747
775
  });
748
776
 
777
+
749
778
  };
750
779
 
751
780
  // ========================================================================================================
@@ -72,9 +72,12 @@ module.exports = {
72
72
  // {object} { bytes:[50,80], statements:[50,80], branches:[50,80], functions:[50,80], lines:[50,80] }
73
73
  watermarks: [50, 80],
74
74
 
75
- // {boolean} clean previous files in output dir before generating report. Defaults to true.
75
+ // {boolean} Indicates whether to clean previous files in output dir before generating report. Defaults to true.
76
76
  clean: true,
77
77
 
78
+ // {boolean} Indicates whether to clean previous cache in output dir before generating report. Defaults to false.
79
+ cleanCache: false,
80
+
78
81
  // {function} onEnd hook
79
82
  // onEnd: async (reportData) => {}
80
83
  onEnd: null
package/lib/index.d.ts CHANGED
@@ -280,9 +280,16 @@ declare namespace MCR {
280
280
  * {object} { bytes:[50,80], statements:[50,80], branches:[50,80], functions:[50,80], lines:[50,80] } */
281
281
  watermarks?: Watermarks;
282
282
 
283
- /** {boolean} clean previous files in output dir before generating report. Defaults to true. */
283
+ /** {boolean} Indicates whether to clean previous files in output dir before generating report. Defaults to true. */
284
284
  clean?: boolean;
285
285
 
286
+ /**
287
+ * {boolean} Indicates whether to clean previous cache in output dir before generating report. Defaults to false.
288
+ *
289
+ * If true, the API `cleanCache()` will execute automatically.
290
+ */
291
+ cleanCache?: boolean;
292
+
286
293
  /** {function} onEnd hook */
287
294
  onEnd?: (reportData: CoverageResults) => Promise<void>;
288
295
  }
package/lib/index.js CHANGED
@@ -14,6 +14,13 @@ class CoverageReport {
14
14
  ... defaultOptions,
15
15
  ... options
16
16
  };
17
+
18
+ this.initOptions();
19
+ // If true, the API `cleanCache()` will execute automatically.
20
+ if (this.options.cleanCache) {
21
+ this.cleanCache();
22
+ }
23
+
17
24
  }
18
25
 
19
26
  initOptions() {
@@ -101,11 +101,22 @@ const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) =>
101
101
  return coverageData;
102
102
  }
103
103
 
104
+ // console.log(fileSources);
105
+
104
106
  const newCoverage = {};
105
107
  Object.keys(coverageData).forEach((sourcePath) => {
106
108
  // previous coverage and source
107
109
  const item = coverageData[sourcePath];
108
- const source = fileSources[sourcePath];
110
+ let source = fileSources[sourcePath];
111
+
112
+ // source could be empty string ''
113
+ if (typeof source !== 'string') {
114
+ // read source with original source path first
115
+ source = Util.readFileSync(sourcePath);
116
+ if (typeof source === 'string') {
117
+ fileSources[sourcePath] = source;
118
+ }
119
+ }
109
120
 
110
121
  // new source path, second is source url
111
122
  const newSourcePath = sourcePathHandler(sourcePath, '');
@@ -117,11 +128,14 @@ const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) =>
117
128
  item.data.path = sourcePath;
118
129
  newCoverage[sourcePath] = item;
119
130
  // update source for the new path
120
- if (source) {
131
+ if (typeof source === 'string') {
121
132
  fileSources[sourcePath] = source;
122
133
  }
123
134
  });
124
135
 
136
+ // console.log(Object.keys(fileSources));
137
+ // console.log(newCoverage);
138
+
125
139
  return newCoverage;
126
140
  };
127
141
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monocart-coverage-reports",
3
- "version": "2.5.8",
3
+ "version": "2.5.9",
4
4
  "description": "Monocart coverage reports",
5
5
  "main": "./lib/index.js",
6
6
  "bin": {
@@ -36,6 +36,7 @@
36
36
  "test-css": "node ./test/test-css.js",
37
37
  "test-cli": "npx mcr \"node ./test/test-node-env.js\" -o docs/cli -c test/cli-options.js",
38
38
  "test-merge": "node ./test/test-merge.js",
39
+ "test-merge-istanbul": "node ./test/test-merge-istanbul.js",
39
40
  "test-all": "npm run test-browser && npm run test-node && npm run test-cli && npm run test-merge && npm run build-docs",
40
41
  "test": "npx mcr \"npm run test-all\" -c test/mcr-options.js",
41
42
  "dev": "sf d v8",