Skip to content

Commit 6d6ee2b

Browse files
committed
feat(test-runner-junit-reporter): Add support for flat test files to Web Test runner's junit-reporter
1 parent 985cb56 commit 6d6ee2b

File tree

7 files changed

+103
-3
lines changed

7 files changed

+103
-3
lines changed

.changeset/rotten-brooms-carry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/test-runner-junit-reporter': minor
3+
---
4+
5+
chore: Add support for flat test files to junit reporter

packages/test-runner-junit-reporter/src/junitReporter.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ interface TestSuiteXMLAttributes {
6969
const assignSessionAndSuitePropertiesToTests = ({
7070
testResults,
7171
...rest
72-
}: TestSession): TestResultWithMetadata[] => {
72+
}: TestSession, rootDir: string): TestResultWithMetadata[] => {
7373
const assignToTest =
7474
(parentSuiteName: string) =>
7575
(test: TestResult): TestResultWithMetadata => {
@@ -87,7 +87,11 @@ const assignSessionAndSuitePropertiesToTests = ({
8787

8888
const suites = testResults?.suites ?? [];
8989

90-
return suites.flatMap(assignToSuite(''));
90+
const testsWithoutSuite = testResults?.tests ?? [];
91+
92+
const suiteName = `${rest.browser.name}_${rest.browser.type}_${rest.testFile.replace(rootDir, '')}`;
93+
94+
return [...suites.flatMap(assignToSuite('')), ...testsWithoutSuite.flatMap(assignToTest(suiteName))];
9195
};
9296

9397
const toResultsWithMetadataByBrowserTestFileName = (
@@ -291,7 +295,7 @@ function getTestRunXML({
291295
}): string {
292296
const testsuites = Object.entries(
293297
sessions
294-
.flatMap(assignSessionAndSuitePropertiesToTests)
298+
.flatMap(s => assignSessionAndSuitePropertiesToTests(s, rootDir))
295299
.reduce(
296300
toResultsWithMetadataByBrowserTestFileName,
297301
{} as TestResultsWithMetadataByBrowserTestFileName,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuites>
3+
<testsuite name="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" id="0" tests="5" skipped="1" errors="1" failures="1" time="<<computed>>">
4+
<properties>
5+
<property name="test.fileName" value="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js"/>
6+
<property name="browser.name" value="Chrome"/>
7+
<property name="browser.launcher" value="puppeteer"/>
8+
</properties>
9+
<testcase name="under addition" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js"/>
10+
<testcase name="null hypothesis" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js"/>
11+
<testcase name="asserts error" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" line="12">
12+
<failure message="expected false to be true" type="AssertionError"><![CDATA[AssertionError: expected false to be true
13+
at <<anonymous>> (packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js:12:27)]]></failure>
14+
</testcase>
15+
<testcase name="tbd: confirm true positive" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js">
16+
<skipped/>
17+
</testcase>
18+
<testcase name="reports logs to JUnit" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/flat/flat-test.js"/>
19+
</testsuite>
20+
</testsuites>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import '../../../../../node_modules/chai/chai.js';
2+
3+
it('under addition', function () {
4+
chai.expect(1 + 1).to.equal(2);
5+
});
6+
7+
it('null hypothesis', function () {
8+
chai.expect(true).to.be.true;
9+
});
10+
11+
it('asserts error', function () {
12+
chai.expect(false).to.be.true;
13+
});
14+
15+
it.skip('tbd: confirm true positive', function () {
16+
chai.expect(false).to.be.false;
17+
});
18+
19+
it('reports logs to JUnit', function () {
20+
const actual = '🤷‍♂️';
21+
console.log('actual is ', actual);
22+
chai.expect(typeof actual).to.equal('string');
23+
});

packages/test-runner-junit-reporter/test/fixtures/multiple/expected.xml

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuites>
3+
<testsuite name="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" id="0" tests="5" skipped="1" errors="1" failures="1" time="<<computed>>">
4+
<properties>
5+
<property name="test.fileName" value="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js"/>
6+
<property name="browser.name" value="Chrome"/>
7+
<property name="browser.launcher" value="puppeteer"/>
8+
</properties>
9+
<testcase name="under addition" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js"/>
10+
<testcase name="null hypothesis" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js"/>
11+
<testcase name="asserts error" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" line="12">
12+
<failure message="expected false to be true" type="AssertionError"><![CDATA[AssertionError: expected false to be true
13+
at <<anonymous>> (packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js:12:27)]]></failure>
14+
</testcase>
15+
<testcase name="tbd: confirm true positive" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js">
16+
<skipped/>
17+
</testcase>
18+
<testcase name="reports logs to JUnit" time="<<computed>>" classname="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js" file="packages/test-runner-junit-reporter/test/fixtures/multiple/flat-test.js"/>
19+
</testsuite>
320
<testsuite name="Chrome_puppeteer_/packages/test-runner-junit-reporter/test/fixtures/multiple/nested-test.js" id="0" tests="1" skipped="0" errors="0" failures="0" time="<<computed>>">
421
<properties>
522
<property name="test.fileName" value="packages/test-runner-junit-reporter/test/fixtures/multiple/nested-test.js"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import '../../../../../node_modules/chai/chai.js';
2+
3+
it('under addition', function () {
4+
chai.expect(1 + 1).to.equal(2);
5+
});
6+
7+
it('null hypothesis', function () {
8+
chai.expect(true).to.be.true;
9+
});
10+
11+
it('asserts error', function () {
12+
chai.expect(false).to.be.true;
13+
});
14+
15+
it.skip('tbd: confirm true positive', function () {
16+
chai.expect(false).to.be.false;
17+
});
18+
19+
it('reports logs to JUnit', function () {
20+
const actual = '🤷‍♂️';
21+
console.log('actual is ', actual);
22+
chai.expect(typeof actual).to.equal('string');
23+
});

packages/test-runner-junit-reporter/test/junitReporter.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,12 @@ describe('junitReporter', function () {
9595
expect(actual).to.equal(expected);
9696
});
9797
});
98+
99+
describe('for flat test files', function () {
100+
const fixtureDir = path.join(__dirname, 'fixtures/flat');
101+
it('produces expected results', async function () {
102+
const { actual, expected } = await run(fixtureDir);
103+
expect(actual).to.equal(expected);
104+
});
105+
});
98106
});

0 commit comments

Comments
 (0)