fix: ensure toMatchAriaSnapshot is properly commented in javascript c… (#33593)

This commit is contained in:
Rui Figueira 2024-11-15 22:44:27 +00:00 committed by GitHub
parent d127255881
commit c36b5a6059
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View File

@ -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} `)});`;
}
}
}

View File

@ -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(`<body style="width:100%;height:100%"><ul><li>item 1</li><li>item 2</li></ul></body>`);
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'));
});
});