Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore: make web-first matchers work outside of pw tests (#9624)

Pavel Feldman9d03a85c3039
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<T>(
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<T>(
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;