Skip to content

Commit

Permalink
feat(expect): add toPass option to playwright config file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonghoon.park committed Nov 18, 2023
1 parent 3f55587 commit 041785f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/src/test-api/class-testconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default defineConfig({
- `threshold` ?<[float]> an acceptable perceived color difference between the same pixel in compared images, ranging from `0` (strict) and `1` (lax). `"pixelmatch"` comparator computes color difference in [YIQ color space](https://en.wikipedia.org/wiki/YIQ) and defaults `threshold` value to `0.2`.
- `maxDiffPixels` ?<[int]> an acceptable amount of pixels that could be different, unset by default.
- `maxDiffPixelRatio` ?<[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
- `toPass` ?<[Object]> Configuration for the [expect(value).toPass([, options])](https://playwright.dev/docs/test-assertions#expecttopass) method.
- `timeout` ?<[int]> timeout for toPass method in milliseconds.
- `intervals` ?<[Array]<[int]>> set custom retry intervals.

This comment has been minimized.

Copy link
@injae-kim

injae-kim Nov 19, 2023

(PR naming) feat(expect): add toPass option to playwright config file
vs feat(expect): Make toPass's option configurable by TestConfig


Configuration for the `expect` assertion library. Learn more about [various timeouts](../test-timeouts.md).

Expand Down
3 changes: 3 additions & 0 deletions docs/src/test-api/class-testproject.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export default defineConfig({
- `threshold` ?<[float]> an acceptable perceived color difference between the same pixel in compared images, ranging from `0` (strict) and `1` (lax). `"pixelmatch"` comparator computes color difference in [YIQ color space](https://en.wikipedia.org/wiki/YIQ) and defaults `threshold` value to `0.2`.
- `maxDiffPixels` ?<[int]> an acceptable amount of pixels that could be different, unset by default.
- `maxDiffPixelRatio` ?<[float]> an acceptable ratio of pixels that are different to the total amount of pixels, between `0` and `1` , unset by default.
- `toPass` ?<[Object]> Configuration for the [expect(value).toPass([, options])](https://playwright.dev/docs/test-assertions#expecttopass) method.
- `timeout` ?<[int]> timeout for toPass method in milliseconds.
- `intervals` ?<[Array]<[int]>> set custom retry intervals.

Configuration for the `expect` assertion library.

Expand Down
3 changes: 2 additions & 1 deletion packages/playwright/src/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { captureRawStack, constructURLBasedOnBaseURL, isRegExp, isTextualMimeTyp
import { currentTestInfo } from '../common/globals';
import { TestInfoImpl, type TestStepInternal } from '../worker/testInfo';
import type { ExpectMatcherContext } from './expect';
import { takeFirst } from '../common/config';

interface LocatorEx extends Locator {
_expect(expression: string, options: Omit<FrameExpectOptions, 'expectedValue'> & { expectedValue?: any }): Promise<{ matches: boolean, received?: any, log?: string[], timedOut?: boolean }>;
Expand Down Expand Up @@ -367,7 +368,7 @@ export async function toPass(
} = {},
) {
const testInfo = currentTestInfo();
const timeout = options.timeout !== undefined ? options.timeout : 0;
const timeout = takeFirst(options.timeout, testInfo?._projectInternal.expect?.toPass?.timeout, 0);

const rawStack = captureRawStack();
const stackFrames = filteredStackTrace(rawStack);
Expand Down
32 changes: 32 additions & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,22 @@ interface TestConfig {
*/
maxDiffPixelRatio?: number;
};

/**
* Configuration for the [expect(value).toPass([, options])](https://playwright.dev/docs/test-assertions#expecttopass)
* method.
*/
toPass?: {
/**
* timeout for toPass method in milliseconds.
*/
timeout?: number;

/**
* set custom retry intervals.
*/
intervals?: Array<number>;
};
};

/**
Expand Down Expand Up @@ -6763,6 +6779,22 @@ interface TestProject {
*/
maxDiffPixelRatio?: number;
};

/**
* Configuration for the [expect(value).toPass([, options])](https://playwright.dev/docs/test-assertions#expecttopass)
* method.
*/
toPass?: {
/**
* timeout for toPass method in milliseconds.
*/
timeout?: number;

/**
* set custom retry intervals.
*/
intervals?: Array<number>;
};
};

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/playwright-test/expect-to-pass.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,25 @@ test('should show intermediate result for toPass that spills over test time', as
expect(result.output).toContain('Expected: 2');
expect(result.output).toContain('Received: 3');
});

test('should respect timeout in config file', async ({ runInlineTest }) => {

This comment has been minimized.

Copy link
@injae-kim

injae-kim Nov 19, 2023

  • how about should respect timeout in config file when timeout parameter is not passed?
  • Also, I think we can add 1 more test like should give priority to timeout parameter over timeout in config file
const result = await runInlineTest({
'playwright.config.js': `module.exports = { expect: { toPass: { timeout: 100 } } }`,
'a.spec.ts': `
import { test, expect } from '@playwright/test';
test('should fail', async () => {
await test.expect(() => {
expect(1).toBe(2);
}).toPass();
});
`
});
expect(result.exitCode).toBe(1);
expect(result.output).toContain('Timeout 100ms exceeded while waiting on the predicate');
expect(result.output).toContain('Received: 1');
expect(result.output).toContain(`
4 | await test.expect(() => {
`.trim());
});


0 comments on commit 041785f

Please sign in to comment.