monocart-reporter 1.6.14 → 1.6.15
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 +16 -14
- package/lib/helper/network.js +40 -0
- package/lib/index.js +8 -2
- package/lib/index.mjs +6 -1
- package/lib/runtime/monocart-reporter.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
* [Style Tags](#style-tags)
|
|
24
24
|
* [Metadata](#metadata)
|
|
25
25
|
* [Trend Chart](#trend-chart)
|
|
26
|
-
* [Code Coverage
|
|
26
|
+
* [Attach Code Coverage Report](#attach-code-coverage-report)
|
|
27
27
|
* [Merge Shard Reports](#merge-shard-reports)
|
|
28
28
|
* [onEnd hook](#onend-hook)
|
|
29
29
|
- [Send Email](#send-email)
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
|
|
47
47
|
## Install
|
|
48
48
|
```sh
|
|
49
|
-
npm i monocart-reporter
|
|
49
|
+
npm i -D monocart-reporter
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
## Playwright Config
|
|
@@ -524,16 +524,16 @@ module.exports = {
|
|
|
524
524
|
};
|
|
525
525
|
```
|
|
526
526
|
|
|
527
|
-
## Code Coverage
|
|
528
|
-
Using `MonocartReporter.
|
|
529
|
-
- [V8](https://playwright.dev/docs/api/class-coverage) (Chromium-based only)
|
|
527
|
+
## Attach Code Coverage Report
|
|
528
|
+
Using `MonocartReporter.attachCoverageReport()` API to generate coverage report during the test. There are 2 supported data inputs: `V8` and `istanbul` (see example: [coverage.spec.js](https://github.com/cenfun/monocart-reporter-test/blob/main/tests/coverage/coverage.spec.js))
|
|
529
|
+
- [V8](https://playwright.dev/docs/api/class-coverage) (Chromium-based only) Simply take V8 coverage with `startJSCoverage` and `stopJSCoverage`, then [converts](https://github.com/istanbuljs/v8-to-istanbul) from v8 coverage format to istanbul's coverage format, finally generate istanbul report. It works with any code format, but it will not be expected if your code is minified.
|
|
530
530
|
```js
|
|
531
531
|
import { test, expect } from '@playwright/test';
|
|
532
|
-
import {
|
|
532
|
+
import { attachCoverageReport } from 'monocart-reporter';
|
|
533
533
|
test.describe('take v8 coverage', () => {
|
|
534
534
|
test('first, startJSCoverage and open page', async () => {
|
|
535
|
-
await
|
|
536
|
-
await
|
|
535
|
+
await page.coverage.startJSCoverage();
|
|
536
|
+
await page.goto(pageUrl);
|
|
537
537
|
});
|
|
538
538
|
|
|
539
539
|
test('next, run test cases', async () => {
|
|
@@ -544,7 +544,7 @@ test.describe('take v8 coverage', () => {
|
|
|
544
544
|
|
|
545
545
|
test('finally, stopJSCoverage and take coverage', async () => {
|
|
546
546
|
// take v8 coverage
|
|
547
|
-
const jsCoverage = await
|
|
547
|
+
const jsCoverage = await page.coverage.stopJSCoverage();
|
|
548
548
|
// filter file list
|
|
549
549
|
const coverageInput = jsCoverage.filter((item) => {
|
|
550
550
|
if (!item.url.endsWith('.js')) {
|
|
@@ -554,16 +554,18 @@ test.describe('take v8 coverage', () => {
|
|
|
554
554
|
});
|
|
555
555
|
expect(coverageInput.length).toBeGreaterThan(0);
|
|
556
556
|
// coverage report
|
|
557
|
-
const report = await
|
|
557
|
+
const report = await attachCoverageReport(coverageInput, test.info());
|
|
558
558
|
console.log(report.lines);
|
|
559
559
|
});
|
|
560
560
|
});
|
|
561
561
|
```
|
|
562
|
-
- [istanbul](https://github.com/istanbuljs) Requires your source code is instrumented
|
|
562
|
+
- [istanbul](https://github.com/istanbuljs) Requires your source code is instrumented. Usually we can use the tool [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) to build instrumenting code (see example: [webpack.config.js](https://github.com/cenfun/monocart-reporter-test/blob/main/packages/coverage/webpack.config.js)) The instrumented code will automatically generate coverage data and save it on `window.__coverage__`
|
|
563
563
|
```js
|
|
564
|
+
import { test, expect } from '@playwright/test';
|
|
565
|
+
import { attachCoverageReport } from 'monocart-reporter';
|
|
564
566
|
test.describe('take istanbul coverage', () => {
|
|
565
567
|
test('first, open page', async () => {
|
|
566
|
-
await
|
|
568
|
+
await page.goto(pageUrl);
|
|
567
569
|
});
|
|
568
570
|
|
|
569
571
|
test('next, run test cases', async () => {
|
|
@@ -574,10 +576,10 @@ test.describe('take istanbul coverage', () => {
|
|
|
574
576
|
|
|
575
577
|
test('finally, take coverage', async () => {
|
|
576
578
|
// take istanbul coverage
|
|
577
|
-
const coverageInput = await
|
|
579
|
+
const coverageInput = await page.evaluate(() => window.__coverage__);
|
|
578
580
|
expect(coverageInput, 'expect found istanbul data: __coverage__').toBeTruthy();
|
|
579
581
|
// coverage report
|
|
580
|
-
const report = await
|
|
582
|
+
const report = await attachCoverageReport(coverageInput, test.info());
|
|
581
583
|
console.log(report.lines);
|
|
582
584
|
});
|
|
583
585
|
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const EC = require('eight-colors');
|
|
4
|
+
const Util = require('../utils/util.js');
|
|
5
|
+
|
|
6
|
+
module.exports = (harPath, testInfo, options = {}) => {
|
|
7
|
+
|
|
8
|
+
if (!fs.existsSync(harPath)) {
|
|
9
|
+
EC.logRed(`[MCR] not found HAR file: ${harPath}`);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const outputDir = testInfo.outputDir;
|
|
14
|
+
if (!fs.existsSync(outputDir)) {
|
|
15
|
+
fs.mkdirSync(outputDir, {
|
|
16
|
+
recursive: true
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
options = {
|
|
21
|
+
callback: 'onInputData',
|
|
22
|
+
... options
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const filename = 'network.harp';
|
|
26
|
+
|
|
27
|
+
const jsonStr = Util.readFileContentSync(harPath);
|
|
28
|
+
const content = `${options.callback}(${jsonStr});`;
|
|
29
|
+
|
|
30
|
+
const attachmentPath = path.resolve(outputDir, filename);
|
|
31
|
+
|
|
32
|
+
fs.writeFileSync(attachmentPath, content);
|
|
33
|
+
|
|
34
|
+
testInfo.attachments.push({
|
|
35
|
+
name: 'network',
|
|
36
|
+
contentType: 'application/javascript',
|
|
37
|
+
path: attachmentPath
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -6,14 +6,20 @@ const defaultOptions = require('./default/options.js');
|
|
|
6
6
|
const { getTrends } = require('./common.js');
|
|
7
7
|
|
|
8
8
|
const merge = require('./merge-data.js');
|
|
9
|
-
const
|
|
9
|
+
const attachCoverageReport = require('./helper/coverage.js');
|
|
10
|
+
const attachNetworkReport = require('./helper/network.js');
|
|
10
11
|
|
|
11
12
|
// custom reporter
|
|
12
13
|
// https://playwright.dev/docs/test-reporters#custom-reporters
|
|
13
14
|
class Reporter {
|
|
14
15
|
|
|
15
16
|
static merge = merge;
|
|
16
|
-
|
|
17
|
+
|
|
18
|
+
// Deprecated
|
|
19
|
+
static takeCoverage = attachCoverageReport;
|
|
20
|
+
|
|
21
|
+
static attachCoverageReport = attachCoverageReport;
|
|
22
|
+
static attachNetworkReport = attachNetworkReport;
|
|
17
23
|
|
|
18
24
|
constructor(userOptions = {}) {
|
|
19
25
|
|
package/lib/index.mjs
CHANGED
|
@@ -4,4 +4,9 @@ export default MonocartReporter;
|
|
|
4
4
|
export { MonocartReporter };
|
|
5
5
|
|
|
6
6
|
export const merge = MonocartReporter.merge;
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
// Deprecated
|
|
9
|
+
export const takeCoverage = MonocartReporter.attachCoverageReport;
|
|
10
|
+
|
|
11
|
+
export const attachCoverageReport = MonocartReporter.attachCoverageReport;
|
|
12
|
+
export const attachNetworkReport = MonocartReporter.attachNetworkReport;
|