From 0d42c16a178742efec373b6f174fda831579d0c0 Mon Sep 17 00:00:00 2001 From: Ross Wollman Date: Thu, 10 Feb 2022 12:33:38 -0800 Subject: [PATCH] fix(test-runner): undefined body crash with manual attachments (#11995) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new (as of 1.18) `async testInfo.attach(…)` API handles this gracefully (and is part of the reason for the new API's existence). However, for the foreseeable future, it's still possible to manually push onto the attachments array where we can't validate the contents until it's too late, so this change ensures more graceful handling in that case. Fixes #11565 --- .../playwright-test/src/reporters/base.ts | 4 ++-- tests/playwright-test/reporter-base.spec.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/playwright-test/src/reporters/base.ts b/packages/playwright-test/src/reporters/base.ts index 8ba830a975..cdfda403e9 100644 --- a/packages/playwright-test/src/reporters/base.ts +++ b/packages/playwright-test/src/reporters/base.ts @@ -258,8 +258,8 @@ export function formatFailure(config: FullConfig, test: TestCase, options: {inde resultLines.push(''); } } else { - if (attachment.contentType.startsWith('text/')) { - let text = attachment.body!.toString(); + if (attachment.contentType.startsWith('text/') && attachment.body) { + let text = attachment.body.toString(); if (text.length > 300) text = text.slice(0, 300) + '...'; resultLines.push(colors.cyan(` ${text}`)); diff --git a/tests/playwright-test/reporter-base.spec.ts b/tests/playwright-test/reporter-base.spec.ts index 531ab5eefc..834f327003 100644 --- a/tests/playwright-test/reporter-base.spec.ts +++ b/tests/playwright-test/reporter-base.spec.ts @@ -270,3 +270,22 @@ test('should print "no tests found" error', async ({ runInlineTest }) => { expect(result.exitCode).toBe(1); expect(result.output).toContain('no tests found.'); }); + +test('should not crash on undefined body with manual attachments', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'a.test.js': ` + const { test } = pwt; + test('one', async ({}, testInfo) => { + testInfo.attachments.push({ + name: 'foo.txt', + body: undefined, + contentType: 'text/plain' + }); + expect(1).toBe(2); + }); + `, + }); + expect(stripAnsi(result.output)).not.toContain('Error in reporter'); + expect(result.failed).toBe(1); + expect(result.exitCode).toBe(1); +});