Skip to content

Commit dec28cd

Browse files
committed
fixup! feat(cloudflare): Support basic WorkerEntrypoint
1 parent 1ef2947 commit dec28cd

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

packages/cloudflare/test/instrumentations/instrumentWorkerEntrypoint.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,25 @@ describe('instrumentWorkerEntrypoint', () => {
195195
});
196196

197197
it('flush performs after all waitUntil promises are finished', async () => {
198-
const flush = vi.spyOn(SentryCore.Client.prototype, 'flush').mockResolvedValue(true);
199-
vi.useFakeTimers();
200-
onTestFinished(() => {
201-
vi.useRealTimers();
198+
let testClientFlushCount = 0;
199+
let testClient: SentryCore.Client | undefined;
200+
201+
vi.spyOn(SentryCore.Client.prototype, 'flush').mockImplementation(function (this: SentryCore.Client) {
202+
if (this === testClient) {
203+
testClientFlushCount++;
204+
}
205+
return Promise.resolve(true);
202206
});
203207

204-
const before = flush.mock.calls.length;
208+
let resolveWaitUntil!: () => void;
209+
const deferred = new Promise<void>(res => {
210+
resolveWaitUntil = res;
211+
});
205212

206213
const waitUntil = vi.fn();
207214
const TestClass = vi.fn((context: ExecutionContext) => ({
208215
fetch: () => {
209-
context.waitUntil(new Promise(res => setTimeout(res)));
216+
context.waitUntil(deferred);
210217
return new Response('test');
211218
},
212219
}));
@@ -215,21 +222,17 @@ describe('instrumentWorkerEntrypoint', () => {
215222
const worker = Reflect.construct(instrumented, [context, {}]);
216223

217224
const responsePromise = worker.fetch(new Request('https://example.com'));
225+
testClient = SentryCore.getClient();
218226

219-
vi.advanceTimersByTime(30);
220227
const response = await responsePromise;
221-
222228
await response.text();
223229

224230
expect(waitUntil).toHaveBeenCalled();
225231

226-
vi.advanceTimersToNextTimer();
232+
resolveWaitUntil();
227233
await Promise.all(waitUntil.mock.calls.map(([p]) => p));
228234

229-
const after = flush.mock.calls.length;
230-
const delta = after - before;
231-
232-
expect(delta).toBe(1);
235+
expect(testClientFlushCount).toBe(1);
233236
});
234237

235238
describe('instrumentPrototypeMethods option', () => {

0 commit comments

Comments
 (0)