Skip to content

Commit c586e03

Browse files
Expand waitForSettledDOM timeout and stall coverage
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent de3e77f commit c586e03

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

src/utils/waitForSettledDOM.test.ts

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ jest.mock("@/cdp", () => ({
77
}));
88

99
jest.mock("@/debug/options", () => ({
10-
getDebugOptions: jest.fn(() => ({
11-
enabled: true,
12-
traceWait: true,
13-
})),
10+
getDebugOptions: jest.fn(),
1411
}));
1512

1613
const { getCDPClient, getOrCreateFrameContextManager } = jest.requireMock(
@@ -19,6 +16,9 @@ const { getCDPClient, getOrCreateFrameContextManager } = jest.requireMock(
1916
getCDPClient: jest.Mock;
2017
getOrCreateFrameContextManager: jest.Mock;
2118
};
19+
const { getDebugOptions } = jest.requireMock("@/debug/options") as {
20+
getDebugOptions: jest.Mock;
21+
};
2222

2323
type EventHandler = (...args: unknown[]) => void;
2424

@@ -64,6 +64,10 @@ describe("waitForSettledDOM diagnostics", () => {
6464
beforeEach(() => {
6565
jest.useFakeTimers();
6666
jest.clearAllMocks();
67+
getDebugOptions.mockReturnValue({
68+
enabled: true,
69+
traceWait: true,
70+
});
6771
});
6872

6973
afterEach(() => {
@@ -103,16 +107,60 @@ describe("waitForSettledDOM diagnostics", () => {
103107
});
104108

105109
await jest.advanceTimersByTimeAsync(3_100);
106-
await waitPromise;
110+
const stats = await waitPromise;
107111

108112
const warning = String(warnSpy.mock.calls[0]?.[0] ?? "");
109113
expect(warning).toContain("[truncated");
110114
expect(warning).not.toContain("\u0000");
111115
expect(warning).not.toContain("\n");
112116
expect(warning.length).toBeLessThan(900);
117+
expect(stats.forcedDrops).toBe(1);
118+
expect(stats.requestsSeen).toBe(1);
119+
expect(stats.peakInflight).toBe(1);
120+
expect(stats.resolvedByTimeout).toBe(false);
113121
} finally {
114122
warnSpy.mockRestore();
115123
logSpy.mockRestore();
116124
}
117125
});
126+
127+
it("reports timeout-driven completion when requests remain inflight", async () => {
128+
const { session, emit } = createSessionWithEvents();
129+
const cdpClient: CDPClient = {
130+
rootSession: session,
131+
createSession: async () => session,
132+
acquireSession: async () => session,
133+
dispose: async () => undefined,
134+
};
135+
getCDPClient.mockResolvedValue(cdpClient);
136+
getOrCreateFrameContextManager.mockReturnValue({
137+
setDebug: jest.fn(),
138+
});
139+
getDebugOptions.mockReturnValue({
140+
enabled: false,
141+
traceWait: false,
142+
});
143+
144+
const page = {
145+
context: () => ({}),
146+
} as never;
147+
148+
const waitPromise = waitForSettledDOM(page, 600);
149+
await Promise.resolve();
150+
await Promise.resolve();
151+
152+
emit("Network.requestWillBeSent", {
153+
requestId: "req-1",
154+
type: "Document",
155+
request: { url: "https://example.com/slow" },
156+
});
157+
158+
await jest.advanceTimersByTimeAsync(700);
159+
const stats = await waitPromise;
160+
161+
expect(stats.resolvedByTimeout).toBe(true);
162+
expect(stats.forcedDrops).toBe(0);
163+
expect(stats.requestsSeen).toBe(1);
164+
expect(stats.peakInflight).toBe(1);
165+
});
118166
});

0 commit comments

Comments
 (0)