@hedia/test 1.2.1 → 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/README.md +11 -1
- package/package.json +18 -8
- package/src/reporter.js +80 -75
- package/src/sonarcloud.js +78 -0
package/README.md
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Custom Test Reporter
|
|
2
|
+
|
|
3
|
+
Format and send test output to the `test-service`.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
node --test --experimental-test-coverage --test-reporter @hedia/test/reporter
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Tip: Save the following command as a script in `package.json`
|
package/package.json
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hedia/test",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.3.0-alpha.0",
|
|
4
|
+
"description": "Custom Test Reporter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
"./reporter": "./src/reporter.js"
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
"./reporter": "./src/reporter.js",
|
|
8
|
+
"./sonarcloud": "./src/sonarcloud.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
12
|
+
"prettier:check": "prettier --check .",
|
|
13
|
+
"prettier:write": "prettier --write ."
|
|
10
14
|
},
|
|
11
15
|
"repository": {
|
|
12
16
|
"type": "git",
|
|
13
17
|
"url": "git+https://github.com/hedia-team/test.git"
|
|
14
18
|
},
|
|
15
|
-
"author": "",
|
|
19
|
+
"author": "Mathieu Veber <mathieu@hedia.com>",
|
|
16
20
|
"license": "UNLICENSED",
|
|
17
21
|
"bugs": {
|
|
18
22
|
"url": "https://github.com/hedia-team/test/issues"
|
|
19
23
|
},
|
|
20
|
-
"homepage": "https://github.com/hedia-team/test#readme"
|
|
21
|
-
|
|
24
|
+
"homepage": "https://github.com/hedia-team/test#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"prettier": "^3.0.3"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hedia/html": "2.0.1"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/reporter.js
CHANGED
|
@@ -1,88 +1,93 @@
|
|
|
1
1
|
import { readFileSync } from "fs";
|
|
2
2
|
|
|
3
3
|
export default async function* testReporter(source) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
try {
|
|
5
|
+
let title = "";
|
|
6
|
+
const tests = [];
|
|
7
|
+
let nestedTitles = [];
|
|
8
|
+
let nestingLevel = -1;
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
10
|
+
for await (const event of source) {
|
|
11
|
+
switch (event.type) {
|
|
12
|
+
case "test:start":
|
|
13
|
+
if (nestingLevel < event.data.nesting) {
|
|
14
|
+
nestedTitles.push(event.data.name);
|
|
15
|
+
nestingLevel += 1;
|
|
16
|
+
}
|
|
17
|
+
if (nestingLevel === event.data.nesting) {
|
|
18
|
+
nestedTitles[nestedTitles.length - 1] = event.data.name;
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
case "test:pass":
|
|
22
|
+
if (nestingLevel > event.data.nesting) {
|
|
23
|
+
nestedTitles = nestedTitles.slice(0, -1);
|
|
24
|
+
nestingLevel -= 1;
|
|
25
|
+
}
|
|
26
|
+
if (event.data?.details?.type === "suite") {
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
yield `test ${nestedTitles.join(" - ")}: pass (${
|
|
31
|
+
event.data.details.duration_ms
|
|
32
|
+
}ms)\n`;
|
|
31
33
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
tests.push({
|
|
35
|
+
name: nestedTitles.join(" - "),
|
|
36
|
+
number: tests.length + 1,
|
|
37
|
+
duration: event.data.details.duration_ms,
|
|
38
|
+
status: "pass",
|
|
39
|
+
});
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
break;
|
|
42
|
+
case "test:fail":
|
|
43
|
+
if (nestingLevel > event.data.nesting) {
|
|
44
|
+
nestedTitles = nestedTitles.slice(0, -1);
|
|
45
|
+
nestingLevel -= 1;
|
|
46
|
+
}
|
|
47
|
+
if (event.data?.details?.type === "suite") {
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
yield `test ${nestedTitles.join(" - ")}: fail (${
|
|
52
|
+
event.data.details.duration_ms
|
|
53
|
+
}ms)\n`;
|
|
54
|
+
tests.push({
|
|
55
|
+
name: nestedTitles.join(" - "),
|
|
56
|
+
number: tests.length + 1,
|
|
57
|
+
duration: event.data.details.duration_ms,
|
|
58
|
+
status: "fail",
|
|
59
|
+
});
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
break;
|
|
62
|
+
case "test:coverage":
|
|
63
|
+
const location =
|
|
64
|
+
event.data.summary.workingDirectory + "/package.json";
|
|
65
|
+
const { name, version } = JSON.parse(readFileSync(location, "utf8"));
|
|
66
|
+
title = `${name}@${version}`;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
64
69
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
70
|
+
const input = "https://test.hedia.dev/api/v1/reports";
|
|
71
|
+
const init = {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: {
|
|
74
|
+
"Content-Type": "application/json",
|
|
75
|
+
},
|
|
76
|
+
body: JSON.stringify(
|
|
77
|
+
{
|
|
78
|
+
title,
|
|
79
|
+
tests,
|
|
80
|
+
},
|
|
81
|
+
null,
|
|
82
|
+
4,
|
|
83
|
+
),
|
|
84
|
+
};
|
|
80
85
|
|
|
81
|
-
|
|
86
|
+
const response = await fetch(input, init);
|
|
82
87
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
console.log(response.ok ? "OK" : "NOT OK");
|
|
89
|
+
yield true;
|
|
90
|
+
} catch (err) {
|
|
91
|
+
console.error(err.message);
|
|
92
|
+
}
|
|
88
93
|
}
|
|
@@ -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
|
+
}
|