From 33f32368e555233b8d7271ca015a1e1400308029 Mon Sep 17 00:00:00 2001 From: Ross Wollman Date: Mon, 17 Jan 2022 18:47:24 -0800 Subject: [PATCH] fix(test runner): dispatcher transformation of empty attachments (#11414) --- packages/playwright-test/src/dispatcher.ts | 2 +- .../reporter-attachment.spec.ts | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/playwright-test/src/dispatcher.ts b/packages/playwright-test/src/dispatcher.ts index cf6514249e..9edccad679 100644 --- a/packages/playwright-test/src/dispatcher.ts +++ b/packages/playwright-test/src/dispatcher.ts @@ -204,7 +204,7 @@ export class Dispatcher { name: a.name, path: a.path, contentType: a.contentType, - body: a.body ? Buffer.from(a.body, 'base64') : undefined + body: typeof a.body !== 'undefined' ? Buffer.from(a.body, 'base64') : undefined })); result.status = params.status; test.expectedStatus = params.expectedStatus; diff --git a/tests/playwright-test/reporter-attachment.spec.ts b/tests/playwright-test/reporter-attachment.spec.ts index 94d6dc0ae5..095f300a7f 100644 --- a/tests/playwright-test/reporter-attachment.spec.ts +++ b/tests/playwright-test/reporter-attachment.spec.ts @@ -143,3 +143,33 @@ test(`testInfo.attach success in fixture`, async ({ runInlineTest }) => { expect(result.failed).toBe(1); expect(stripAscii(result.output)).toContain('attachment #1: name (text/plain)'); }); + +test(`testInfo.attach allow empty string body`, async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.js': ` + const { test } = pwt; + test('success', async ({}, testInfo) => { + await testInfo.attach('name', { body: '', contentType: 'text/plain' }); + expect(0).toBe(1); + }); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(stripAscii(result.output)).toMatch(/^.*attachment #1: name \(text\/plain\).*\n.*\n.*------/gm); +}); + +test(`testInfo.attach allow empty buffer body`, async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.js': ` + const { test } = pwt; + test('success', async ({}, testInfo) => { + await testInfo.attach('name', { body: Buffer.from(''), contentType: 'text/plain' }); + expect(0).toBe(1); + }); + `, + }); + expect(result.exitCode).toBe(1); + expect(result.failed).toBe(1); + expect(stripAscii(result.output)).toMatch(/^.*attachment #1: name \(text\/plain\).*\n.*\n.*------/gm); +});