Skip to content

Commit

Permalink
feat(expect): expose expect timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s committed May 23, 2024
1 parent 2cbd7b7 commit 680a2ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/playwright/src/matchers/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/playwright/types/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6513,6 +6513,7 @@ export type ExpectMatcherState = {
isNot: boolean;
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
timeout: number;
};

export type MatcherReturnType = {
Expand Down
39 changes: 39 additions & 0 deletions tests/playwright-test/expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
1 change: 1 addition & 0 deletions utils/generate_types/overrides-test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ export type ExpectMatcherState = {
isNot: boolean;
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
timeout: number;
};

export type MatcherReturnType = {
Expand Down

0 comments on commit 680a2ea

Please sign in to comment.