monocart-coverage-reports 2.8.2 → 2.8.4

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
@@ -39,6 +39,7 @@
39
39
  * [Integration with Any Testing Framework](#integration-with-any-testing-framework)
40
40
  * [Integration Examples](#integration-examples)
41
41
  - [Playwright](#playwright)
42
+ - [c8](#c8)
42
43
  - [CodeceptJS](#codeceptjs)
43
44
  - [Jest](#jest)
44
45
  - [Vitest](#vitest)
@@ -914,7 +915,6 @@ When `logging` is `debug`, the raw report data will be preserved in `[outputDir]
914
915
  - [playwright-coverage](https://github.com/cenfun/playwright-coverage) - Example for Playwright coverage reports
915
916
  - [playwright-bdd-coverage](https://github.com/cenfun/playwright-bdd-coverage) - Example for Playwright BDD coverage reports
916
917
  - [monocart-reporter](https://github.com/cenfun/monocart-reporter) - Playwright custom reporter, supports generating [Code coverage report](https://github.com/cenfun/monocart-reporter?#code-coverage-report)
917
- - [merge-code-coverage](https://github.com/cenfun/merge-code-coverage) - Example for merging code coverage (unit + e2e shard)
918
918
  - Coverage for component testing with `monocart-reporter`:
919
919
  - [playwright-ct-vue](https://github.com/cenfun/playwright-ct-vue)
920
920
  - [playwright-ct-react](https://github.com/cenfun/playwright-ct-react)
@@ -924,14 +924,22 @@ When `logging` is `debug`, the raw report data will be preserved in `[outputDir]
924
924
  - [nextjs-with-playwright-istanbul](https://github.com/cenfun/nextjs-with-playwright-istanbul)
925
925
  - see [Collecting V8 Coverage Data with Playwright](#collecting-v8-coverage-data-with-playwright)
926
926
 
927
+ ### [c8](https://github.com/bcoe/c8)
928
+ - c8 has integrated `MCR` as an experimental feature since [v10.1.0](https://github.com/bcoe/c8/releases/tag/v10.1.0)
929
+ ```sh
930
+ c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js
931
+ ```
932
+
927
933
  ### [CodeceptJS](https://github.com/codeceptjs/CodeceptJS)
928
- - CodeceptJS is a [BDD](https://codecept.io/bdd/) + [AI](https://codecept.io/ai/) testing framework for e2e testing, it has integrated `MCR` since [v3.5.15](https://github.com/codeceptjs/CodeceptJS/releases/tag/3.5.15), see [plugins/coverage](https://codecept.io/plugins/#coverage). There's no need to use ~~[codeceptjs-monocart-coverage](https://github.com/cenfun/codeceptjs-monocart-coverage)~~ anymore.
934
+ - CodeceptJS is a [BDD](https://codecept.io/bdd/) + [AI](https://codecept.io/ai/) testing framework for e2e testing, it has integrated `MCR` since [v3.5.15](https://github.com/codeceptjs/CodeceptJS/releases/tag/3.5.15), see [plugins/coverage](https://codecept.io/plugins/#coverage)
929
935
 
930
936
  ### [Jest](https://github.com/jestjs/jest/)
931
937
  - [jest-monocart-coverage](https://github.com/cenfun/jest-monocart-coverage) - Jest custom reporter for coverage reports
938
+ - [merge-code-coverage](https://github.com/cenfun/merge-code-coverage) - Example for merging code coverage (Jest unit + Playwright e2e sharding)
932
939
 
933
940
  ### [Vitest](https://github.com/vitest-dev/vitest)
934
941
  - [vitest-monocart-coverage](https://github.com/cenfun/vitest-monocart-coverage) - Vitest custom provider module for coverage reports
942
+ - [merge-code-coverage-vitest](https://github.com/cenfun/merge-code-coverage-vitest) - Example for merging code coverage (Vitest unit + Playwright e2e sharding)
935
943
 
936
944
  ### [Puppeteer](https://github.com/puppeteer/puppeteer/)
937
945
  - [jest-puppeteer-coverage](https://github.com/cenfun/jest-puppeteer-coverage) - Example for Jest puppeteer coverage
@@ -1062,6 +1070,13 @@ npm run test
1062
1070
 
1063
1071
  npm run dev
1064
1072
  ```
1073
+ - Refreshing `eol=lf` for snapshot of test (Windows)
1074
+ ```sh
1075
+ git add . -u
1076
+ git commit -m "Saving files before refreshing line endings"
1077
+
1078
+ npm run eol
1079
+ ```
1065
1080
 
1066
1081
  ### VSCode Extension
1067
1082
  - [Coverage Gutters](https://github.com/ryanluker/vscode-coverage-gutters) - Display test coverage generated by lcov or xml in VSCode editor.
package/README.zh-Hans.md CHANGED
@@ -39,6 +39,7 @@
39
39
  * [如何跟其他框架集成](#integration-with-any-testing-framework)
40
40
  * [集成的例子](#integration-examples)
41
41
  - [Playwright](#playwright)
42
+ - [c8](#c8)
42
43
  - [CodeceptJS](#codeceptjs)
43
44
  - [Jest](#jest)
44
45
  - [Vitest](#vitest)
@@ -918,7 +919,6 @@ const coverageOptions = {
918
919
  - [playwright-coverage](https://github.com/cenfun/playwright-coverage) - Example for Playwright coverage reports
919
920
  - [playwright-bdd-coverage](https://github.com/cenfun/playwright-bdd-coverage) - Example for Playwright BDD coverage reports
920
921
  - [monocart-reporter](https://github.com/cenfun/monocart-reporter) - Playwright custom reporter, supports generating [Code coverage report](https://github.com/cenfun/monocart-reporter?#code-coverage-report)
921
- - [merge-code-coverage](https://github.com/cenfun/merge-code-coverage) - Example for merging code coverage (unit + e2e shard)
922
922
  - Coverage for component testing with `monocart-reporter`:
923
923
  - [playwright-ct-vue](https://github.com/cenfun/playwright-ct-vue)
924
924
  - [playwright-ct-react](https://github.com/cenfun/playwright-ct-react)
@@ -928,14 +928,22 @@ const coverageOptions = {
928
928
  - [nextjs-with-playwright-istanbul](https://github.com/cenfun/nextjs-with-playwright-istanbul)
929
929
  - see [Collecting V8 Coverage Data with Playwright](#collecting-v8-coverage-data-with-playwright)
930
930
 
931
+ ### [c8](https://github.com/bcoe/c8)
932
+ - c8 has integrated `MCR` as an experimental feature since [v10.1.0](https://github.com/bcoe/c8/releases/tag/v10.1.0)
933
+ ```sh
934
+ c8 --experimental-monocart --reporter=v8 --reporter=console-details node foo.js
935
+ ```
936
+
931
937
  ### [CodeceptJS](https://github.com/codeceptjs/CodeceptJS)
932
- - CodeceptJS is a [BDD](https://codecept.io/bdd/) + [AI](https://codecept.io/ai/) testing framework for e2e testing, it has integrated `MCR` since [v3.5.15](https://github.com/codeceptjs/CodeceptJS/releases/tag/3.5.15), see [plugins/coverage](https://codecept.io/plugins/#coverage). There's no need to use ~~[codeceptjs-monocart-coverage](https://github.com/cenfun/codeceptjs-monocart-coverage)~~ anymore.
938
+ - CodeceptJS is a [BDD](https://codecept.io/bdd/) + [AI](https://codecept.io/ai/) testing framework for e2e testing, it has integrated `MCR` since [v3.5.15](https://github.com/codeceptjs/CodeceptJS/releases/tag/3.5.15), see [plugins/coverage](https://codecept.io/plugins/#coverage)
933
939
 
934
940
  ### [Jest](https://github.com/jestjs/jest/)
935
941
  - [jest-monocart-coverage](https://github.com/cenfun/jest-monocart-coverage) - Jest custom reporter for coverage reports
942
+ - [merge-code-coverage](https://github.com/cenfun/merge-code-coverage) - Example for merging code coverage (Jest unit + Playwright e2e sharding)
936
943
 
937
944
  ### [Vitest](https://github.com/vitest-dev/vitest)
938
945
  - [vitest-monocart-coverage](https://github.com/cenfun/vitest-monocart-coverage) - Vitest custom provider module for coverage reports
946
+ - [merge-code-coverage-vitest](https://github.com/cenfun/merge-code-coverage-vitest) - Example for merging code coverage (Vitest unit + Playwright e2e sharding)
939
947
 
940
948
  ### [Puppeteer](https://github.com/puppeteer/puppeteer/)
941
949
  - [jest-puppeteer-coverage](https://github.com/cenfun/jest-puppeteer-coverage) - Example for Jest puppeteer coverage
@@ -1066,6 +1074,13 @@ npm run test
1066
1074
 
1067
1075
  npm run dev
1068
1076
  ```
1077
+ - Refreshing `eol=lf` for snapshot of test (Windows)
1078
+ ```sh
1079
+ git add . -u
1080
+ git commit -m "Saving files before refreshing line endings"
1081
+
1082
+ npm run eol
1083
+ ```
1069
1084
 
1070
1085
  ### VSCode Extension
1071
1086
  - [Coverage Gutters](https://github.com/ryanluker/vscode-coverage-gutters) - Display test coverage generated by lcov or xml in VSCode editor.
package/lib/cli.js CHANGED
@@ -6,7 +6,7 @@ const EC = require('eight-colors');
6
6
  const {
7
7
  foregroundChild,
8
8
  program
9
- } = require('./packages/monocart-coverage-vendor.js');
9
+ } = require('./packages/monocart-coverage-cli.js');
10
10
 
11
11
  const MCR = require('./index.js');
12
12
  const Util = require('./utils/util.js');
@@ -32,10 +32,10 @@ const getParentCount = (reverseParents, functionCount) => {
32
32
  return functionCount;
33
33
  };
34
34
 
35
- const getFunctionRange = (start, end, coverageInfo) => {
35
+ const getFunctionRange = (start, end, type, coverageInfo) => {
36
36
 
37
37
  const {
38
- functionMap, functionNameMap, functionUncoveredRanges
38
+ functionMap, functionNameMap, functionStaticRanges, functionUncoveredRanges
39
39
  } = coverageInfo;
40
40
 
41
41
  // exact matched in functionMap
@@ -49,6 +49,13 @@ const getFunctionRange = (start, end, coverageInfo) => {
49
49
  return nameRange;
50
50
  }
51
51
 
52
+ if (type === 'StaticBlock' && functionStaticRanges.length) {
53
+ const staticRange = Util.findInRanges(start, end, functionStaticRanges, 'startOffset', 'endOffset');
54
+ if (staticRange) {
55
+ return staticRange;
56
+ }
57
+ }
58
+
52
59
  // find in uncoveredRanges
53
60
  return Util.findInRanges(start, end, functionUncoveredRanges, 'startOffset', 'endOffset');
54
61
  };
@@ -283,8 +290,10 @@ const updateBranchCount = (group) => {
283
290
  if (none) {
284
291
  return;
285
292
  }
293
+
286
294
  item.block = getFunctionBlock(start, end, functionState);
287
295
  setGeneratedOnly(item.block, group.generatedOnly);
296
+
288
297
  });
289
298
 
290
299
 
@@ -464,6 +473,15 @@ const collectNodes = (ast) => {
464
473
  });
465
474
  },
466
475
 
476
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
477
+ // as a function "functionName": "<static_initializer>",
478
+ StaticBlock: (node, parents) => {
479
+ const reverseParents = [].concat(parents).reverse();
480
+ functionNodes.push({
481
+ node,
482
+ reverseParents
483
+ });
484
+ },
467
485
 
468
486
  // ===============================================================================
469
487
  // functions
@@ -617,10 +635,12 @@ const getRootFunctionState = (ast, coverageInfo) => {
617
635
  const findFunctionRange = (item, coverageInfo) => {
618
636
 
619
637
  const { node, reverseParents } = item;
620
- const { start, end } = node;
638
+ const {
639
+ start, end, type
640
+ } = node;
621
641
 
622
642
  // try function start/end
623
- const functionRange = getFunctionRange(start, end, coverageInfo);
643
+ const functionRange = getFunctionRange(start, end, type, coverageInfo);
624
644
  if (functionRange) {
625
645
  return functionRange;
626
646
  }
@@ -629,7 +649,7 @@ const findFunctionRange = (item, coverageInfo) => {
629
649
  // 0 is function self
630
650
  const parent = reverseParents[1];
631
651
  if (parent && parent.type === 'MethodDefinition') {
632
- return getFunctionRange(parent.start, parent.end, coverageInfo);
652
+ return getFunctionRange(parent.start, parent.end, parent.type, coverageInfo);
633
653
  }
634
654
 
635
655
  };
@@ -8,7 +8,9 @@ const { collectAstInfo } = require('./ast-visitor.js');
8
8
  const getCoverageInfo = (coverageList) => {
9
9
  const functionMap = new Map();
10
10
  const functionNameMap = new Map();
11
+ const functionStaticRanges = [];
11
12
  const functionUncoveredRanges = [];
13
+
12
14
  let rootRange;
13
15
  coverageList.forEach((block) => {
14
16
  const {
@@ -38,9 +40,7 @@ const getCoverageInfo = (coverageList) => {
38
40
  functionRange.blockMap = blockMap;
39
41
  // uncovered is unique
40
42
  const blockUncoveredRanges = blockRanges.filter((it) => it.count === 0);
41
- blockUncoveredRanges.sort((a, b) => {
42
- return a.startOffset - b.startOffset;
43
- });
43
+ Util.sortOffsetRanges(blockUncoveredRanges);
44
44
  functionRange.blockUncoveredRanges = blockUncoveredRanges;
45
45
  functionRange.blockCoveredRanges = blockRanges.filter((it) => it.count > 0);
46
46
  }
@@ -61,7 +61,11 @@ const getCoverageInfo = (coverageList) => {
61
61
  functionNameMap.set(functionRange.startOffset + functionName.length, functionRange);
62
62
  }
63
63
 
64
- // uncovered is unique
64
+ if (functionName === '<static_initializer>') {
65
+ functionStaticRanges.push(functionRange);
66
+ }
67
+
68
+ // cache uncovered
65
69
  if (functionRange.count === 0) {
66
70
  functionUncoveredRanges.push(functionRange);
67
71
  }
@@ -69,13 +73,12 @@ const getCoverageInfo = (coverageList) => {
69
73
  });
70
74
 
71
75
  // sort ranges
72
- functionUncoveredRanges.sort((a, b) => {
73
- return a.startOffset - b.startOffset;
74
- });
76
+ Util.sortOffsetRanges(functionUncoveredRanges);
75
77
 
76
78
  return {
77
79
  functionMap,
78
80
  functionNameMap,
81
+ functionStaticRanges,
79
82
  functionUncoveredRanges,
80
83
  rootRange
81
84
  };
@@ -49,7 +49,7 @@ const logMappingErrors = (type, result) => {
49
49
 
50
50
  const handleIgnoredRanges = (list, ignoredRanges) => {
51
51
  list.forEach((item) => {
52
- const range = Util.findInRanges(item.start, item.end, ignoredRanges, 'start', 'end');
52
+ const range = Util.findInRanges(item.start, item.end, ignoredRanges);
53
53
  if (range) {
54
54
  // console.log(item, range);
55
55
  item.ignored = true;
@@ -74,32 +74,7 @@ const applyBytesToLines = (bytes, locator, lineMap) => {
74
74
 
75
75
  // update lines coverage
76
76
  const lines = Util.getRangeLines(sLoc, eLoc);
77
-
78
- lines.forEach((it) => {
79
- const lineItem = lineMap.get(it.line);
80
- if (!lineItem) {
81
- // not found line, could be comment or blank line
82
- return;
83
- }
84
- if (lineItem.ignored) {
85
- return;
86
- }
87
-
88
- it.count = count;
89
-
90
- // default is covered, so only focus on
91
- // 1, biggest covered count
92
- // 2, uncovered entire and pieces
93
- if (count > 0) {
94
- lineItem.coveredCount = Math.max(lineItem.coveredCount, count);
95
- } else {
96
- if (it.entire) {
97
- lineItem.uncoveredEntire = it;
98
- } else {
99
- lineItem.uncoveredPieces.push(it);
100
- }
101
- }
102
- });
77
+ Util.updateLinesCoverage(lines, count, lineMap);
103
78
 
104
79
  });
105
80
 
@@ -1237,6 +1212,84 @@ const filterCoverageList = (item) => {
1237
1212
  return coverageList;
1238
1213
  };
1239
1214
 
1215
+ const initJsCoverageList = (item) => {
1216
+ const coverageList = filterCoverageList(item);
1217
+
1218
+ // function could be covered even it is defined after an uncovered return, see case closures.js
1219
+ // fix uncovered range if there are covered ranges in uncovered range
1220
+
1221
+ const uncoveredBlocks = [];
1222
+ const uncoveredList = [];
1223
+ coverageList.forEach((block) => {
1224
+ block.ranges.forEach((range, i) => {
1225
+ const {
1226
+ count, startOffset, endOffset
1227
+ } = range;
1228
+
1229
+ if (i === 0) {
1230
+ // check only first level
1231
+ if (count > 0) {
1232
+ const inUncoveredRange = Util.findInRanges(startOffset, endOffset, uncoveredBlocks, 'startOffset', 'endOffset');
1233
+ if (inUncoveredRange) {
1234
+ if (!inUncoveredRange.coveredList) {
1235
+ inUncoveredRange.coveredList = [];
1236
+ uncoveredList.push(inUncoveredRange);
1237
+ }
1238
+ inUncoveredRange.coveredList.push(range);
1239
+ }
1240
+ }
1241
+ } else {
1242
+ if (count === 0) {
1243
+ uncoveredBlocks.push({
1244
+ ... range,
1245
+ index: i,
1246
+ ranges: block.ranges
1247
+ });
1248
+ }
1249
+ }
1250
+ });
1251
+ });
1252
+
1253
+ if (uncoveredList.length) {
1254
+
1255
+ uncoveredList.forEach((it) => {
1256
+ const {
1257
+ ranges, index, count, coveredList
1258
+ } = it;
1259
+
1260
+ // remove previous range first
1261
+ const args = [index, 1];
1262
+
1263
+ Util.sortOffsetRanges(coveredList);
1264
+ let startOffset = it.startOffset;
1265
+ coveredList.forEach((cov) => {
1266
+ // ignore sub functions in the function
1267
+ if (cov.startOffset > startOffset) {
1268
+ args.push({
1269
+ startOffset,
1270
+ endOffset: cov.startOffset,
1271
+ count
1272
+ });
1273
+ startOffset = cov.endOffset;
1274
+ }
1275
+ });
1276
+
1277
+ if (it.endOffset > startOffset) {
1278
+ args.push({
1279
+ startOffset,
1280
+ endOffset: it.endOffset,
1281
+ count
1282
+ });
1283
+ }
1284
+
1285
+ ranges.splice.apply(ranges, args);
1286
+
1287
+ });
1288
+ }
1289
+
1290
+ return coverageList;
1291
+ };
1292
+
1240
1293
  // ========================================================================================================
1241
1294
 
1242
1295
  const isCoveredInRanges = (range, uncoveredBytes) => {
@@ -1464,7 +1517,7 @@ const convertV8List = (v8list, options) => {
1464
1517
  let coverageList = [];
1465
1518
  let astInfo;
1466
1519
  if (js) {
1467
- coverageList = filterCoverageList(item);
1520
+ coverageList = initJsCoverageList(item);
1468
1521
  // remove original functions
1469
1522
  if (!Util.isDebug()) {
1470
1523
  delete item.functions;
package/lib/index.d.ts CHANGED
@@ -206,10 +206,11 @@ declare namespace MCR {
206
206
  files: CoverageFile[];
207
207
  } | undefined;
208
208
 
209
+ export type LoggingType = "off" | "error" | "info" | "debug";
209
210
  export interface CoverageReportOptions {
210
211
 
211
212
  /** {string} logging levels: off, error, info, debug */
212
- logging?: string;
213
+ logging?: LoggingType;
213
214
 
214
215
  /** {string} Report name. Defaults to "Coverage Report". */
215
216
  name?: string;
@@ -222,7 +223,7 @@ declare namespace MCR {
222
223
  *
223
224
  * By default, `v8` for V8 data, `html` for Istanbul data
224
225
  */
225
- reports?: string | string[] | ReportDescription[];
226
+ reports?: string | (string | ReportDescription)[];
226
227
 
227
228
  /** {string} output dir */
228
229
  outputDir?: string;
@@ -494,6 +495,10 @@ declare namespace MCR {
494
495
 
495
496
  export function CDPClient(cdpOptions: CDPOptions): Promise<CoverageClient | undefined>;
496
497
 
498
+ export const Util: {
499
+ initLoggingLevel: (logging: LoggingType) => string;
500
+ };
501
+
497
502
  }
498
503
 
499
504
  /** create coverage report */
package/lib/index.js CHANGED
@@ -279,5 +279,6 @@ MCR.getSnapshot = getSnapshot;
279
279
  MCR.diffSnapshot = diffSnapshot;
280
280
  MCR.WSSession = WSSession;
281
281
  MCR.CDPClient = CDPClient;
282
+ MCR.Util = Util;
282
283
 
283
284
  module.exports = MCR;
package/lib/index.mjs CHANGED
@@ -6,3 +6,4 @@ export const diffSnapshot = MCR.diffSnapshot;
6
6
 
7
7
  export const WSSession = MCR.WSSession;
8
8
  export const CDPClient = MCR.CDPClient;
9
+ export const Util = MCR.Util;