Skip to content

Commit 8bb6fa7

Browse files
committed
1 parent 5419551 commit 8bb6fa7

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎test/non-isolated-runner.resolve-mocks.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ describe("serializeMockerResolveMocks", () => {
9898
expect(FakeMocker.pendingIds).toEqual([]);
9999
});
100100

101+
it("drains ids queued during the final pass without requiring another caller", async () => {
102+
FakeMocker.pendingIds = ["mock-a"];
103+
const firstPassStarted = deferred();
104+
const releaseFirstPass = deferred();
105+
const mocker = new FakeMocker();
106+
mocker.beforeFirstPass = async () => {
107+
firstPassStarted.resolve();
108+
await releaseFirstPass.promise;
109+
};
110+
serializeMockerResolveMocks(mocker);
111+
112+
const first = mocker.resolveMocks();
113+
await firstPassStarted.promise;
114+
FakeMocker.pendingIds.push("mock-late");
115+
releaseFirstPass.resolve();
116+
await first;
117+
118+
expect(mocker.processed).toEqual(["mock-a", "mock-late"]);
119+
expect(mocker.passes).toBe(2);
120+
expect(FakeMocker.pendingIds).toEqual([]);
121+
});
122+
101123
it("does not double-wrap when installed repeatedly", async () => {
102124
FakeMocker.pendingIds = ["mock-a"];
103125
const mocker = new FakeMocker();

‎test/non-isolated-runner.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,20 @@ export function serializeMockerResolveMocks(mocker: SerializableMocker): void {
374374
const original = mocker.resolveMocks.bind(mocker);
375375
const statics = mocker.constructor as { pendingIds?: unknown[] };
376376
const runPass = async (): Promise<void> => {
377-
const queue = statics.pendingIds;
378-
const processedCount = queue?.length ?? 0;
379-
await original();
380-
// Upstream snapshots the queue contents at pass start and reassigns the
381-
// pendingIds static to [] at the end, so ids queued during the pass's RPC
382-
// window land in the abandoned array. Requeue them so the next chained
383-
// pass registers them instead of silently dropping the registration.
384-
if (queue && queue !== statics.pendingIds && queue.length > processedCount) {
385-
statics.pendingIds?.push(...queue.slice(processedCount));
377+
while (true) {
378+
const queue = statics.pendingIds;
379+
const processedCount = queue?.length ?? 0;
380+
await original();
381+
// Upstream snapshots the queue contents at pass start and reassigns the
382+
// pendingIds static to [] at the end, so ids queued during the pass's RPC
383+
// window land in the abandoned array. Requeue and drain them before this
384+
// caller proceeds so a later module fetch cannot invalidate mocks mid-import.
385+
if (queue && queue !== statics.pendingIds && queue.length > processedCount) {
386+
statics.pendingIds?.push(...queue.slice(processedCount));
387+
}
388+
if ((statics.pendingIds?.length ?? 0) === 0) {
389+
return;
390+
}
386391
}
387392
};
388393
mocker.resolveMocks = () => {

0 commit comments

Comments
 (0)