diff --git a/dev-packages/browser-integration-tests/suites/transport/offline/flush/subject.js b/dev-packages/browser-integration-tests/suites/transport/offline/flush/subject.js new file mode 100644 index 000000000000..27ce1c0c0b52 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/transport/offline/flush/subject.js @@ -0,0 +1,7 @@ +setTimeout(() => { + Sentry.captureMessage(`foo ${Math.random()}`); +}, 500); + +setTimeout(() => { + Sentry.flush(); +}, 2000); diff --git a/dev-packages/browser-integration-tests/suites/transport/offline/flush/test.ts b/dev-packages/browser-integration-tests/suites/transport/offline/flush/test.ts new file mode 100644 index 000000000000..b7feab8a6448 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/transport/offline/flush/test.ts @@ -0,0 +1,47 @@ +import { expect } from '@playwright/test'; +import type { Event } from '@sentry/core'; + +import { sentryTest } from '../../../../utils/fixtures'; +import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers'; + +function delay(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +sentryTest('should flush event', async ({ getLocalTestUrl, page }) => { + // makeBrowserOfflineTransport is not included in any CDN bundles + if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle')) { + sentryTest.skip(); + } + + const url = await getLocalTestUrl({ testDir: __dirname }); + + // This would be the obvious way to test offline support but it doesn't appear to work! + // await context.setOffline(true); + + let abortedCount = 0; + + // Abort all envelope requests so the event gets queued + await page.route(/ingest\.sentry\.io/, route => { + abortedCount += 1; + return route.abort(); + }); + await page.goto(url); + await delay(1_000); + await page.unroute(/ingest\.sentry\.io/); + + expect(abortedCount).toBe(1); + + // The previous event should now be queued + + // It should get flushed after a few seconds + const eventData = await getMultipleSentryEnvelopeRequests(page, 2, { timeout: 4_000 }); + + // Filter out any client reports + const events = eventData.filter(e => !('discarded_events' in e)) as Event[]; + + expect(events).toHaveLength(1); + + // The next two events will be message events starting with 'foo' + expect(events[0].message?.startsWith('foo')); +});