Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/test/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,28 @@ class JUnitReporter implements Reporter {
text: stripAnsiEscapes(formatFailure(this.config, test))
});
}
for (const result of test.results) {
for (const stdout of result.stdout) {
entry.children.push({
name: 'system-out',
text: stdout.toString()
});
}

const systemOut: string[] = [];
const systemErr: string[] = [];
for (const result of test.results) {
systemOut.push(...result.stdout.map(item => item.toString()));
systemErr.push(...result.stderr.map(item => item.toString()));
for (const attachment of result.attachments) {
if (attachment.path) {
entry.children.push({
name: 'system-out',
text: `[[ATTACHMENT|${path.relative(this.config.rootDir, attachment.path)}]]`
});
if (!attachment.path)
continue;
try {
if (fs.existsSync(attachment.path))
systemOut.push(`\n[[ATTACHMENT|${path.relative(this.config.rootDir, attachment.path)}]]\n`);
} catch (e) {
}
}

for (const stderr of result.stderr) {
entry.children.push({
name: 'system-err',
text: stderr.toString()
});
}
}
// Note: it is important to only produce a single system-out/system-err entry
// so that parsers in the wild understand it.
if (systemOut.length)
entry.children.push({ name: 'system-out', text: systemOut.join('') });
if (systemErr.length)
entry.children.push({ name: 'system-err', text: systemErr.join('') });
}
}

Expand Down
22 changes: 17 additions & 5 deletions tests/playwright-test/junit-reporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,19 @@ test('should render stdout', async ({ runInlineTest }) => {
const { test } = pwt;
test('one', async ({}) => {
console.log(colors.yellow('Hello world'));
console.log('Hello again');
console.error('My error');
test.expect("abc").toBe('abcd');
});
`,
}, { reporter: 'junit' });
const xml = parseXML(result.output);
const testcase = xml['testsuites']['testsuite'][0]['testcase'][0];
expect(testcase['system-out'].length).toBe(1);
expect(testcase['system-out'][0]).toContain('Hello world');
expect(testcase['system-out'][0]).toContain('[33mHello world[39m\nHello again');
expect(testcase['system-out'][0]).not.toContain('u00');
expect(testcase['failure'][0]['_']).toContain(`> 9 | test.expect("abc").toBe('abcd');`);
expect(testcase['system-err'][0]).toContain('My error');
expect(testcase['failure'][0]['_']).toContain(`> 11 | test.expect("abc").toBe('abcd');`);
expect(result.exitCode).toBe(1);
});

Expand Down Expand Up @@ -221,20 +224,29 @@ test('should render projects', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(0);
});

test('should render attachments', async ({ runInlineTest }) => {
test('should render existing attachments, but not missing ones', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.js': `
const { test } = pwt;
test.use({ screenshot: 'on' });
test('one', async ({ page }) => {
test('one', async ({ page }, testInfo) => {
await page.setContent('hello');
const file = testInfo.outputPath('file.txt');
require('fs').writeFileSync(file, 'my file', 'utf8');
testInfo.attachments.push({ name: 'my-file', path: file, contentType: 'text/plain' });
testInfo.attachments.push({ name: 'my-file-missing', path: file + '-missing', contentType: 'text/plain' });
console.log('log here');
});
`,
}, { reporter: 'junit' });
const xml = parseXML(result.output);
const testcase = xml['testsuites']['testsuite'][0]['testcase'][0];
expect(testcase['system-out'].length).toBe(1);
expect(testcase['system-out'][0].trim()).toBe(`[[ATTACHMENT|test-results${path.sep}a-one${path.sep}test-finished-1.png]]`);
expect(testcase['system-out'][0].trim()).toBe([
`log here`,
`\n[[ATTACHMENT|test-results${path.sep}a-one${path.sep}file.txt]]`,
`\n[[ATTACHMENT|test-results${path.sep}a-one${path.sep}test-finished-1.png]]`,
].join('\n'));
expect(result.exitCode).toBe(0);
});

Expand Down