@saucelabs/sauce-json-reporter
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -18,43 +18,2 @@ type HasKeyOf<T> = { | ||
/** | ||
* JUnitTestCase represents a single, individual testcase in JUnit format. | ||
*/ | ||
export declare class JUnitTestCase { | ||
_name: string; | ||
_status: Status; | ||
_time: number; | ||
_timestamp: string; | ||
_videoTimestamp?: number; | ||
properties?: object; | ||
failure?: object; | ||
skipped?: object; | ||
constructor(name: string, status: Status, time: number, properties: object[], timestamp: string, videoTimestamp?: number, output?: string); | ||
} | ||
/** | ||
* JUnitTestSuite represents a testsuite in JUnit format. | ||
*/ | ||
export declare class JUnitTestSuite { | ||
_name: string; | ||
_status: Status; | ||
_tests: number; | ||
_failures: number; | ||
_skipped: number; | ||
_time: number; | ||
properties?: object; | ||
testcase: JUnitTestCase[]; | ||
constructor(name: string, status: Status, properties: object[], testcases: JUnitTestCase[]); | ||
} | ||
/** | ||
* JUnitReport represents a JUnit report. | ||
*/ | ||
export declare class JUnitReport { | ||
_status: Status; | ||
_tests: number; | ||
_failures: number; | ||
_skipped: number; | ||
_time: number; | ||
testsuite: JUnitTestSuite[]; | ||
properties?: object; | ||
constructor(testsuites: JUnitTestSuite[], status: Status, properties: object[]); | ||
} | ||
/** | ||
* TestRun represents the entire test run. | ||
@@ -73,4 +32,2 @@ */ | ||
stringify(computeStatus?: boolean): string; | ||
toJUnitObj(): JUnitReport; | ||
toJUnitFile(filepath: string, computeStatus?: boolean): void; | ||
toFile(filepath: string, computeStatus?: boolean): void; | ||
@@ -95,3 +52,2 @@ } | ||
withTest(name: string, options?: HasKeyOf<Test>): Test; | ||
toJUnitObj(): JUnitTestSuite[]; | ||
} | ||
@@ -113,3 +69,2 @@ /** | ||
attach(attachment: Attachment): void; | ||
toJUnitObj(): JUnitTestCase; | ||
} | ||
@@ -116,0 +71,0 @@ /** |
152
lib/index.js
@@ -26,5 +26,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.statusOf = exports.TestCode = exports.Test = exports.Suite = exports.TestRun = exports.JUnitReport = exports.JUnitTestSuite = exports.JUnitTestCase = exports.Status = void 0; | ||
exports.statusOf = exports.TestCode = exports.Test = exports.Suite = exports.TestRun = exports.Status = void 0; | ||
const fs = __importStar(require("fs")); | ||
const fast_xml_parser_1 = require("fast-xml-parser"); | ||
var Status; | ||
@@ -37,111 +36,2 @@ (function (Status) { | ||
/** | ||
* JUnitTestCase represents a single, individual testcase in JUnit format. | ||
*/ | ||
class JUnitTestCase { | ||
_name; | ||
_status; | ||
_time; | ||
_timestamp; | ||
_videoTimestamp; | ||
properties; | ||
failure; | ||
skipped; | ||
constructor(name, status, time, properties, timestamp, videoTimestamp, output) { | ||
this._name = name; | ||
this._status = status; | ||
this._time = time; | ||
this._timestamp = timestamp; | ||
this._videoTimestamp = videoTimestamp; | ||
this.properties = (properties.length > 0) ? { property: properties } : undefined; | ||
if (status === Status.Failed) { | ||
this.failure = { output }; | ||
} | ||
if (status === Status.Skipped) { | ||
this.skipped = { output }; | ||
} | ||
} | ||
} | ||
exports.JUnitTestCase = JUnitTestCase; | ||
/** | ||
* JUnitTestSuite represents a testsuite in JUnit format. | ||
*/ | ||
class JUnitTestSuite { | ||
_name; | ||
_status; | ||
_tests; | ||
_failures; | ||
_skipped; | ||
_time; | ||
properties; | ||
testcase; // to correctly build an array using XMLBuilder, testcase should be kept in the singular form. | ||
constructor(name, status, properties, testcases) { | ||
this._name = name; | ||
this._status = status; | ||
this.testcase = testcases; | ||
this._tests = testcases.length; | ||
this._failures = 0; | ||
this._skipped = 0; | ||
this._time = 0; | ||
this.properties = (properties.length > 0) ? { property: properties } : undefined; | ||
this.testcase.forEach(testcase => { | ||
if (testcase._status === Status.Failed) { | ||
this._failures += 1; | ||
} | ||
if (testcase._status === Status.Skipped) { | ||
this._skipped += 1; | ||
} | ||
this._time += testcase._time; | ||
}); | ||
} | ||
} | ||
exports.JUnitTestSuite = JUnitTestSuite; | ||
/** | ||
* JUnitReport represents a JUnit report. | ||
*/ | ||
class JUnitReport { | ||
_status; | ||
_tests; | ||
_failures; | ||
_skipped; | ||
_time; | ||
testsuite; // to correctly build an array using XMLBuilder, testsuite should be kept in the singular form. | ||
properties; | ||
constructor(testsuites, status, properties) { | ||
this.testsuite = testsuites; | ||
this._status = status; | ||
this._failures = 0; | ||
this._skipped = 0; | ||
this._tests = 0; | ||
this._time = 0; | ||
this.properties = (properties.length > 0) ? { property: properties } : undefined; | ||
this.testsuite.forEach(testsuite => { | ||
this._failures += testsuite._failures; | ||
this._skipped += testsuite._skipped; | ||
this._tests += testsuite._tests; | ||
this._time += testsuite._time; | ||
}); | ||
} | ||
} | ||
exports.JUnitReport = JUnitReport; | ||
/** | ||
* toProperty converts attachments and metadata to JUnit properties. | ||
*/ | ||
function toProperty(attachments, metadata) { | ||
const properties = []; | ||
attachments.forEach(attachment => { | ||
properties.push({ | ||
_name: 'attachment', | ||
_value: attachment.name, | ||
'#text': attachment.path, | ||
}); | ||
}); | ||
Object.entries(metadata).forEach(([key, value]) => { | ||
properties.push({ | ||
_name: key, | ||
_value: value, | ||
}); | ||
}); | ||
return properties; | ||
} | ||
/** | ||
* TestRun represents the entire test run. | ||
@@ -187,24 +77,2 @@ */ | ||
} | ||
toJUnitObj() { | ||
const testsuites = []; | ||
this.suites.forEach(suite => { | ||
testsuites.push(...suite.toJUnitObj()); | ||
}); | ||
return new JUnitReport(testsuites, this.status, toProperty(this.attachments, this.metadata)); | ||
} | ||
toJUnitFile(filepath, computeStatus = true) { | ||
if (computeStatus) { | ||
this.computeStatus(); | ||
} | ||
const options = { | ||
ignoreAttributes: false, | ||
attributeNamePrefix: "_", | ||
format: true, | ||
suppressEmptyNode: true, | ||
cdataPropName: "output", | ||
}; | ||
const builder = new fast_xml_parser_1.XMLBuilder(options); | ||
const xml = builder.build({ testsuites: this.toJUnitObj() }); | ||
fs.writeFileSync(filepath, xml); | ||
} | ||
toFile(filepath, computeStatus = true) { | ||
@@ -268,15 +136,2 @@ if (computeStatus) { | ||
} | ||
toJUnitObj() { | ||
const testcases = []; | ||
this.tests.forEach(test => { | ||
testcases.push(test.toJUnitObj()); | ||
}); | ||
const suites = [ | ||
new JUnitTestSuite(this.name, this.status, toProperty(this.attachments, this.metadata), testcases) | ||
]; | ||
this.suites.forEach(suite => { | ||
suites.push(...suite.toJUnitObj()); | ||
}); | ||
return suites; | ||
} | ||
} | ||
@@ -311,7 +166,2 @@ exports.Suite = Suite; | ||
} | ||
toJUnitObj() { | ||
return new JUnitTestCase(this.name, this.status, this.duration / 1000.0, toProperty(this.attachments || [], this.metadata), | ||
// startTime should be a string in this case. Otherwise, XMLBuilder will not recognize the attribute name prefix. | ||
this.startTime.toISOString(), this.videoTimestamp, this.output); | ||
} | ||
} | ||
@@ -318,0 +168,0 @@ exports.Test = Test; |
{ | ||
"name": "@saucelabs/sauce-json-reporter", | ||
"version": "3.1.0", | ||
"description": "", | ||
"version": "4.0.0", | ||
"description": "A library for creating test results in the Sauce Labs JSON format.", | ||
"main": "lib/index.js", | ||
"exports": { | ||
".": "./lib/index.js" | ||
}, | ||
"types": "lib/index.d.ts", | ||
@@ -25,5 +28,2 @@ "files": [ | ||
}, | ||
"dependencies": { | ||
"fast-xml-parser": "4.3.2" | ||
}, | ||
"devDependencies": { | ||
@@ -30,0 +30,0 @@ "@tsconfig/node20": "20.1.2", |
@@ -46,4 +46,2 @@ # sauce-json-reporter-js | ||
r.toFile('my_json_report.json') // writes the JSON to a file instead | ||
// or | ||
r.toJUnitFile('my_junit_report.xml') // converts and writes the result to a JUnit file | ||
``` | ||
@@ -132,37 +130,1 @@ | ||
``` | ||
The resulting JUnit file of the above example is: | ||
``` | ||
<testsuites status="failed" tests="3" failures="1" skipped="1" time="0.369"> | ||
<testsuite name="somegroup" status="failed" tests="0" failures="0" skipped="0" time="0"> | ||
<properties> | ||
<property name="attachment" value="screenshot1.png">./screenshot1.png</property> | ||
</properties> | ||
</testsuite> | ||
<testsuite name="somefile.test.js" status="failed" tests="3" failures="1" skipped="1" time="0.369"> | ||
<testcase name="yay" status="passed" time="0.123" timestamp="2023-06-20T21:32:07.467Z"> | ||
<properties> | ||
<property name="attachment" value="video.mp4">./video.mp4</property> | ||
<property name="attachment" value="screenshot2.png">./screenshot2.png</property> | ||
</properties> | ||
</testcase> | ||
<testcase name="nay" status="failed" time="0.123" timestamp="2023-06-20T21:32:07.467Z"> | ||
<properties> | ||
<property name="attachment" value="video.mp4">./video.mp4</property> | ||
</properties> | ||
<failure> | ||
<![CDATA[test failed]]> | ||
</failure> | ||
</testcase> | ||
<testcase name="oops" status="skipped" time="0.123" timestamp="2023-06-20T21:32:07.467Z"> | ||
<skipped> | ||
<![CDATA[test skipped]]> | ||
</skipped> | ||
</testcase> | ||
</testsuite> | ||
<properties> | ||
<property name="attachment" value="screenshot.png">./screenshot.png</property> | ||
<property name="id" value="123"/> | ||
</properties> | ||
</testsuites> | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
15053
280
129
- Removedfast-xml-parser@4.3.2
- Removedfast-xml-parser@4.3.2(transitive)
- Removedstrnum@1.0.5(transitive)