Collect coverage data from all workers when closing integration-tests

The "Merge PDF" integration-tests will (indirectly) invoke `PDFViewerApplication.open` as part of loading the new PDF document, which will end up creating a new `PDFWorker` instance.
Currently worker coverage is only collected at the end of each integration-test, which means that in these cases we miss the coverage data from any "previous" workers.
This commit is contained in:
Jonas Jenwald 2026-06-13 22:54:33 +02:00
parent feec28583d
commit 2dc73ad2a7
2 changed files with 19 additions and 17 deletions

View File

@ -151,19 +151,6 @@ function closePages(pages) {
async function closeSinglePage(page) {
const coverage = await page.evaluate(async () => {
// Collect coverage data from the worker before the document is closed.
let workerCoverage = null;
const handler =
window.PDFViewerApplication.pdfDocument?._transport?.messageHandler;
if (handler) {
try {
workerCoverage = await handler.sendWithPromise(
"GetWorkerCoverage",
null
);
} catch {}
}
// Close the viewer gracefully, and clear local storage to avoid state
// leaking from one test to another.
await window.PDFViewerApplication.testingClose();
@ -175,16 +162,14 @@ async function closeSinglePage(page) {
// logic kicks in (see https://github.com/puppeteer/puppeteer/issues/2427).
return {
page: window.__coverage__ ? JSON.stringify(window.__coverage__) : null,
worker: workerCoverage ? JSON.stringify(workerCoverage) : null,
workers: window.__worker_coverage__?.map(c => JSON.stringify(c)) ?? null,
};
});
if (coverage.page) {
mergeCoverageIntoGlobal(JSON.parse(coverage.page));
}
if (coverage.worker) {
mergeCoverageIntoGlobal(JSON.parse(coverage.worker));
}
coverage.workers?.map(c => mergeCoverageIntoGlobal(JSON.parse(c)));
await page.close({ runBeforeUnload: false });
}

View File

@ -1165,6 +1165,23 @@ const PDFViewerApplication = {
// Ignoring errors, to ensure that document closing won't break.
}
}
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("COVERAGE")) {
// Collect coverage data from the worker before the document is closed.
//
// Note that `PDFViewerApplication.open` may be invoked multiple times
// during an integration-test (see e.g. the "Merge PDF" tests).
const handler = this.pdfDocument?._transport?.messageHandler;
if (handler) {
try {
const workerCoverage = await handler.sendWithPromise(
"GetWorkerCoverage",
null
);
(window.__worker_coverage__ ??= []).push(workerCoverage);
} catch {}
}
}
const promises = [];
promises.push(this.pdfLoadingTask.destroy());