feat(expect): ensure it works in global setup (#13896)

This commit is contained in:
Dmitry Gozman 2022-05-03 21:53:15 +01:00 committed by GitHub
parent b5183b4cf9
commit 85b86e19b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View file

@ -172,7 +172,7 @@ class ExpectMetaInfoProxyHandler {
}
get(target: any, matcherName: any, receiver: any): any {
const matcher = Reflect.get(target, matcherName, receiver);
let matcher = Reflect.get(target, matcherName, receiver);
if (matcher === undefined)
throw new Error(`expect: Property '${matcherName}' not found.`);
if (typeof matcher !== 'function') {
@ -180,6 +180,11 @@ class ExpectMetaInfoProxyHandler {
this._info.isNot = !this._info.isNot;
return new Proxy(matcher, this);
}
if (this._info.isPoll) {
if ((customMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects')
throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`);
matcher = (...args: any[]) => pollMatcher(matcherName, this._info.isNot, this._info.pollIntervals, currentExpectTimeout({ timeout: this._info.pollTimeout }), this._info.generator!, ...args);
}
return (...args: any[]) => {
const testInfo = currentTestInfo();
if (!testInfo)
@ -231,14 +236,7 @@ class ExpectMetaInfoProxyHandler {
};
try {
let result;
if (this._info.isPoll) {
if ((customMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects')
throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`);
result = pollMatcher(matcherName, this._info.isNot, this._info.pollIntervals, currentExpectTimeout({ timeout: this._info.pollTimeout }), this._info.generator!, ...args);
} else {
result = matcher.call(target, ...args);
}
const result = matcher.call(target, ...args);
if ((result instanceof Promise))
return result.then(() => step.complete({})).catch(reportStepError);
else

View file

@ -348,3 +348,29 @@ test.describe('helpful expect errors', () => {
expect(result.passed).toBe(1);
});
});
test('should reasonably work in global setup', async ({ runInlineTest }) => {
const result = await runInlineTest({
'playwright.config.ts': `
export default { globalSetup: './global-setup' };
`,
'global-setup.ts': `
const { expect } = pwt;
export default async () => {
expect(1).toBe(1);
await expect.poll(async () => {
await new Promise(f => setTimeout(f, 50));
return 42;
}).toBe(42);
expect(1).toBe(2);
};
`,
'a.spec.ts': `
const { test } = pwt;
test('skipped', () => {});
`,
});
expect(result.exitCode).toBe(1);
expect(stripAnsi(result.output)).toContain('> 11 | expect(1).toBe(2);');
});