diff --git a/packages/playwright/src/common/expectBundle.ts b/packages/playwright/src/common/expectBundle.ts index 506606414b..3322c36277 100644 --- a/packages/playwright/src/common/expectBundle.ts +++ b/packages/playwright/src/common/expectBundle.ts @@ -15,9 +15,7 @@ */ export const expect: typeof import('../../bundles/expect/node_modules/expect/build').expect = require('./expectBundleImpl').expect; -export type ExpectMatcherContext = import('../../bundles/expect/node_modules/expect/build').MatcherContext; export const EXPECTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').EXPECTED_COLOR = require('./expectBundleImpl').EXPECTED_COLOR; export const INVERTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').INVERTED_COLOR = require('./expectBundleImpl').INVERTED_COLOR; export const RECEIVED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').RECEIVED_COLOR = require('./expectBundleImpl').RECEIVED_COLOR; -export const matcherErrorMessage: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').matcherErrorMessage = require('./expectBundleImpl').matcherErrorMessage; export const printReceived: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').printReceived = require('./expectBundleImpl').printReceived; diff --git a/packages/playwright/src/matchers/expect.ts b/packages/playwright/src/matchers/expect.ts index 8995120240..ae629fae74 100644 --- a/packages/playwright/src/matchers/expect.ts +++ b/packages/playwright/src/matchers/expect.ts @@ -58,7 +58,6 @@ import { RECEIVED_COLOR, printReceived, } from '../common/expectBundle'; -export type { ExpectMatcherContext } from '../common/expectBundle'; import { zones } from 'playwright-core/lib/utils'; import { TestInfoImpl } from '../worker/testInfo'; import { ExpectError } from './matcherHint'; @@ -132,12 +131,11 @@ function createExpect(info: ExpectMetaInfo) { const wrappedMatchers: any = {}; Object.entries(matchers).forEach(([name, matcher]) => { wrappedMatchers[name] = function(...args: any[]) { - const { isNot, promise, utils, expand } = this; + const { isNot, promise, utils } = this; const newThis: ExpectMatcherState = { isNot, promise, utils, - expand, timeout: currentExpectTimeout({}) }; return (matcher as any).call(newThis, ...args); diff --git a/packages/playwright/src/matchers/toMatchText.ts b/packages/playwright/src/matchers/toMatchText.ts index 8397e7e2b0..790b402d2f 100644 --- a/packages/playwright/src/matchers/toMatchText.ts +++ b/packages/playwright/src/matchers/toMatchText.ts @@ -22,11 +22,12 @@ import { printReceivedStringContainExpectedResult, printReceivedStringContainExpectedSubstring } from './expect'; -import { EXPECTED_COLOR, matcherErrorMessage } from '../common/expectBundle'; +import { EXPECTED_COLOR } from '../common/expectBundle'; import type { ExpectMatcherState } from '../../types/test'; import { kNoElementsFoundError, matcherHint } from './matcherHint'; import type { MatcherResult } from './matcherHint'; import type { Locator } from 'playwright-core'; +import { colors } from 'playwright-core/lib/utilsBundle'; export async function toMatchText( this: ExpectMatcherState, @@ -48,15 +49,12 @@ export async function toMatchText( !(typeof expected === 'string') && !(expected && typeof expected.test === 'function') ) { - throw new Error( - matcherErrorMessage( - matcherHint(this, receiver, matcherName, receiver, expected, matcherOptions), - `${EXPECTED_COLOR( - 'expected', - )} value must be a string or regular expression`, - this.utils.printWithType('Expected', expected, this.utils.printExpected), - ), - ); + // Same format as jest's matcherErrorMessage + throw new Error([ + matcherHint(this, receiver, matcherName, receiver, expected, matcherOptions), + `${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected',)} value must be a string or regular expression`, + this.utils.printWithType('Expected', expected, this.utils.printExpected) + ].join('\n\n')); } const timeout = options.timeout ?? this.timeout; diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 136e504fcd..f426448578 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -6510,10 +6510,20 @@ export interface ExpectMatcherUtils { } export type ExpectMatcherState = { + /** + * Wehther this matcher was called with the negated .not modifier. + */ isNot: boolean; + /** + * - 'rejects' if matcher was called with the promise .rejects modifier + * - 'resolves' if matcher was called with the promise .resolves modifier + * - '' if matcher was not called with a promise modifier + */ promise: 'rejects' | 'resolves' | ''; utils: ExpectMatcherUtils; - expand: boolean; + /** + * Timeout in milliseconds for the assertion to be fulfilled. + */ timeout: number; }; diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index 64b4f64c13..1393bdd3ff 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -1012,7 +1012,7 @@ test('should expose timeout to custom matchers', async ({ runInlineTest, runTSC import type { ExpectMatcherState, MatcherReturnType } from '@playwright/test'; import { test, expect as base } from '@playwright/test'; - const expect = base.extend<{assertTimeout: (this: ExpectMatcherState, page: any, value: number) => MatcherReturnType}>({ + const expect = base.extend({ assertTimeout(page: any, value: number) { const pass = this.timeout === value; return { diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 48f47052dc..b6c5197ea1 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -358,10 +358,20 @@ export interface ExpectMatcherUtils { } export type ExpectMatcherState = { + /** + * Wehther this matcher was called with the negated .not modifier. + */ isNot: boolean; + /** + * - 'rejects' if matcher was called with the promise .rejects modifier + * - 'resolves' if matcher was called with the promise .resolves modifier + * - '' if matcher was not called with a promise modifier + */ promise: 'rejects' | 'resolves' | ''; utils: ExpectMatcherUtils; - expand: boolean; + /** + * Timeout in milliseconds for the assertion to be fulfilled. + */ timeout: number; };