Skip to content

Commit df48298

Browse files
committed
debug
1 parent 1fd6927 commit df48298

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

packages/cloudflare/test/request.test.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,80 @@ describe('withSentry', () => {
9191
});
9292

9393
test('flush must be called when all waitUntil are done', async () => {
94-
// Reset all mocks to ensure clean state
95-
vi.clearAllMocks();
96-
vi.restoreAllMocks();
94+
const flushSpy = vi.spyOn(SentryCore.Client.prototype, 'flush').mockImplementation(async function (this: any) {
95+
const callNum = flushSpy.mock.calls.length;
96+
console.log(`[FLUSH #${callNum}] Client: ${this?.constructor?.name || 'unknown'}`);
97+
const stack = new Error().stack?.split('\n').slice(2, 7).join('\n ');
98+
console.log(` Stack:\n ${stack}`);
99+
return true;
100+
});
101+
flushSpy.mockClear(); // Explicitly clear before test
97102

98-
const flushSpy = vi.spyOn(SentryCore.Client.prototype, 'flush').mockResolvedValue(true);
99103
vi.useFakeTimers();
100104
onTestFinished(() => {
101105
vi.useRealTimers();
102106
flushSpy.mockRestore();
103107
});
104108
const waits: Promise<unknown>[] = [];
105-
const waitUntil = vi.fn(promise => waits.push(promise));
109+
const waitUntil = vi.fn(promise => {
110+
const callNum = waitUntil.mock.calls.length;
111+
console.log(`[WAITUNTIL #${callNum}]`);
112+
const stack = new Error().stack?.split('\n').slice(2, 5).join('\n ');
113+
console.log(` Stack:\n ${stack}`);
114+
waits.push(promise);
115+
});
106116

107117
const context = {
108118
waitUntil,
109119
} as unknown as ExecutionContext;
110120

121+
console.log('\n=== TEST START ===');
111122
await wrapRequestHandler({ options: MOCK_OPTIONS, request: new Request('https://example.com'), context }, () => {
112123
addDelayedWaitUntil(context);
113124
const response = new Response('test');
114125
// Add Content-Length to skip probing
115126
response.headers.set('content-length', '4');
116127
return response;
117128
});
118-
expect(flushSpy).not.toBeCalled();
129+
130+
console.log('\n=== AFTER HANDLER ===');
131+
console.log(`waitUntil calls: ${waitUntil.mock.calls.length}`);
132+
console.log(`flush calls: ${flushSpy.mock.calls.length}`);
133+
134+
const callsBeforeResolve = flushSpy.mock.calls.length;
135+
expect(callsBeforeResolve).toBe(0); // flush not called during handler
119136
expect(waitUntil).toBeCalled();
137+
138+
console.log('\n=== ADVANCING TIMERS ===');
120139
await vi.advanceTimersToNextTimerAsync();
121140
vi.runAllTimers();
141+
142+
console.log('\n=== WAITING FOR PROMISES ===');
122143
await Promise.all(waits);
123-
expect(flushSpy).toHaveBeenCalledOnce();
144+
145+
console.log('\n=== TEST END ===');
146+
console.log(`Total waitUntil calls: ${waitUntil.mock.calls.length}`);
147+
console.log(`Total flush calls: ${flushSpy.mock.calls.length}`);
148+
console.log(`Flush calls before resolve: ${callsBeforeResolve}`);
149+
150+
// Check that flush was called at least once after waitUntils resolved
151+
const callsAfterResolve = flushSpy.mock.calls.length;
152+
expect(callsAfterResolve).toBeGreaterThanOrEqual(1);
153+
154+
// Log detailed failure info if the delta assertion will fail
155+
const delta = callsAfterResolve - callsBeforeResolve;
156+
if (delta !== 1) {
157+
console.error('\n=== ASSERTION FAILURE ===');
158+
console.error(`Expected exactly 1 NEW flush call, but got ${delta}`);
159+
console.error(`Calls before: ${callsBeforeResolve}, Calls after: ${callsAfterResolve}`);
160+
console.error('\nAll flush call arguments:');
161+
flushSpy.mock.calls.forEach((call, i) => {
162+
console.error(` Call ${i + 1}:`, call);
163+
});
164+
}
165+
166+
// Ideally it should be exactly 1, but verify the delta to avoid CI flakiness
167+
expect(callsAfterResolve - callsBeforeResolve).toBe(1);
124168
});
125169

126170
describe('scope instrumentation', () => {

0 commit comments

Comments
 (0)