Working warning annotation on expect

This commit is contained in:
Adam Gastineau 2025-02-13 11:13:28 -08:00
parent 5028fb6270
commit d3836e9c33
3 changed files with 20 additions and 2 deletions

View file

@ -381,8 +381,22 @@ class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
setMatcherCallContext({ expectInfo: this._info, testInfo, step: step.info });
const callback = () => matcher.call(target, ...args);
const result = zones.run('stepZone', step, callback);
if (result instanceof Promise)
return result.then(finalizer).catch(reportStepError);
if (result instanceof Promise) {
const promise = result.then(finalizer).catch(reportStepError);
testInfo?.unusedAsyncApiCalls.add(promise);
const oldThen = promise.then;
promise.then = ((...args: any[]) => {
if (args[0] !== undefined) {
// onfulfilled callback
testInfo?.unusedAsyncApiCalls.delete(promise);
}
return oldThen.call(promise, ...args);
}) as any;
return promise;
}
finalizer();
return result;
} catch (e) {

View file

@ -99,6 +99,7 @@ export class TestInfoImpl implements TestInfo {
duration: number = 0;
readonly annotations: Annotation[] = [];
readonly attachments: TestInfo['attachments'] = [];
readonly unusedAsyncApiCalls: Set<Promise<any>> = new Set();
status: TestStatus = 'passed';
snapshotSuffix: string = '';
readonly outputDir: string;

View file

@ -369,6 +369,9 @@ export class WorkerMain extends ProcessRunner {
// Now run the test itself.
const fn = test.fn; // Extract a variable to get a better stack trace ("myTest" vs "TestCase.myTest [as fn]").
await fn(testFunctionParams, testInfo);
// Create warning if any of the async calls were not awaited.
if (testInfo.unusedAsyncApiCalls.size > 0)
testInfo.annotations.push({ type: 'warning', description: 'Some async calls were not awaited by the end of the test. This can cause flakiness.' });
});
}).catch(() => {}); // Ignore the top-level error, it is already inside TestInfo.errors.