@hedia/test 1.3.0-alpha.0 → 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 Reporter
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/reporter
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,11 +1,11 @@
1
1
  {
2
2
  "name": "@hedia/test",
3
- "version": "1.3.0-alpha.0",
4
- "description": "Custom Test Reporter",
3
+ "version": "2.0.0-alpha.0",
4
+ "description": "Custom Test Reporters",
5
5
  "type": "module",
6
6
  "exports": {
7
- "./reporter": "./src/reporter.js",
8
- "./sonarcloud": "./src/sonarcloud.js"
7
+ "./reporters/sonarcloud": "./src/reporters/sonarcloud.js",
8
+ "./reporters/test": "./src/reporters/test.js"
9
9
  },
10
10
  "scripts": {
11
11
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -1,40 +1,36 @@
1
1
  import { attr, element, render } from "@hedia/html";
2
2
  import { readFileSync } from "fs";
3
3
  import { SourceMap } from "module";
4
- import { relative, resolve } from "path";
4
+ import { dirname, relative, resolve } from "path";
5
5
 
6
- export default async function* testReporter(source) {
6
+ export default async function* sonarcloudReporter(source) {
7
7
  try {
8
- let xml;
9
-
10
8
  for await (const event of source) {
11
9
  switch (event.type) {
12
10
  case "test:coverage":
13
- xml = generic(event);
11
+ yield coverage(event);
14
12
  break;
15
13
  }
16
14
  }
17
-
18
- yield xml;
19
15
  } catch (err) {
20
16
  console.error(err);
21
17
  }
22
18
  }
23
19
 
24
- function generic(event) {
20
+ function coverage(event) {
25
21
  const originalFiles = {};
26
22
 
27
23
  for (const file of event.data.summary.files) {
28
24
  const sourceMap = loadSourceMapForFile(file.path);
29
25
 
30
- for (let lineOffset = 1; lineOffset <= file.totalLineCount; lineOffset++) {
31
- const entry = sourceMap.findEntry(lineOffset, 1);
26
+ for (const lineNumber of file.uncoveredLineNumbers) {
27
+ const entry = sourceMap.findOrigin(lineNumber, 1);
32
28
 
33
29
  if (entry) {
34
- originalFiles[entry.originalSource] =
35
- originalFiles[entry.originalSource] || new Set();
30
+ originalFiles[entry.fileName] =
31
+ originalFiles[entry.fileName] || new Set();
36
32
 
37
- originalFiles[entry.originalSource].add(entry.originalLine);
33
+ originalFiles[entry.fileName].add(entry.lineNumber);
38
34
  }
39
35
  }
40
36
  }
@@ -51,7 +47,7 @@ function generic(event) {
51
47
  element(
52
48
  "lineToCover",
53
49
  attr("lineNumber", line.toString()),
54
- attr("covered", "true")
50
+ attr("covered", "false")
55
51
  )
56
52
  )
57
53
  )
@@ -61,11 +57,17 @@ function generic(event) {
61
57
  }
62
58
 
63
59
  function loadSourceMapForFile(path) {
64
- const rel = resolve(process.cwd(), path);
60
+ const source = readFileSync(path, "utf8");
65
61
 
66
- const file = path + ".map";
62
+ const sourceMappingURL = getSourceMappingURL(source);
63
+
64
+ if (!sourceMappingURL) {
65
+ return;
66
+ }
67
67
 
68
- const json = readFileSync(file, "utf8");
68
+ const sourceMappingFile = resolve(dirname(path), sourceMappingURL);
69
+
70
+ const json = readFileSync(sourceMappingFile, "utf8");
69
71
  const payload = JSON.parse(json);
70
72
 
71
73
  payload.sources = payload.sources.map((source) =>
@@ -76,3 +78,8 @@ function loadSourceMapForFile(path) {
76
78
 
77
79
  return sourceMap;
78
80
  }
81
+
82
+ function getSourceMappingURL(source) {
83
+ return /^\/\/# sourceMappingURL=(?<sourceMappingURL>.*)$/gm.exec(source)
84
+ ?.groups?.sourceMappingURL;
85
+ }
File without changes