Properly handle .not in toHaveURL

This commit is contained in:
Adam Gastineau 2025-01-21 07:47:43 -08:00
parent 26d3a77946
commit efd3ab04a2

View file

@ -21,7 +21,7 @@ import { expectTypes, callLogText } from '../util';
import { toBeTruthy } from './toBeTruthy'; import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual'; import { toEqual } from './toEqual';
import { toMatchText } from './toMatchText'; import { toMatchText } from './toMatchText';
import { constructURLBasedOnBaseURL, isRegExp, isString, isTextualMimeType, pollAgainstDeadline, serializeExpectedTextValues } from 'playwright-core/lib/utils'; import { constructURLBasedOnBaseURL, isRegExp, isString, isTextualMimeType, pollAgainstDeadline, serializeExpectedTextValues, urlMatches } from 'playwright-core/lib/utils';
import { currentTestInfo } from '../common/globals'; import { currentTestInfo } from '../common/globals';
import { TestInfoImpl } from '../worker/testInfo'; import { TestInfoImpl } from '../worker/testInfo';
import type { ExpectMatcherState } from '../../types/test'; import type { ExpectMatcherState } from '../../types/test';
@ -406,23 +406,27 @@ export async function toHaveURL2(
); );
const timeout = options?.timeout ?? this.timeout; const timeout = options?.timeout ?? this.timeout;
let urlMatched = false; let conditionSucceeded = false;
try { try {
await page.mainFrame().waitForURL(expected, { timeout }); await page.mainFrame().waitForURL(url => {
urlMatched = true; const baseURL = (page.context() as any)._options.baseURL;
return !this.isNot === urlMatches(baseURL, url.toString(), expected);
}, { timeout });
conditionSucceeded = true;
} catch (e) { } catch (e) {
urlMatched = false; conditionSucceeded = false;
} }
if (urlMatched !== this.isNot) if (conditionSucceeded)
return { pass: urlMatched, message: () => '' }; return { pass: !this.isNot, message: () => '' };
const matcherOptions = { const matcherOptions = {
isNot: this.isNot, isNot: this.isNot,
promise: this.promise, promise: this.promise,
}; };
return { return {
pass: urlMatched, pass: this.isNot,
message: () => message: () =>
matcherHint( matcherHint(
this, this,