@hedia/test 1.2.2 → 1.3.0-alpha.0
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/package.json +7 -3
- package/src/sonarcloud.js +78 -0
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hedia/test",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-alpha.0",
|
|
4
4
|
"description": "Custom Test Reporter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
"./reporter": "./src/reporter.js"
|
|
7
|
+
"./reporter": "./src/reporter.js",
|
|
8
|
+
"./sonarcloud": "./src/sonarcloud.js"
|
|
8
9
|
},
|
|
9
10
|
"scripts": {
|
|
10
11
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -23,5 +24,8 @@
|
|
|
23
24
|
"homepage": "https://github.com/hedia-team/test#readme",
|
|
24
25
|
"devDependencies": {
|
|
25
26
|
"prettier": "^3.0.3"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hedia/html": "2.0.1"
|
|
26
30
|
}
|
|
27
|
-
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { attr, element, render } from "@hedia/html";
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { SourceMap } from "module";
|
|
4
|
+
import { relative, resolve } from "path";
|
|
5
|
+
|
|
6
|
+
export default async function* testReporter(source) {
|
|
7
|
+
try {
|
|
8
|
+
let xml;
|
|
9
|
+
|
|
10
|
+
for await (const event of source) {
|
|
11
|
+
switch (event.type) {
|
|
12
|
+
case "test:coverage":
|
|
13
|
+
xml = generic(event);
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
yield xml;
|
|
19
|
+
} catch (err) {
|
|
20
|
+
console.error(err);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function generic(event) {
|
|
25
|
+
const originalFiles = {};
|
|
26
|
+
|
|
27
|
+
for (const file of event.data.summary.files) {
|
|
28
|
+
const sourceMap = loadSourceMapForFile(file.path);
|
|
29
|
+
|
|
30
|
+
for (let lineOffset = 1; lineOffset <= file.totalLineCount; lineOffset++) {
|
|
31
|
+
const entry = sourceMap.findEntry(lineOffset, 1);
|
|
32
|
+
|
|
33
|
+
if (entry) {
|
|
34
|
+
originalFiles[entry.originalSource] =
|
|
35
|
+
originalFiles[entry.originalSource] || new Set();
|
|
36
|
+
|
|
37
|
+
originalFiles[entry.originalSource].add(entry.originalLine);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return render(
|
|
43
|
+
element(
|
|
44
|
+
"coverage",
|
|
45
|
+
attr("version", "1"),
|
|
46
|
+
...Object.entries(originalFiles).map(([fileName, lines]) =>
|
|
47
|
+
element(
|
|
48
|
+
"file",
|
|
49
|
+
attr("path", fileName),
|
|
50
|
+
...Array.from(lines).map((line) =>
|
|
51
|
+
element(
|
|
52
|
+
"lineToCover",
|
|
53
|
+
attr("lineNumber", line.toString()),
|
|
54
|
+
attr("covered", "true")
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function loadSourceMapForFile(path) {
|
|
64
|
+
const rel = resolve(process.cwd(), path);
|
|
65
|
+
|
|
66
|
+
const file = path + ".map";
|
|
67
|
+
|
|
68
|
+
const json = readFileSync(file, "utf8");
|
|
69
|
+
const payload = JSON.parse(json);
|
|
70
|
+
|
|
71
|
+
payload.sources = payload.sources.map((source) =>
|
|
72
|
+
relative(process.cwd(), resolve(path, "..", source))
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const sourceMap = new SourceMap(payload);
|
|
76
|
+
|
|
77
|
+
return sourceMap;
|
|
78
|
+
}
|