monocart-reporter 2.9.6 → 2.9.7

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.
@@ -1,119 +1,119 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const Util = require('../../utils/util.js');
4
-
5
- const getStatus = (s) => {
6
- if (s < 0.5) {
7
- return 'low';
8
- }
9
- if (s < 0.9) {
10
- return 'medium';
11
- }
12
- return 'high';
13
- };
14
-
15
- const getSummaryReport = (lhr) => {
16
- const summary = {};
17
-
18
- const props = [
19
- 'lighthouseVersion',
20
- 'requestedUrl',
21
- 'fetchTime'
22
- ];
23
- props.forEach((k) => {
24
- summary[k] = lhr[k];
25
- });
26
-
27
- const audits = lhr.audits;
28
- const categories = lhr.categories;
29
- const performanceAudits = [
30
- 'first-contentful-paint',
31
- 'largest-contentful-paint',
32
- 'total-blocking-time',
33
- 'cumulative-layout-shift',
34
- 'speed-index'
35
- ];
36
-
37
- summary.categories = Object.keys(categories).map((k) => {
38
- const category = categories[k];
39
- const item = {
40
- name: category.title,
41
- score: category.score,
42
- status: getStatus(category.score),
43
- value: '',
44
- description: category.description || ''
45
- };
46
- if (k === 'performance') {
47
- item.metrics = performanceAudits.map((id) => {
48
- const it = audits[id];
49
- return {
50
- name: it.title,
51
- score: it.score,
52
- status: getStatus(it.score),
53
- value: it.displayValue,
54
- description: it.description || ''
55
- };
56
- });
57
- }
58
-
59
- return item;
60
- });
61
-
62
- return summary;
63
- };
64
-
65
- const attachAuditReport = async (runnerResult, testInfo, options = {}) => {
66
-
67
- const logging = Util.resolveLogging(testInfo, options);
68
- Util.initLoggingLevel(logging, 'audit');
69
-
70
- if (!runnerResult || !runnerResult.lhr || !runnerResult.report) {
71
- Util.logError('invalid lighthouse runner result');
72
- return;
73
- }
74
-
75
- const outputDir = Util.resolveOutputDir(testInfo);
76
-
77
- options = {
78
- name: `Lighthouse Report - ${testInfo.title}`,
79
- outputDir,
80
- outputName: `audit-${Util.resolveTestIdWithRetry(testInfo)}`,
81
- ... options
82
- };
83
-
84
- const htmlDir = path.resolve(options.outputDir, options.outputName);
85
- if (!fs.existsSync(htmlDir)) {
86
- fs.mkdirSync(htmlDir, {
87
- recursive: true
88
- });
89
- }
90
- options.htmlDir = htmlDir;
91
-
92
- // `.lhr` is the Lighthouse Result as a JS object
93
- const report = {
94
- name: options.name,
95
- ... getSummaryReport(runnerResult.lhr)
96
- };
97
-
98
- const htmlPath = path.resolve(htmlDir, 'index.html');
99
- // `.report` is the HTML report as a string
100
- await Util.writeFile(htmlPath, runnerResult.report);
101
-
102
- const definition = Util.attachments.audit;
103
-
104
- const reportPath = path.resolve(htmlDir, definition.reportFile);
105
- Util.writeJSONSync(reportPath, report);
106
-
107
- testInfo.attachments.push({
108
- name: definition.name,
109
- contentType: definition.contentType,
110
- path: path.resolve(htmlDir, 'index.html')
111
- });
112
-
113
- return report;
114
- };
115
-
116
-
117
- module.exports = {
118
- attachAuditReport
119
- };
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const Util = require('../../utils/util.js');
4
+
5
+ const getStatus = (s) => {
6
+ if (s < 0.5) {
7
+ return 'low';
8
+ }
9
+ if (s < 0.9) {
10
+ return 'medium';
11
+ }
12
+ return 'high';
13
+ };
14
+
15
+ const getSummaryReport = (lhr) => {
16
+ const summary = {};
17
+
18
+ const props = [
19
+ 'lighthouseVersion',
20
+ 'requestedUrl',
21
+ 'fetchTime'
22
+ ];
23
+ props.forEach((k) => {
24
+ summary[k] = lhr[k];
25
+ });
26
+
27
+ const audits = lhr.audits;
28
+ const categories = lhr.categories;
29
+ const performanceAudits = [
30
+ 'first-contentful-paint',
31
+ 'largest-contentful-paint',
32
+ 'total-blocking-time',
33
+ 'cumulative-layout-shift',
34
+ 'speed-index'
35
+ ];
36
+
37
+ summary.categories = Object.keys(categories).map((k) => {
38
+ const category = categories[k];
39
+ const item = {
40
+ name: category.title,
41
+ score: category.score,
42
+ status: getStatus(category.score),
43
+ value: '',
44
+ description: category.description || ''
45
+ };
46
+ if (k === 'performance') {
47
+ item.metrics = performanceAudits.map((id) => {
48
+ const it = audits[id];
49
+ return {
50
+ name: it.title,
51
+ score: it.score,
52
+ status: getStatus(it.score),
53
+ value: it.displayValue,
54
+ description: it.description || ''
55
+ };
56
+ });
57
+ }
58
+
59
+ return item;
60
+ });
61
+
62
+ return summary;
63
+ };
64
+
65
+ const attachAuditReport = async (runnerResult, testInfo, options = {}) => {
66
+
67
+ const logging = Util.resolveLogging(testInfo, options);
68
+ Util.initLoggingLevel(logging, 'audit');
69
+
70
+ if (!runnerResult || !runnerResult.lhr || !runnerResult.report) {
71
+ Util.logError('invalid lighthouse runner result');
72
+ return;
73
+ }
74
+
75
+ const outputDir = Util.resolveOutputDir(testInfo);
76
+
77
+ options = {
78
+ name: `Lighthouse Report - ${testInfo.title}`,
79
+ outputDir,
80
+ outputName: `audit-${Util.resolveTestIdWithRetry(testInfo)}`,
81
+ ... options
82
+ };
83
+
84
+ const htmlDir = path.resolve(options.outputDir, options.outputName);
85
+ if (!fs.existsSync(htmlDir)) {
86
+ fs.mkdirSync(htmlDir, {
87
+ recursive: true
88
+ });
89
+ }
90
+ options.htmlDir = htmlDir;
91
+
92
+ // `.lhr` is the Lighthouse Result as a JS object
93
+ const report = {
94
+ name: options.name,
95
+ ... getSummaryReport(runnerResult.lhr)
96
+ };
97
+
98
+ const htmlPath = path.resolve(htmlDir, 'index.html');
99
+ // `.report` is the HTML report as a string
100
+ await Util.writeFile(htmlPath, runnerResult.report);
101
+
102
+ const definition = Util.attachments.audit;
103
+
104
+ const reportPath = path.resolve(htmlDir, definition.reportFile);
105
+ Util.writeJSONSync(reportPath, report);
106
+
107
+ testInfo.attachments.push({
108
+ name: definition.name,
109
+ contentType: definition.contentType,
110
+ path: path.resolve(htmlDir, 'index.html')
111
+ });
112
+
113
+ return report;
114
+ };
115
+
116
+
117
+ module.exports = {
118
+ attachAuditReport
119
+ };
@@ -1,124 +1,124 @@
1
- const fs = require('fs');
2
- const Util = require('../utils/util.js');
3
- const { Locator } = require('monocart-locator');
4
-
5
- const cacheMap = new Map();
6
-
7
- const getFileComments = (sourcePath) => {
8
-
9
- const map = new Map();
10
-
11
- if (!fs.existsSync(sourcePath)) {
12
- return map;
13
- }
14
-
15
- // console.log('babel parse file: ', sourcePath);
16
-
17
- const source = fs.readFileSync(sourcePath).toString('utf-8');
18
-
19
- const locator = new Locator(source);
20
-
21
- const { comments, lines } = locator.lineParser;
22
-
23
- // get empty lines, no value
24
- lines.forEach((item) => {
25
- if (item.blank) {
26
- // 0-base
27
- map.set(item.line + 1, {});
28
- }
29
- });
30
-
31
- comments.forEach((item) => {
32
- const {
33
- start, end, text
34
- } = item;
35
-
36
- // using last line
37
- // 1-base
38
- const eLoc = locator.offsetToLocation(end);
39
- map.set(eLoc.line, {
40
- value: text
41
- });
42
-
43
- // exists first line
44
- if (map.has(1)) {
45
- return;
46
- }
47
-
48
- // first line comments for file line 0
49
- const sLoc = locator.offsetToLocation(start);
50
- if (sLoc.line === 1) {
51
- map.set(1, {
52
- value: text
53
- });
54
- }
55
-
56
- });
57
-
58
- return map;
59
- };
60
-
61
- const findCommentAbove = (line, map) => {
62
- if (line <= 1) {
63
- return;
64
- }
65
- const lineAbove = line - 1;
66
- const comment = map.get(lineAbove);
67
- if (!comment) {
68
- return;
69
- }
70
-
71
- // found
72
- if (comment.value) {
73
- return comment;
74
- }
75
-
76
- // empty line, continue above
77
- return findCommentAbove(lineAbove, map);
78
-
79
- };
80
-
81
- const findComment = (line, map) => {
82
- if (line <= 1) {
83
- // first line comments for file line 0
84
- const comment = map.get(1);
85
- if (comment && comment.value) {
86
- return comment;
87
- }
88
- return;
89
- }
90
- return findCommentAbove(line, map);
91
- };
92
-
93
- module.exports = (metadata) => {
94
- const location = metadata.location;
95
- if (!location) {
96
- return;
97
- }
98
-
99
- // location {}
100
- // file: 'H:\\workspace\\monocart-reporter\\tests\\home-page\\home-page.spec.js',
101
- // line: 13,
102
- // column: 6
103
-
104
- let map = cacheMap.get(location.file);
105
- if (!map) {
106
- map = getFileComments(location.file);
107
- // cache file info
108
- cacheMap.set(location.file, map);
109
- }
110
-
111
- // location line above
112
- const comment = findComment(location.line, map);
113
- if (!comment) {
114
- return;
115
- }
116
-
117
- let parsedValue = comment.parsedValue;
118
- if (!parsedValue) {
119
- parsedValue = Util.parseComments(comment.value);
120
- comment.parsedValue = parsedValue;
121
- }
122
-
123
- return parsedValue;
124
- };
1
+ const fs = require('fs');
2
+ const Util = require('../utils/util.js');
3
+ const { Locator } = require('monocart-locator');
4
+
5
+ const cacheMap = new Map();
6
+
7
+ const getFileComments = (sourcePath) => {
8
+
9
+ const map = new Map();
10
+
11
+ if (!fs.existsSync(sourcePath)) {
12
+ return map;
13
+ }
14
+
15
+ // console.log('babel parse file: ', sourcePath);
16
+
17
+ const source = fs.readFileSync(sourcePath).toString('utf-8');
18
+
19
+ const locator = new Locator(source);
20
+
21
+ const { comments, lines } = locator.lineParser;
22
+
23
+ // get empty lines, no value
24
+ lines.forEach((item) => {
25
+ if (item.blank) {
26
+ // 0-base
27
+ map.set(item.line + 1, {});
28
+ }
29
+ });
30
+
31
+ comments.forEach((item) => {
32
+ const {
33
+ start, end, text
34
+ } = item;
35
+
36
+ // using last line
37
+ // 1-base
38
+ const eLoc = locator.offsetToLocation(end);
39
+ map.set(eLoc.line, {
40
+ value: text
41
+ });
42
+
43
+ // exists first line
44
+ if (map.has(1)) {
45
+ return;
46
+ }
47
+
48
+ // first line comments for file line 0
49
+ const sLoc = locator.offsetToLocation(start);
50
+ if (sLoc.line === 1) {
51
+ map.set(1, {
52
+ value: text
53
+ });
54
+ }
55
+
56
+ });
57
+
58
+ return map;
59
+ };
60
+
61
+ const findCommentAbove = (line, map) => {
62
+ if (line <= 1) {
63
+ return;
64
+ }
65
+ const lineAbove = line - 1;
66
+ const comment = map.get(lineAbove);
67
+ if (!comment) {
68
+ return;
69
+ }
70
+
71
+ // found
72
+ if (comment.value) {
73
+ return comment;
74
+ }
75
+
76
+ // empty line, continue above
77
+ return findCommentAbove(lineAbove, map);
78
+
79
+ };
80
+
81
+ const findComment = (line, map) => {
82
+ if (line <= 1) {
83
+ // first line comments for file line 0
84
+ const comment = map.get(1);
85
+ if (comment && comment.value) {
86
+ return comment;
87
+ }
88
+ return;
89
+ }
90
+ return findCommentAbove(line, map);
91
+ };
92
+
93
+ module.exports = (metadata) => {
94
+ const location = metadata.location;
95
+ if (!location) {
96
+ return;
97
+ }
98
+
99
+ // location {}
100
+ // file: 'H:\\workspace\\monocart-reporter\\tests\\home-page\\home-page.spec.js',
101
+ // line: 13,
102
+ // column: 6
103
+
104
+ let map = cacheMap.get(location.file);
105
+ if (!map) {
106
+ map = getFileComments(location.file);
107
+ // cache file info
108
+ cacheMap.set(location.file, map);
109
+ }
110
+
111
+ // location line above
112
+ const comment = findComment(location.line, map);
113
+ if (!comment) {
114
+ return;
115
+ }
116
+
117
+ let parsedValue = comment.parsedValue;
118
+ if (!parsedValue) {
119
+ parsedValue = Util.parseComments(comment.value);
120
+ comment.parsedValue = parsedValue;
121
+ }
122
+
123
+ return parsedValue;
124
+ };