Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(expect): support expect.soft.poll chain (#40848)
Pavel Feldmandaa2146d412c
diff --git a/docs/src/test-assertions-js.md b/docs/src/test-assertions-js.mdindex a1f2f8d727..69edc341e8 100644--- a/docs/src/test-assertions-js.md+++ b/docs/src/test-assertions-js.md@@ -234,16 +234,24 @@ await expect.poll(async () => {}).toBe(200);```-You can combine `expect.configure({ soft: true })` with expect.poll to perform soft assertions in polling logic.+You can combine `expect.soft` with `expect.poll` to perform soft assertions in polling logic. This allows the test to continue even if the assertion inside poll fails.++```js+await expect.soft.poll(async () => {+ const response = await page.request.get('https://api.example.com');+ return response.status();+}).toBe(200);+```++`expect.configure({ soft: true })` also chains with `expect.poll` and is useful when you want to reuse a configured instance.```jsconst softExpect = expect.configure({ soft: true });await softExpect.poll(async () => {const response = await page.request.get('https://api.example.com');return response.status();-}, {}).toBe(200);+}).toBe(200);```-This allows the test to continue even if the assertion inside poll fails.## expect.toPassdiff --git a/packages/playwright/src/matchers/expect.ts b/packages/playwright/src/matchers/expect.tsindex ade895df50..c4de97609d 100644--- a/packages/playwright/src/matchers/expect.ts+++ b/packages/playwright/src/matchers/expect.ts@@ -268,9 +268,11 @@ function createExpect(info: ExpectMetaInfo): Expect<{}> {return createExpect(newInfo);};- expectFn.soft = (actual: unknown, messageOrOptions?: ExpectMessage) => {- return createMatchers(actual, { ... info, isSoft: true }, messageOrOptions);- };+ Object.defineProperty(expectFn, 'soft', {+ configurable: true,+ enumerable: true,+ get: () => info.isSoft ? expectFn : createExpect({ ...info, isSoft: true }),+ });expectFn.poll = (actual: unknown, messageOrOptions?: ExpectMessage & { timeout?: number, intervals?: number[] }) => {const poll = isString(messageOrOptions) ? {} : messageOrOptions || {};diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.tsindex b5d167754d..dc5bf13625 100644--- a/packages/playwright/types/test.d.ts+++ b/packages/playwright/types/test.d.ts@@ -8557,7 +8557,7 @@ type PollMatchers<R, T, ExtendedMatchers> = {export type Expect<ExtendedMatchers = {}> = {<T = unknown>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, ExtendedMatchers>;- soft: <T = unknown>(actual: T, messageOrOptions?: string | { message?: string }) => MakeMatchers<void, T, ExtendedMatchers>;+ soft: Expect<ExtendedMatchers>;poll: <T = unknown>(actual: () => T | Promise<T>, messageOrOptions?: string | { message?: string, timeout?: number, intervals?: number[] }) => PollMatchers<Promise<void>, T, ExtendedMatchers>;extend<MoreMatchers extends Record<string, (this: ExpectMatcherState, receiver: any, ...args: any[]) => MatcherReturnType | Promise<MatcherReturnType>>>(matchers: MoreMatchers): Expect<ExtendedMatchers & MoreMatchers>;configure: (configuration: {diff --git a/tests/playwright-test/expect-configure.spec.ts b/tests/playwright-test/expect-configure.spec.tsindex b0bf725cd6..0abdea5dac 100644--- a/tests/playwright-test/expect-configure.spec.ts+++ b/tests/playwright-test/expect-configure.spec.ts@@ -152,3 +152,34 @@ test('should configure soft after poll', async ({ runInlineTest }) => {});expect(result.exitCode).toBe(0);});++test('should support expect.soft.poll', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'a.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('should fail softly', async () => {+ let probes = 0;+ const startTime = Date.now();+ await expect.soft.poll(() => ++probes, { timeout: 1000, intervals: [0, 10000] }).toBe(3);+ expect(probes).toBe(2);+ expect(Date.now() - startTime).toBeLessThan(5000);+ console.log('%% reached-end');+ });+ `+ });+ expect(result.exitCode).toBe(1);+ expect(result.outputLines).toEqual(['reached-end']);+});++test('should support expect.soft.poll in passing test', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'a.spec.ts': `+ import { test, expect } from '@playwright/test';+ test('should pass', async () => {+ let probes = 0;+ await expect.soft.poll(() => ++probes).toBe(3);+ });+ `+ });+ expect(result.exitCode).toBe(0);+});diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.tsindex 0c43f4ddc0..f66f4c0039 100644--- a/utils/generate_types/overrides-test.d.ts+++ b/utils/generate_types/overrides-test.d.ts@@ -481,7 +481,7 @@ type PollMatchers<R, T, ExtendedMatchers> = {export type Expect<ExtendedMatchers = {}> = {<T = unknown>(actual: T, messageOrOptions?: string | { message?: string }): MakeMatchers<void, T, ExtendedMatchers>;- soft: <T = unknown>(actual: T, messageOrOptions?: string | { message?: string }) => MakeMatchers<void, T, ExtendedMatchers>;+ soft: Expect<ExtendedMatchers>;poll: <T = unknown>(actual: () => T | Promise<T>, messageOrOptions?: string | { message?: string, timeout?: number, intervals?: number[] }) => PollMatchers<Promise<void>, T, ExtendedMatchers>;extend<MoreMatchers extends Record<string, (this: ExpectMatcherState, receiver: any, ...args: any[]) => MatcherReturnType | Promise<MatcherReturnType>>>(matchers: MoreMatchers): Expect<ExtendedMatchers & MoreMatchers>;configure: (configuration: {