@reporters/junit 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0 (2022-12-19)
4
+
5
+ Initial release
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Junit Reporter
2
+ A Junit reporter for `node:test`
3
+
4
+ ## Installation
5
+
6
+ ```bash
7
+ npm install --save-dev @reporters/junit
8
+ ```
9
+ or
10
+ ```bash
11
+ yarn add --dev @reporters/junit
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ```bash
17
+ node --test \
18
+ --test-reporter=@reporters/junit --test-reporter-destination=stdout \
19
+ --test-reporter=spec --test-reporter-destination=stdout
20
+ ```
21
+
22
+ ## Example
23
+
package/index.js ADDED
@@ -0,0 +1,108 @@
1
+ const assert = require('node:assert');
2
+ const util = require('node:util');
3
+ const { hostname } = require('node:os');
4
+
5
+ const HOSTNAME = hostname();
6
+
7
+ function escapeProperty(s = '') {
8
+ return s.replace(/"/g, '\\"');
9
+ }
10
+
11
+ function escapeContent(s = '') {
12
+ return s.replace(/</g, '&lt;').replace(/>/g, '&gt;');
13
+ }
14
+
15
+ function treeToXML(tree) {
16
+ if (typeof tree === 'string') {
17
+ return escapeContent(tree);
18
+ }
19
+ const {
20
+ tag, props, nesting, children,
21
+ } = tree;
22
+ const propsString = Object.entries(props)
23
+ .map(([key, value]) => `${key}="${escapeProperty(String(value))}"`)
24
+ .join(' ');
25
+ const indent = ' '.repeat(nesting + 1);
26
+ const newLine = children?.length ? '\n' : '';
27
+ const postIndent = newLine ? indent : '';
28
+ const postNewLine = newLine ? '' : '\n';
29
+ const childrenString = `${newLine}${(children ?? []).map(treeToXML).join('')}${newLine}${postIndent}`;
30
+ return `${indent}<${tag} ${propsString}>${childrenString}</${tag}>${postNewLine}`;
31
+ }
32
+
33
+ function isFailure(node) {
34
+ return node?.children.some((c) => c.tag === 'failure') || node?.props?.failures;
35
+ }
36
+
37
+ function isSkipped(node) {
38
+ return node?.children.some((c) => c.tag === 'skipped') || node?.props?.skipped;
39
+ }
40
+
41
+ module.exports = async function* junitReporter(source) {
42
+ yield '<?xml version="1.0" encoding="utf-8"?>\n';
43
+ yield '<testsuites>\n';
44
+ let currentSuite = null;
45
+ for await (const event of source) {
46
+ switch (event.type) {
47
+ case 'test:start': {
48
+ const originalSuite = currentSuite;
49
+ currentSuite = {
50
+ props: { name: event.data.name },
51
+ nesting: event.data.nesting,
52
+ parent: currentSuite,
53
+ children: [],
54
+ };
55
+ originalSuite?.children.push(currentSuite);
56
+ break;
57
+ }
58
+ case 'test:pass':
59
+ case 'test:fail': {
60
+ const currentTest = currentSuite;
61
+ if (currentSuite?.nesting === event.data.nesting && currentSuite?.parent) {
62
+ currentSuite = currentSuite.parent;
63
+ }
64
+ assert.strictEqual(currentTest.props.name, event.data.name);
65
+ currentTest.props.time = (event.data.details.duration_ms / 1000).toFixed(5);
66
+ if (currentTest.children.length > 0) {
67
+ currentTest.tag = 'testsuite';
68
+ currentTest.props.disabled = 0;
69
+ currentTest.props.errors = 0;
70
+ currentTest.props.tests = currentTest.children.length;
71
+ currentTest.props.failures = currentTest.children.filter(isFailure).length;
72
+ currentTest.props.skipped = currentTest.children.filter(isSkipped).length;
73
+ currentTest.props.hostname = HOSTNAME;
74
+ } else {
75
+ currentTest.tag = 'testcase';
76
+ currentTest.props.classname = 'test';
77
+ if (event.data.skip) {
78
+ currentTest.children.push({ nesting: event.data.nesting + 1, tag: 'skipped', props: { message: event.data.skip } });
79
+ }
80
+ if (event.data.todo) {
81
+ currentTest.children.push({ nesting: event.data.nesting + 1, tag: 'skipped', props: { message: event.data.todo } });
82
+ }
83
+ if (event.type === 'test:fail') {
84
+ const error = event.data.details?.error;
85
+ currentTest.children.push({
86
+ nesting: event.data.nesting + 1,
87
+ tag: 'failure',
88
+ props: { message: error?.message ?? '', type: error.failureType || error.code },
89
+ children: [util.inspect(
90
+ event.data.details?.error,
91
+ { colors: false, breakLength: Infinity },
92
+ )],
93
+ });
94
+ currentTest.failures = 1;
95
+ currentTest.props.failure = event.data.details?.error?.message ?? '';
96
+ }
97
+ }
98
+ break;
99
+ }
100
+ case 'test:diagnostic':
101
+ break;
102
+ default:
103
+ break;
104
+ }
105
+ }
106
+ yield treeToXML(currentSuite);
107
+ yield '\n</testsuites>\n';
108
+ };
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@reporters/junit",
3
+ "version": "1.0.0",
4
+ "description": "A junit reporter for `node:test`",
5
+ "keywords": [
6
+ "junit",
7
+ "node:test",
8
+ "test",
9
+ "reporter",
10
+ "reporters"
11
+ ],
12
+ "main": "index.js",
13
+ "repository": "https://github.com/MoLow/reporters.git",
14
+ "author": "Moshe Atlow",
15
+ "license": "MIT"
16
+ }