From c36b5a6059ee46a8c0e59b85d42fd99f6b85ec8a Mon Sep 17 00:00:00 2001 From: Rui Figueira Date: Fri, 15 Nov 2024 22:44:27 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20ensure=20toMatchAriaSnapshot=20is=20prop?= =?UTF-8?q?erly=20commented=20in=20javascript=20c=E2=80=A6=20(#33593)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/server/codegen/javascript.ts | 6 ++-- .../inspector/cli-codegen-aria.spec.ts | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/playwright-core/src/server/codegen/javascript.ts b/packages/playwright-core/src/server/codegen/javascript.ts index 428ca493f8..e5f72ce122 100644 --- a/packages/playwright-core/src/server/codegen/javascript.ts +++ b/packages/playwright-core/src/server/codegen/javascript.ts @@ -117,8 +117,10 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator { const assertion = action.value ? `toHaveValue(${quote(action.value)})` : `toBeEmpty()`; return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).${assertion};`; } - case 'assertSnapshot': - return `${this._isTest ? '' : '// '}await expect(${subject}.${this._asLocator(action.selector)}).toMatchAriaSnapshot(${quoteMultiline(action.snapshot)});`; + case 'assertSnapshot': { + const commentIfNeeded = this._isTest ? '' : '// '; + return `${commentIfNeeded}await expect(${subject}.${this._asLocator(action.selector)}).toMatchAriaSnapshot(${quoteMultiline(action.snapshot, `${commentIfNeeded} `)});`; + } } } diff --git a/tests/library/inspector/cli-codegen-aria.spec.ts b/tests/library/inspector/cli-codegen-aria.spec.ts index 820dab7c14..093ab0bbe5 100644 --- a/tests/library/inspector/cli-codegen-aria.spec.ts +++ b/tests/library/inspector/cli-codegen-aria.spec.ts @@ -141,4 +141,35 @@ test.describe(() => { // 3 highlighted tokens. await expect(recorder.recorderPage.locator('.source-line-error-underline')).toHaveCount(3); }); + + test('should generate valid javascript with multiline snapshot assertion', async ({ openRecorder }) => { + const { recorder } = await openRecorder(); + // set width and height to 100% to ensure click is outside of the list + await recorder.setContentAndWait(``); + + await recorder.page.click('x-pw-tool-item.snapshot'); + await recorder.page.hover('body'); + await recorder.trustedClick(); + + // playwright tests assertions are uncommented + await expect.poll(() => + recorder.text('Playwright Test')).toContain([ + ` await expect(page.locator('body')).toMatchAriaSnapshot(\``, + ` - list:`, + ` - listitem: item 1`, + ` - listitem: item 2`, + ` \`);`, + ].join('\n')); + + // non-test javascript has commented assertions + await expect.poll(() => + recorder.text('JavaScript')).toContain([ + ` // await expect(page.locator('body')).toMatchAriaSnapshot(\``, + ` // - list:`, + ` // - listitem: item 1`, + ` // - listitem: item 2`, + ` // \`);`, + ].join('\n')); + + }); });