From 755edfba5be2ad0f70366631d34b3e846bbfc6e9 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 24 Sep 2024 14:00:13 -0700 Subject: [PATCH] chore: fallback expect.extend to legacy (#32795) --- packages/playwright/src/matchers/expect.ts | 5 +-- tests/playwright-test/expect.spec.ts | 36 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/playwright/src/matchers/expect.ts b/packages/playwright/src/matchers/expect.ts index 3a254ae7bfb3f..16300607d9961 100644 --- a/packages/playwright/src/matchers/expect.ts +++ b/packages/playwright/src/matchers/expect.ts @@ -140,8 +140,7 @@ function createExpect(info: ExpectMetaInfo, prefix: string[], customMatchers: Re const wrappedMatchers: any = {}; const extendedMatchers: any = { ...customMatchers }; for (const [name, matcher] of Object.entries(matchers)) { - const key = qualifiedMatcherName(qualifier, name); - wrappedMatchers[key] = function(...args: any[]) { + wrappedMatchers[name] = function(...args: any[]) { const { isNot, promise, utils } = this; const newThis: ExpectMatcherState = { isNot, @@ -152,6 +151,8 @@ function createExpect(info: ExpectMetaInfo, prefix: string[], customMatchers: Re (newThis as any).equals = throwUnsupportedExpectMatcherError; return (matcher as any).call(newThis, ...args); }; + const key = qualifiedMatcherName(qualifier, name); + wrappedMatchers[key] = wrappedMatchers[name]; Object.defineProperty(wrappedMatchers[key], 'name', { value: name }); extendedMatchers[name] = wrappedMatchers[key]; } diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index a541ad9c96324..8f82c256fbdb1 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -1068,4 +1068,38 @@ test('expect.extend should be immutable', async ({ runInlineTest }) => { 'foo', 'bar', ]); -}); \ No newline at end of file +}); + +test('expect.extend should fall back to legacy behavior', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'expect-test.spec.ts': ` + import { test, expect } from '@playwright/test'; + expect.extend({ + toFoo() { + console.log('%%foo'); + return { pass: true }; + } + }); + expect.extend({ + toFoo() { + console.log('%%foo2'); + return { pass: true }; + } + }); + expect.extend({ + toBar() { + console.log('%%bar'); + return { pass: true }; + } + }); + test('logs', () => { + expect().toFoo(); + expect().toBar(); + }); + ` + }); + expect(result.outputLines).toEqual([ + 'foo2', + 'bar', + ]); +});