Skip to content

Commit cb8055f

Browse files
committed
Collect worker-side coverage for browser unit tests
1 parent da0b99c commit cb8055f

3 files changed

Lines changed: 128 additions & 35 deletions

File tree

test/coverage_utils.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* Copyright 2026 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// Istanbul coverage objects use s (statements), b (branches), and f (functions)
17+
// as shorthand keys for the hit-count maps.
18+
function mergeWorkerCoverageIntoWindow(coverage) {
19+
if (!coverage || Object.keys(coverage).length === 0) {
20+
return;
21+
}
22+
window.__coverage__ ??= {};
23+
for (const [key, fileCoverage] of Object.entries(coverage)) {
24+
const existing = window.__coverage__[key];
25+
if (!existing) {
26+
window.__coverage__[key] = fileCoverage;
27+
continue;
28+
}
29+
for (const id of Object.keys(fileCoverage.s)) {
30+
existing.s[id] = (existing.s[id] ?? 0) + fileCoverage.s[id];
31+
}
32+
for (const id of Object.keys(fileCoverage.b)) {
33+
existing.b[id] = fileCoverage.b[id].map(
34+
(c, i) => (existing.b[id]?.[i] ?? 0) + c
35+
);
36+
}
37+
for (const id of Object.keys(fileCoverage.f)) {
38+
existing.f[id] = (existing.f[id] ?? 0) + fileCoverage.f[id];
39+
}
40+
}
41+
}
42+
43+
async function fetchAndMergeWorkerCoverage(pdfWorker) {
44+
if (!pdfWorker) {
45+
return;
46+
}
47+
try {
48+
const coverage = await pdfWorker.messageHandler.sendWithPromise(
49+
"GetWorkerCoverage",
50+
null
51+
);
52+
mergeWorkerCoverageIntoWindow(coverage);
53+
} catch (e) {
54+
console.warn(`Failed to collect worker coverage: ${e}`);
55+
}
56+
}
57+
58+
export { fetchAndMergeWorkerCoverage, mergeWorkerCoverageIntoWindow };

test/driver.js

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
/* globals pdfjsLib, _pdfjsTestingUtils, pdfjsViewer */
1616

17+
import { fetchAndMergeWorkerCoverage } from "./coverage_utils.js";
18+
1719
const {
1820
AnnotationLayer,
1921
AnnotationMode,
@@ -1531,39 +1533,7 @@ class Driver {
15311533
}
15321534

15331535
async _collectWorkerCoverage() {
1534-
try {
1535-
const workerCoverage =
1536-
await this.#pdfWorker.messageHandler.sendWithPromise(
1537-
"GetWorkerCoverage",
1538-
null
1539-
);
1540-
if (workerCoverage && Object.keys(workerCoverage).length > 0) {
1541-
window.__coverage__ ??= {};
1542-
for (const [key, fileCoverage] of Object.entries(workerCoverage)) {
1543-
if (window.__coverage__[key]) {
1544-
// Istanbul coverage objects use s (statements), b (branches), and
1545-
// f (functions) as shorthand keys for the hit-count maps.
1546-
for (const id of Object.keys(fileCoverage.s)) {
1547-
window.__coverage__[key].s[id] =
1548-
(window.__coverage__[key].s[id] ?? 0) + fileCoverage.s[id];
1549-
}
1550-
for (const id of Object.keys(fileCoverage.b)) {
1551-
window.__coverage__[key].b[id] = fileCoverage.b[id].map(
1552-
(c, i) => (window.__coverage__[key].b[id]?.[i] ?? 0) + c
1553-
);
1554-
}
1555-
for (const id of Object.keys(fileCoverage.f)) {
1556-
window.__coverage__[key].f[id] =
1557-
(window.__coverage__[key].f[id] ?? 0) + fileCoverage.f[id];
1558-
}
1559-
} else {
1560-
window.__coverage__[key] = fileCoverage;
1561-
}
1562-
}
1563-
}
1564-
} catch (e) {
1565-
console.warn(`Failed to collect worker coverage: ${e}`);
1566-
}
1536+
await fetchAndMergeWorkerCoverage(this.#pdfWorker);
15671537
this.#pdfWorker.destroy();
15681538
this.#pdfWorker = null;
15691539
}

test/unit/jasmine-boot.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
import { GlobalWorkerOptions } from "pdfjs/display/worker_options.js";
4444
import { isNodeJS } from "../../src/shared/util.js";
45+
import { mergeWorkerCoverageIntoWindow } from "../coverage_utils.js";
46+
import { MessageHandler } from "pdfjs/shared/message_handler.js";
47+
import { PDFWorker } from "pdfjs/display/api.js";
4548
import { TestReporter } from "../reporter.js";
4649

4750
async function initializePDFJS(callback) {
@@ -114,12 +117,66 @@ async function initializePDFJS(callback) {
114117
"The `gulp unittest` command cannot be used in Node.js environments."
115118
);
116119
}
117-
// Configure the worker.
118-
GlobalWorkerOptions.workerSrc = "../../build/generic/build/pdf.worker.mjs";
120+
// Configure the worker. Point at the raw source so the webserver can
121+
// instrument it on request and the worker accumulates `__coverage__`.
122+
GlobalWorkerOptions.workerSrc = "../../src/pdf.worker.js";
119123

120124
callback();
121125
}
122126

127+
// Each unit-test typically spins up its own `PDFWorker`, which is destroyed
128+
// when the loading task is. Hook `destroy` so that we extract the worker-side
129+
// `__coverage__` before terminating, and merge it into the main thread's
130+
// `window.__coverage__`. Without this, anything tested through `getDocument`
131+
// → worker (most of `core/`) has its execution counts dropped on the floor.
132+
const pendingWorkerCoverage = new Set();
133+
134+
function installWorkerCoverageHook() {
135+
if (!window.__coverage__) {
136+
return;
137+
}
138+
const originalDestroy = PDFWorker.prototype.destroy;
139+
PDFWorker.prototype.destroy = function () {
140+
if (this.destroyed || !this._webWorker) {
141+
// Already torn down, or wrapping a foreign port — defer to the original
142+
// implementation, which leaves the underlying `Worker` alone.
143+
return originalDestroy.call(this);
144+
}
145+
// Capture the underlying Worker, then run the original destroy with
146+
// `terminate` neutralized so the public `destroyed`/`port` contract is
147+
// preserved synchronously while the Worker stays alive long enough to
148+
// hand back its `__coverage__`.
149+
const webWorker = this._webWorker;
150+
const realTerminate = webWorker.terminate.bind(webWorker);
151+
webWorker.terminate = () => {};
152+
try {
153+
originalDestroy.call(this);
154+
} finally {
155+
webWorker.terminate = realTerminate;
156+
}
157+
const handler = new MessageHandler("main", "worker", webWorker);
158+
const promise = handler
159+
.sendWithPromise("GetWorkerCoverage", null)
160+
.then(mergeWorkerCoverageIntoWindow)
161+
.catch(e => {
162+
console.warn(`Failed to collect worker coverage: ${e}`);
163+
})
164+
.finally(() => {
165+
handler.destroy();
166+
realTerminate();
167+
pendingWorkerCoverage.delete(promise);
168+
});
169+
pendingWorkerCoverage.add(promise);
170+
return undefined;
171+
};
172+
}
173+
174+
async function flushPendingWorkerCoverage() {
175+
while (pendingWorkerCoverage.size > 0) {
176+
await Promise.allSettled(pendingWorkerCoverage);
177+
}
178+
}
179+
123180
(function () {
124181
window.jasmine = jasmineRequire.core(jasmineRequire);
125182

@@ -140,6 +197,13 @@ async function initializePDFJS(callback) {
140197

141198
env.addReporter(htmlReporter);
142199

200+
if (window.__coverage__) {
201+
// Must run before `TestReporter`, whose `jasmineDone` triggers the
202+
// browser teardown; the worker-side counters need to be merged into
203+
// `window.__coverage__` before the page is closed.
204+
env.addReporter({ jasmineDone: flushPendingWorkerCoverage });
205+
}
206+
143207
if (urls.queryString.getParam("browser")) {
144208
const testReporter = new TestReporter(urls.queryString.getParam("browser"));
145209
env.addReporter(testReporter);
@@ -157,6 +221,7 @@ async function initializePDFJS(callback) {
157221

158222
function unitTestInit() {
159223
initializePDFJS(function () {
224+
installWorkerCoverageHook();
160225
env.execute();
161226
});
162227
}

0 commit comments

Comments
 (0)