Normalize error "expected" text

This commit is contained in:
Adam Gastineau 2025-01-17 12:19:32 -08:00
parent afd19da215
commit 26d3a77946
3 changed files with 8 additions and 7 deletions

View file

@ -20,7 +20,7 @@ import { colors } from 'playwright-core/lib/utilsBundle';
import type { Locator } from 'playwright-core';
import { EXPECTED_COLOR } from '../common/expectBundle';
export function toMatchExpectedVerification(
export function toMatchExpectedStringOrPredicateVerification(
state: ExpectMatcherState,
matcherName: string,
receiver: Locator | undefined,
@ -42,7 +42,8 @@ export function toMatchExpectedVerification(
const message = supportsPredicate ? 'string, regular expression, or predicate' : 'string or regular expression';
throw new Error([
matcherHint(state, receiver, matcherName, expression, expected, matcherOptions),
// Always display `expected` in expectation place
matcherHint(state, receiver, matcherName, expression, undefined, matcherOptions),
`${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected',)} value must be a ${message}`,
state.utils.printWithType('Expected', expected, state.utils.printExpected)
].join('\n\n'));

View file

@ -27,7 +27,7 @@ import { TestInfoImpl } from '../worker/testInfo';
import type { ExpectMatcherState } from '../../types/test';
import { takeFirst } from '../common/config';
import { matcherHint } from './matcherHint';
import { toMatchExpectedVerification } from './error';
import { toMatchExpectedStringOrPredicateVerification } from './error';
export interface LocatorEx extends Locator {
_expect(expression: string, options: FrameExpectParams): Promise<{ matches: boolean, received?: any, log?: string[], timedOut?: boolean }>;
@ -396,7 +396,7 @@ export async function toHaveURL2(
) {
const matcherName = 'toHaveURL';
const expression = 'page';
toMatchExpectedVerification(
toMatchExpectedStringOrPredicateVerification(
this,
matcherName,
undefined,
@ -429,7 +429,7 @@ export async function toHaveURL2(
undefined,
matcherName,
expression,
typeof expected === 'function' ? 'predicate' : expected,
undefined,
matcherOptions,
timeout,
),

View file

@ -24,7 +24,7 @@ import type { ExpectMatcherState } from '../../types/test';
import { kNoElementsFoundError, matcherHint } from './matcherHint';
import type { MatcherResult } from './matcherHint';
import type { Locator } from 'playwright-core';
import { toMatchExpectedVerification } from './error';
import { toMatchExpectedStringOrPredicateVerification } from './error';
export async function toMatchText(
this: ExpectMatcherState,
@ -36,7 +36,7 @@ export async function toMatchText(
options: { timeout?: number, matchSubstring?: boolean } = {},
): Promise<MatcherResult<string | RegExp, string>> {
expectTypes(receiver, [receiverType], matcherName);
toMatchExpectedVerification(this, matcherName, receiver, receiver, expected);
toMatchExpectedStringOrPredicateVerification(this, matcherName, receiver, receiver, expected);
const timeout = options.timeout ?? this.timeout;