From 80c0bbeb86ea45cc16e202d72dd00aa1afa60c12 Mon Sep 17 00:00:00 2001 From: CR Drost Date: Mon, 21 Nov 2022 11:42:41 -0800 Subject: [PATCH] fix(test-runner-junit-reporter): standardize JUnit format There is no single standard XML format for JUnit output, sadly. However there are some XML Schema Definition (XSD) files which are circulated defining a couple of schemas which Jenkins plugins etc. know about and can validate. This change brings the test-runner-junit-reporter output into conformance with one, `@jenkinssci/xunit-plugin::junit-10.xsd`, which it seemed "closest to". (XSD permits `` tags and each individual `` has an `id` attribute and a `skipped` count, but no `package` attribute, like the foregoing junit-reporter output.) To make this change requires three semantic changes: - `junit-10.xsd` does not permit the `file` attribute on ``. This is no great loss of information, as it is recapitulated a level above on the ``. - The requested `file` attribute is now on `` where the schema says it's permissible. - After aggregating `results.reduce(addSuiteTime, 0)` this change has to perform a `Number.prototype.toFixed()` reduction in precision to match the XSD. This change is bubbled up into the data types, which store the number as a `string` now. Fixes: #2072 --- .changeset/beige-timers-juggle.md | 5 ++++ .../src/junitReporter.ts | 29 +++++++++---------- .../test/fixtures/multiple/expected.xml | 18 ++++++------ .../test/fixtures/nested/expected.xml | 6 ++-- .../test/fixtures/simple/expected.xml | 16 +++++----- 5 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 .changeset/beige-timers-juggle.md diff --git a/.changeset/beige-timers-juggle.md b/.changeset/beige-timers-juggle.md new file mode 100644 index 000000000..bb55a0e97 --- /dev/null +++ b/.changeset/beige-timers-juggle.md @@ -0,0 +1,5 @@ +--- +'@web/test-runner-junit-reporter': minor +--- + +Update result format to match JUnit Schema definitions diff --git a/packages/test-runner-junit-reporter/src/junitReporter.ts b/packages/test-runner-junit-reporter/src/junitReporter.ts index 7ba8d5b79..96b3f3dd3 100644 --- a/packages/test-runner-junit-reporter/src/junitReporter.ts +++ b/packages/test-runner-junit-reporter/src/junitReporter.ts @@ -31,7 +31,6 @@ interface TestCaseXMLAttributes { _attr: { name: string; time: number; - file: string; line?: string; classname: string; }; @@ -62,7 +61,8 @@ interface TestSuiteXMLAttributes { skipped: number; errors: number; failures: number; - time: number; + time: string; + file: string; }; } @@ -120,8 +120,6 @@ const getTestName: TestMetaGetter = test => test.name; const getSuiteName: TestMetaGetter = test => test.suiteName; -const getTestFile: TestMetaGetter = test => test.testFile; - const getTestDurationInSeconds = ({ duration }: TestResult): number => (typeof duration === 'undefined' ? 0 : duration) / 1000; @@ -161,18 +159,13 @@ function testFailureXMLElement(test: TestResultWithMetadata): TestFailureXMLElem * Makes attributes for a `` element * @param test Test Result */ -function testCaseXMLAttributes( - test: TestResultWithMetadata, - rootDir: string, -): TestCaseXMLAttributes { +function testCaseXMLAttributes(test: TestResultWithMetadata): TestCaseXMLAttributes { const name = getTestName(test); const time = getTestDurationInSeconds(test); const classname = getSuiteName(test); - const file = getTestFile(test).replace(`${rootDir}${path.sep}`, ''); - const [, line] = stripXMLInvalidChars(test.error?.stack ?? '').match(/(\d+):\d+/m) ?? []; return { @@ -180,7 +173,6 @@ function testCaseXMLAttributes( name, time, classname, - file, ...(!!line && { line }), }, }; @@ -189,8 +181,8 @@ function testCaseXMLAttributes( /** * Makes a `` element */ -function testCaseXMLElement(test: TestResultWithMetadata, rootDir: string): TestCaseXMLElement { - const attributes = testCaseXMLAttributes(test, rootDir); +function testCaseXMLElement(test: TestResultWithMetadata): TestCaseXMLElement { + const attributes = testCaseXMLAttributes(test); // prettier-ignore if (isSkippedTest(test)) @@ -211,6 +203,8 @@ function testSuiteXMLAttributes( name: string, id: number, results: TestResultWithMetadata[], + testFile: string, + rootDir: string, ): TestSuiteXMLAttributes { const tests = results.length; @@ -220,7 +214,9 @@ function testSuiteXMLAttributes( const failures = results.filter(isFailedTest).length; - const time = results.reduce(addSuiteTime, 0); + const time = results.reduce(addSuiteTime, 0).toFixed(3); + + const file = testFile.replace(`${rootDir}${path.sep}`, ''); return { _attr: { @@ -231,6 +227,7 @@ function testSuiteXMLAttributes( errors, failures, time, + file, }, }; } @@ -303,11 +300,11 @@ function getTestRunXML({ const launcherType = browser.type ?? ''; - const attributes = testSuiteXMLAttributes(name, testRun, tests); + const attributes = testSuiteXMLAttributes(name, testRun, tests, testFile, rootDir); const properties = testSuitePropertiesXMLElement(testFile, browserName, launcherType, rootDir); - const testcases = tests.map(t => testCaseXMLElement(t, rootDir)); + const testcases = tests.map(testCaseXMLElement); const systemOut = !reportLogs ? [] : tests.flatMap(escapeLogs).map(x => ({ 'system-out': x })); diff --git a/packages/test-runner-junit-reporter/test/fixtures/multiple/expected.xml b/packages/test-runner-junit-reporter/test/fixtures/multiple/expected.xml index 88dd3f7a4..6e4ce1472 100644 --- a/packages/test-runner-junit-reporter/test/fixtures/multiple/expected.xml +++ b/packages/test-runner-junit-reporter/test/fixtures/multiple/expected.xml @@ -1,28 +1,28 @@ - + - + - + - - - + + + (packages/test-runner-junit-reporter/test/fixtures/multiple/simple-test.js:15:29)]]> - + - + - + \ No newline at end of file diff --git a/packages/test-runner-junit-reporter/test/fixtures/nested/expected.xml b/packages/test-runner-junit-reporter/test/fixtures/nested/expected.xml index d1633aa04..e8260fb00 100644 --- a/packages/test-runner-junit-reporter/test/fixtures/nested/expected.xml +++ b/packages/test-runner-junit-reporter/test/fixtures/nested/expected.xml @@ -1,11 +1,11 @@ - + - + - + \ No newline at end of file diff --git a/packages/test-runner-junit-reporter/test/fixtures/simple/expected.xml b/packages/test-runner-junit-reporter/test/fixtures/simple/expected.xml index d2293173b..34fc363a1 100644 --- a/packages/test-runner-junit-reporter/test/fixtures/simple/expected.xml +++ b/packages/test-runner-junit-reporter/test/fixtures/simple/expected.xml @@ -1,25 +1,25 @@ - + - - - + + + (packages/test-runner-junit-reporter/test/fixtures/simple/simple-test.js:17:29)]]> - + - - + + (packages/test-runner-junit-reporter/test/fixtures/simple/simple-test.js:33:17)]]> - + \ No newline at end of file