Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: trim long expect messages (#20517)

Yury Semikhatskya03f3223c4e8
diff --git a/packages/playwright-test/src/matchers/expect.ts b/packages/playwright-test/src/matchers/expect.ts
index f484add649..9313e1bca2 100644
--- a/packages/playwright-test/src/matchers/expect.ts
+++ b/packages/playwright-test/src/matchers/expect.ts
@@ -43,7 +43,7 @@ import {
import { toMatchSnapshot, toHaveScreenshot } from './toMatchSnapshot';
import type { Expect } from '../common/types';
import { currentTestInfo, currentExpectTimeout } from '../common/globals';
-import { serializeError, captureStackTrace } from '../util';
+import { serializeError, captureStackTrace, trimLongString } from '../util';
import {
expect as expectLibrary,
INVERTED_COLOR,
@@ -201,7 +201,7 @@ class ExpectMetaInfoProxyHandler {
const step = testInfo._addStep({
location: frame && frame.file ? { file: path.resolve(process.cwd(), frame.file), line: frame.line || 0, column: frame.column || 0 } : undefined,
category: 'expect',
- title: customMessage || defaultTitle,
+ title: trimLongString(customMessage || defaultTitle, 1024),
canHaveChildren: true,
forceNoParent: false
});
diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts
index 5fcc8a86e6..1802a08eb5 100644
--- a/tests/playwright-test/expect.spec.ts
+++ b/tests/playwright-test/expect.spec.ts
@@ -545,3 +545,29 @@ test('should print timed out error message', async ({ runInlineTest }) => {
const output = stripAnsi(result.output);
expect(output).toContain('Timed out 1ms waiting for expect(received).toBeChecked()');
});
+
+test('should not leak long expect message strings', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.ts': `
+ const { test } = pwt;
+
+ let logs: string = 'Ab';
+ const consoleLogWatcher = (msg: ConsoleMessage) => {
+ if (logs.length < (1<<28))
+ logs += logs;
+ expect(msg.text(), logs).toMatch(/^\\d+$/);
+ }
+
+ test('main', async ({ page }) => {
+ page.on('console', consoleLogWatcher);
+ await page.evaluate(() => {
+ for (let i = 0; i < 20; i++)
+ console.log(i);
+ });
+ });
+ `,
+ }, { workers: 1 });
+ // expect(result.output).toBe('');
+ expect(result.failed).toBe(0);
+ expect(result.exitCode).toBe(0);
+});