Testing error messages for consistency

This commit is contained in:
Adam Gastineau 2025-01-21 09:58:15 -08:00
parent 883bb5ff1d
commit 7ddbad2987
2 changed files with 28 additions and 1 deletions

View file

@ -14,6 +14,7 @@
* limitations under the License.
*/
import { stripVTControlCharacters } from 'node:util';
import { stripAnsi } from '../config/utils';
import { test, expect } from './pageTest';
@ -243,7 +244,15 @@ test.describe('toHaveURL', () => {
test('fail', async ({ page }) => {
await page.goto('data:text/html,<div>B</div>');
const error = await expect(page).toHaveURL('wrong', { timeout: 1000 }).catch(e => e);
expect(error.message).toContain('expect.toHaveURL with timeout 1000ms');
expect(stripVTControlCharacters(error.message)).toContain('Timed out 1000ms waiting for expect(page).toHaveURL(expected)');
});
test('fail with invalid argument', async ({ page }) => {
await page.goto('data:text/html,<div>B</div>');
// @ts-expect-error
const error = await expect(page).toHaveURL({}).catch(e => e);
expect(stripVTControlCharacters(error.message)).toContain('expect(page).toHaveURL(expected)\n\n\nMatcher error: expected value should be a string, regular expression, or predicate');
expect(stripVTControlCharacters(error.message)).toContain('Expected has type: object\nExpected has value: {}');
});
test('support ignoreCase', async ({ page }) => {

View file

@ -556,6 +556,24 @@ test('should respect expect.timeout', async ({ runInlineTest }) => {
expect(result.passed).toBe(1);
});
test('should support toHaveURL predicate', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
'a.test.ts': `
import { test, expect } from '@playwright/test';
test('predicate', async ({ page }) => {
await page.goto('data:text/html,<div>A</div>');
const error = await expect(page).toHaveURL('data:text/html,<div>B</div>').catch(e => e);
expect(error.message).toContain('expect.toHaveURL with timeout 1000ms');
expect(error.message).toContain('data:text/html,<div>');
});
`,
}, { workers: 1 });
expect(result.exitCode).toBe(0);
expect(result.passed).toBe(1);
});
test('should log scale the time', async ({ runInlineTest }) => {
const result = await runInlineTest({
'a.test.ts': `