diff --git a/packages/playwright-test/src/expect.ts b/packages/playwright-test/src/expect.ts index e302bb0e8c..77d1b88f9c 100644 --- a/packages/playwright-test/src/expect.ts +++ b/packages/playwright-test/src/expect.ts @@ -172,7 +172,7 @@ class ExpectMetaInfoProxyHandler { } get(target: any, matcherName: any, receiver: any): any { - const matcher = Reflect.get(target, matcherName, receiver); + let matcher = Reflect.get(target, matcherName, receiver); if (matcher === undefined) throw new Error(`expect: Property '${matcherName}' not found.`); if (typeof matcher !== 'function') { @@ -180,6 +180,11 @@ class ExpectMetaInfoProxyHandler { this._info.isNot = !this._info.isNot; return new Proxy(matcher, this); } + if (this._info.isPoll) { + if ((customMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects') + throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`); + matcher = (...args: any[]) => pollMatcher(matcherName, this._info.isNot, this._info.pollIntervals, currentExpectTimeout({ timeout: this._info.pollTimeout }), this._info.generator!, ...args); + } return (...args: any[]) => { const testInfo = currentTestInfo(); if (!testInfo) @@ -231,14 +236,7 @@ class ExpectMetaInfoProxyHandler { }; try { - let result; - if (this._info.isPoll) { - if ((customMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects') - throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`); - result = pollMatcher(matcherName, this._info.isNot, this._info.pollIntervals, currentExpectTimeout({ timeout: this._info.pollTimeout }), this._info.generator!, ...args); - } else { - result = matcher.call(target, ...args); - } + const result = matcher.call(target, ...args); if ((result instanceof Promise)) return result.then(() => step.complete({})).catch(reportStepError); else diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index 960f2dc398..4c7051ce88 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -348,3 +348,29 @@ test.describe('helpful expect errors', () => { expect(result.passed).toBe(1); }); }); + +test('should reasonably work in global setup', async ({ runInlineTest }) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + export default { globalSetup: './global-setup' }; + `, + 'global-setup.ts': ` + const { expect } = pwt; + export default async () => { + expect(1).toBe(1); + await expect.poll(async () => { + await new Promise(f => setTimeout(f, 50)); + return 42; + }).toBe(42); + expect(1).toBe(2); + }; + `, + 'a.spec.ts': ` + const { test } = pwt; + test('skipped', () => {}); + `, + }); + + expect(result.exitCode).toBe(1); + expect(stripAnsi(result.output)).toContain('> 11 | expect(1).toBe(2);'); +});