@hedia/test 1.2.1 → 1.2.2

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.
Files changed (3) hide show
  1. package/README.md +11 -1
  2. package/package.json +12 -6
  3. package/src/reporter.js +80 -75
package/README.md CHANGED
@@ -1 +1,11 @@
1
- # test
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,27 @@
1
1
  {
2
2
  "name": "@hedia/test",
3
- "version": "1.2.1",
4
- "description": "",
3
+ "version": "1.2.2",
4
+ "description": "Custom Test Reporter",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./reporter": "./src/reporter.js"
8
- }, "scripts": {
9
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1",
11
+ "prettier:check": "prettier --check .",
12
+ "prettier:write": "prettier --write ."
10
13
  },
11
14
  "repository": {
12
15
  "type": "git",
13
16
  "url": "git+https://github.com/hedia-team/test.git"
14
17
  },
15
- "author": "",
18
+ "author": "Mathieu Veber <mathieu@hedia.com>",
16
19
  "license": "UNLICENSED",
17
20
  "bugs": {
18
21
  "url": "https://github.com/hedia-team/test/issues"
19
22
  },
20
- "homepage": "https://github.com/hedia-team/test#readme"
23
+ "homepage": "https://github.com/hedia-team/test#readme",
24
+ "devDependencies": {
25
+ "prettier": "^3.0.3"
26
+ }
21
27
  }
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
- try {
5
- let title = ''
6
- const tests = [];
7
- let nestedTitles = [];
8
- let nestingLevel = -1;
4
+ try {
5
+ let title = "";
6
+ const tests = [];
7
+ let nestedTitles = [];
8
+ let nestingLevel = -1;
9
9
 
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
- }
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
- yield `test ${nestedTitles.join(" - ")}: pass (${event.data.details.duration_ms}ms)\n`;
30
+ yield `test ${nestedTitles.join(" - ")}: pass (${
31
+ event.data.details.duration_ms
32
+ }ms)\n`;
31
33
 
32
- tests.push({
33
- name: nestedTitles.join(" - "),
34
- number: tests.length + 1,
35
- duration: event.data.details.duration_ms,
36
- status: "pass",
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
- break;
40
- case "test:fail":
41
- if (nestingLevel > event.data.nesting) {
42
- nestedTitles = nestedTitles.slice(0, -1);
43
- nestingLevel -= 1;
44
- }
45
- if (event.data?.details?.type === "suite") {
46
- break;
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
- yield `test ${nestedTitles.join(" - ")}: fail (${event.data.details.duration_ms}ms)\n`;
50
- tests.push({
51
- name: nestedTitles.join(" - "),
52
- number: tests.length + 1,
53
- duration: event.data.details.duration_ms,
54
- status: "fail",
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
- break;
58
- case "test:coverage":
59
- const location = event.data.summary.workingDirectory + "/package.json"
60
- const { name, version } = JSON.parse(readFileSync(location, 'utf8'))
61
- title = `${name}@${version}`
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
- const input = "https://test.hedia.dev/api/v1/reports";
66
- const init = {
67
- method: "POST",
68
- headers: {
69
- "Content-Type": "application/json",
70
- },
71
- body: JSON.stringify(
72
- {
73
- title,
74
- tests,
75
- },
76
- null,
77
- 4,
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
- const response = await fetch(input, init);
86
+ const response = await fetch(input, init);
82
87
 
83
- console.log(response.ok ? "OK" : "NOT OK");
84
- yield true;
85
- } catch (err) {
86
- console.error(err.message);
87
- }
88
+ console.log(response.ok ? "OK" : "NOT OK");
89
+ yield true;
90
+ } catch (err) {
91
+ console.error(err.message);
92
+ }
88
93
  }