feat(expect): expose expect timeout
Fixes https://github.com/microsoft/playwright/issues/30583
This commit is contained in:
parent
2cbd7b78ea
commit
680a2ea3da
|
|
@ -129,7 +129,14 @@ function createExpect(info: ExpectMetaInfo) {
|
||||||
|
|
||||||
if (property === 'extend') {
|
if (property === 'extend') {
|
||||||
return (matchers: any) => {
|
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;
|
return expectInstance;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
1
packages/playwright/types/test.d.ts
vendored
1
packages/playwright/types/test.d.ts
vendored
|
|
@ -6513,6 +6513,7 @@ export type ExpectMatcherState = {
|
||||||
isNot: boolean;
|
isNot: boolean;
|
||||||
promise: 'rejects' | 'resolves' | '';
|
promise: 'rejects' | 'resolves' | '';
|
||||||
utils: ExpectMatcherUtils;
|
utils: ExpectMatcherUtils;
|
||||||
|
timeout: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type MatcherReturnType = {
|
export type MatcherReturnType = {
|
||||||
|
|
|
||||||
|
|
@ -1000,3 +1000,42 @@ test('should respect timeout from configured expect when used outside of the tes
|
||||||
expect(stdout).toBe('');
|
expect(stdout).toBe('');
|
||||||
expect(stripAnsi(stderr)).toContain('Timed out 10ms waiting for expect(locator).toBeAttached()');
|
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);
|
||||||
|
});
|
||||||
|
|
|
||||||
1
utils/generate_types/overrides-test.d.ts
vendored
1
utils/generate_types/overrides-test.d.ts
vendored
|
|
@ -361,6 +361,7 @@ export type ExpectMatcherState = {
|
||||||
isNot: boolean;
|
isNot: boolean;
|
||||||
promise: 'rejects' | 'resolves' | '';
|
promise: 'rejects' | 'resolves' | '';
|
||||||
utils: ExpectMatcherUtils;
|
utils: ExpectMatcherUtils;
|
||||||
|
timeout: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type MatcherReturnType = {
|
export type MatcherReturnType = {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue