Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions docs/src/test-assertions-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

```js
const 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.toPass

Expand Down
8 changes: 5 additions & 3 deletions packages/playwright/src/matchers/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
31 changes: 31 additions & 0 deletions tests/playwright-test/expect-configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
2 changes: 1 addition & 1 deletion utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Loading