feat(expect): expose expect timeout

Fixes https://github.com/microsoft/playwright/issues/30583
This commit is contained in:
Yury Semikhatsky 2024-05-22 12:49:07 -07:00
parent 2cbd7b78ea
commit 680a2ea3da
4 changed files with 49 additions and 1 deletions

View file

@ -129,7 +129,14 @@ function createExpect(info: ExpectMetaInfo) {
if (property === 'extend') {
return (matchers: any) => {
expectLibrary.extend(matchers);
const wrappedMatchers: any = {};
Object.entries(matchers).forEach(([name, matcher]) => {
wrappedMatchers[name] = function (...args: any[]) {
this.timeout = currentExpectTimeout({});
return (matcher as any).call(this, ...args);
};
});
expectLibrary.extend(wrappedMatchers);
return expectInstance;
};
}

View file

@ -6513,6 +6513,7 @@ export type ExpectMatcherState = {
isNot: boolean;
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
timeout: number;
};
export type MatcherReturnType = {

View file

@ -1000,3 +1000,42 @@ test('should respect timeout from configured expect when used outside of the tes
expect(stdout).toBe('');
expect(stripAnsi(stderr)).toContain('Timed out 10ms waiting for expect(locator).toBeAttached()');
});
test('should expose timeout to custom matchers', async ({ runInlineTest, runTSC }) => {
const files = {
'playwright.config.ts': `
export default {
expect: { timeout: 1100 }
};
`,
'a.test.ts': `
import type { ExpectMatcherState, MatcherReturnType } from '@playwright/test';
import { test, expect as base } from '@playwright/test';
const expect = base.extend<{assertTimeout: (this: ExpectMatcherState, page: any, value: number) => MatcherReturnType}>({
assertTimeout(page: any, value: number) {
const pass = this.timeout === value;
return {
message: () => 'Unexpected timeout: ' + this.timeout,
pass,
name: 'assertTimeout',
};
}
});
test('from config', async ({ page }) => {
expect(page).assertTimeout(1100);
});
test('from expect.configure', async ({ page }) => {
expect.configure({ timeout: 2200 })(page).assertTimeout(2200);
});
`,
};
const { exitCode } = await runTSC(files);
expect(exitCode).toBe(0);
const result = await runInlineTest(files);
expect(result.exitCode).toBe(0);
expect(result.failed).toBe(0);
expect(result.passed).toBe(2);
});

View file

@ -361,6 +361,7 @@ export type ExpectMatcherState = {
isNot: boolean;
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
timeout: number;
};
export type MatcherReturnType = {