chore: better zone stack preservation in expect (#22563)

This commit is contained in:
Pavel Feldman 2023-04-21 16:13:35 -07:00 committed by GitHub
parent becb072703
commit 17df03c043
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -130,7 +130,8 @@ expect.poll = (actual: unknown, messageOrOptions: ExpectMessageOrOptions) => {
}; };
expectLibrary.setState({ expand: false }); expectLibrary.setState({ expand: false });
const customMatchers = {
const customAsyncMatchers = {
toBeAttached, toBeAttached,
toBeChecked, toBeChecked,
toBeDisabled, toBeDisabled,
@ -154,11 +155,15 @@ const customMatchers = {
toHaveURL, toHaveURL,
toHaveValue, toHaveValue,
toHaveValues, toHaveValues,
toMatchSnapshot,
toHaveScreenshot, toHaveScreenshot,
toPass, toPass,
}; };
const customMatchers = {
...customAsyncMatchers,
toMatchSnapshot,
};
type Generator = () => any; type Generator = () => any;
type ExpectMetaInfo = { type ExpectMetaInfo = {
@ -198,7 +203,7 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
return new Proxy(matcher, this); return new Proxy(matcher, this);
} }
if (this._info.isPoll) { if (this._info.isPoll) {
if ((customMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects') if ((customAsyncMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects')
throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`); 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); matcher = (...args: any[]) => pollMatcher(matcherName, this._info.isNot, this._info.pollIntervals, currentExpectTimeout({ timeout: this._info.pollTimeout }), this._info.generator!, ...args);
} }
@ -267,17 +272,27 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
step.complete({}); step.complete({});
}; };
try { // Process the async matchers separately to preserve the zones in the stacks.
const expectZone: ExpectZone = { title: defaultTitle, wallTime }; if (this._info.isPoll || matcherName in customAsyncMatchers) {
const result = zones.run<ExpectZone, any>('expectZone', expectZone, () => { return (async () => {
return matcher.call(target, ...args); try {
}); const expectZone: ExpectZone = { title: defaultTitle, wallTime };
if (result instanceof Promise) await zones.run<ExpectZone, any>('expectZone', expectZone, async () => {
return result.then(() => finalizer()).catch(reportStepError); await matcher.call(target, ...args);
else });
finalizer();
} catch (e) {
reportStepError(e);
}
})();
} else {
try {
const result = matcher.call(target, ...args);
finalizer(); finalizer();
} catch (e) { return result;
reportStepError(e); } catch (e) {
reportStepError(e);
}
} }
}; };
} }