@hedia/test 1.2.2 → 2.0.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/README.md
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
|
-
# Custom Test
|
|
1
|
+
# Custom Test Reporters
|
|
2
|
+
|
|
3
|
+
## Test Reporter
|
|
2
4
|
|
|
3
5
|
Format and send test output to the `test-service`.
|
|
4
6
|
|
|
5
7
|
## Usage
|
|
6
8
|
|
|
7
9
|
```
|
|
8
|
-
node --test --experimental-test-coverage --test-reporter @hedia/test/
|
|
10
|
+
node --test --experimental-test-coverage --test-reporter @hedia/test/reporters/test
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Tip: Save the following command as a script in `package.json`
|
|
14
|
+
|
|
15
|
+
## Sonarcloud Reporter
|
|
16
|
+
|
|
17
|
+
Extracts coverage information from tests and creates generic coverage.xml file.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
node --test --experimental-test-coverage --test-reporter @hedia/test/reporters/sonarcloud
|
|
9
23
|
```
|
|
10
24
|
|
|
11
25
|
Tip: Save the following command as a script in `package.json`
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hedia/test",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Custom Test
|
|
3
|
+
"version": "2.0.0-alpha.0",
|
|
4
|
+
"description": "Custom Test Reporters",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
"./
|
|
7
|
+
"./reporters/sonarcloud": "./src/reporters/sonarcloud.js",
|
|
8
|
+
"./reporters/test": "./src/reporters/test.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,85 @@
|
|
|
1
|
+
import { attr, element, render } from "@hedia/html";
|
|
2
|
+
import { readFileSync } from "fs";
|
|
3
|
+
import { SourceMap } from "module";
|
|
4
|
+
import { dirname, relative, resolve } from "path";
|
|
5
|
+
|
|
6
|
+
export default async function* sonarcloudReporter(source) {
|
|
7
|
+
try {
|
|
8
|
+
for await (const event of source) {
|
|
9
|
+
switch (event.type) {
|
|
10
|
+
case "test:coverage":
|
|
11
|
+
yield coverage(event);
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error(err);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function coverage(event) {
|
|
21
|
+
const originalFiles = {};
|
|
22
|
+
|
|
23
|
+
for (const file of event.data.summary.files) {
|
|
24
|
+
const sourceMap = loadSourceMapForFile(file.path);
|
|
25
|
+
|
|
26
|
+
for (const lineNumber of file.uncoveredLineNumbers) {
|
|
27
|
+
const entry = sourceMap.findOrigin(lineNumber, 1);
|
|
28
|
+
|
|
29
|
+
if (entry) {
|
|
30
|
+
originalFiles[entry.fileName] =
|
|
31
|
+
originalFiles[entry.fileName] || new Set();
|
|
32
|
+
|
|
33
|
+
originalFiles[entry.fileName].add(entry.lineNumber);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return render(
|
|
39
|
+
element(
|
|
40
|
+
"coverage",
|
|
41
|
+
attr("version", "1"),
|
|
42
|
+
...Object.entries(originalFiles).map(([fileName, lines]) =>
|
|
43
|
+
element(
|
|
44
|
+
"file",
|
|
45
|
+
attr("path", fileName),
|
|
46
|
+
...Array.from(lines).map((line) =>
|
|
47
|
+
element(
|
|
48
|
+
"lineToCover",
|
|
49
|
+
attr("lineNumber", line.toString()),
|
|
50
|
+
attr("covered", "false")
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function loadSourceMapForFile(path) {
|
|
60
|
+
const source = readFileSync(path, "utf8");
|
|
61
|
+
|
|
62
|
+
const sourceMappingURL = getSourceMappingURL(source);
|
|
63
|
+
|
|
64
|
+
if (!sourceMappingURL) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const sourceMappingFile = resolve(dirname(path), sourceMappingURL);
|
|
69
|
+
|
|
70
|
+
const json = readFileSync(sourceMappingFile, "utf8");
|
|
71
|
+
const payload = JSON.parse(json);
|
|
72
|
+
|
|
73
|
+
payload.sources = payload.sources.map((source) =>
|
|
74
|
+
relative(process.cwd(), resolve(path, "..", source))
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const sourceMap = new SourceMap(payload);
|
|
78
|
+
|
|
79
|
+
return sourceMap;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function getSourceMappingURL(source) {
|
|
83
|
+
return /^\/\/# sourceMappingURL=(?<sourceMappingURL>.*)$/gm.exec(source)
|
|
84
|
+
?.groups?.sourceMappingURL;
|
|
85
|
+
}
|
|
File without changes
|