fix: trim long expect messages (#20517)

Fixes #20324
This commit is contained in:
Yury Semikhatsky 2023-01-30 17:24:12 -08:00 committed by GitHub
parent 0dd090aeab
commit a03f3223c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View file

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

View file

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