From 9d03a85c3039fcc968aa7d29777bbca85789fd4a Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 19 Oct 2021 13:46:56 -0800 Subject: [PATCH] chore: make web-first matchers work outside of pw tests (#9624) --- packages/playwright-test/src/matchers/toBeTruthy.ts | 6 ++---- packages/playwright-test/src/matchers/toEqual.ts | 6 ++---- packages/playwright-test/src/matchers/toMatchText.ts | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/playwright-test/src/matchers/toBeTruthy.ts b/packages/playwright-test/src/matchers/toBeTruthy.ts index 226cb284e1..f48380628f 100644 --- a/packages/playwright-test/src/matchers/toBeTruthy.ts +++ b/packages/playwright-test/src/matchers/toBeTruthy.ts @@ -27,9 +27,6 @@ export async function toBeTruthy( query: (isNot: boolean, timeout: number) => Promise<{ matches: boolean, log?: string[] }>, options: { timeout?: number } = {}, ) { - const testInfo = currentTestInfo(); - if (!testInfo) - throw new Error(`${matcherName} must be called during the test`); expectType(receiver, receiverType, matcherName); const matcherOptions = { @@ -37,7 +34,8 @@ export async function toBeTruthy( promise: this.promise, }; - let defaultExpectTimeout = testInfo.project.expect?.timeout; + const testInfo = currentTestInfo(); + let defaultExpectTimeout = testInfo?.project.expect?.timeout; if (typeof defaultExpectTimeout === 'undefined') defaultExpectTimeout = 5000; const timeout = options.timeout === 0 ? 0 : options.timeout || defaultExpectTimeout; diff --git a/packages/playwright-test/src/matchers/toEqual.ts b/packages/playwright-test/src/matchers/toEqual.ts index 9c12242c08..a1f0d4cb6a 100644 --- a/packages/playwright-test/src/matchers/toEqual.ts +++ b/packages/playwright-test/src/matchers/toEqual.ts @@ -35,9 +35,6 @@ export async function toEqual( expected: T, options: { timeout?: number, contains?: boolean } = {}, ) { - const testInfo = currentTestInfo(); - if (!testInfo) - throw new Error(`${matcherName} must be called during the test`); expectType(receiver, receiverType, matcherName); const matcherOptions = { @@ -46,7 +43,8 @@ export async function toEqual( promise: this.promise, }; - let defaultExpectTimeout = testInfo.project.expect?.timeout; + const testInfo = currentTestInfo(); + let defaultExpectTimeout = testInfo?.project.expect?.timeout; if (typeof defaultExpectTimeout === 'undefined') defaultExpectTimeout = 5000; const timeout = options.timeout === 0 ? 0 : options.timeout || defaultExpectTimeout; diff --git a/packages/playwright-test/src/matchers/toMatchText.ts b/packages/playwright-test/src/matchers/toMatchText.ts index e45faddcac..c2139256d8 100644 --- a/packages/playwright-test/src/matchers/toMatchText.ts +++ b/packages/playwright-test/src/matchers/toMatchText.ts @@ -35,9 +35,6 @@ export async function toMatchText( expected: string | RegExp, options: { timeout?: number, matchSubstring?: boolean } = {}, ) { - const testInfo = currentTestInfo(); - if (!testInfo) - throw new Error(`${matcherName} must be called during the test`); expectType(receiver, receiverType, matcherName); const matcherOptions = { @@ -60,7 +57,8 @@ export async function toMatchText( ); } - let defaultExpectTimeout = testInfo.project.expect?.timeout; + const testInfo = currentTestInfo(); + let defaultExpectTimeout = testInfo?.project.expect?.timeout; if (typeof defaultExpectTimeout === 'undefined') defaultExpectTimeout = 5000; const timeout = options.timeout === 0 ? 0 : options.timeout || defaultExpectTimeout;