|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import type { Event } from '@sentry/core'; |
| 3 | + |
| 4 | +import { sentryTest } from '../../../../utils/fixtures'; |
| 5 | +import { getMultipleSentryEnvelopeRequests } from '../../../../utils/helpers'; |
| 6 | + |
| 7 | +function delay(ms: number) { |
| 8 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 9 | +} |
| 10 | + |
| 11 | +sentryTest('should flush event', async ({ getLocalTestUrl, page }) => { |
| 12 | + // makeBrowserOfflineTransport is not included in any CDN bundles |
| 13 | + if (process.env.PW_BUNDLE && process.env.PW_BUNDLE.startsWith('bundle')) { |
| 14 | + sentryTest.skip(); |
| 15 | + } |
| 16 | + |
| 17 | + const url = await getLocalTestUrl({ testDir: __dirname }); |
| 18 | + |
| 19 | + // This would be the obvious way to test offline support but it doesn't appear to work! |
| 20 | + // await context.setOffline(true); |
| 21 | + |
| 22 | + let abortedCount = 0; |
| 23 | + |
| 24 | + // Abort all envelope requests so the event gets queued |
| 25 | + await page.route(/ingest\.sentry\.io/, route => { |
| 26 | + abortedCount += 1; |
| 27 | + return route.abort(); |
| 28 | + }); |
| 29 | + await page.goto(url); |
| 30 | + await delay(1_000); |
| 31 | + await page.unroute(/ingest\.sentry\.io/); |
| 32 | + |
| 33 | + expect(abortedCount).toBe(1); |
| 34 | + |
| 35 | + // The previous event should now be queued |
| 36 | + |
| 37 | + // It should get flushed after a few seconds |
| 38 | + const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { timeout: 4_000 }); |
| 39 | + |
| 40 | + // Filter out any client reports |
| 41 | + const events = eventData.filter(e => !('discarded_events' in e)) as Event[]; |
| 42 | + |
| 43 | + expect(events).toHaveLength(1); |
| 44 | + |
| 45 | + // The next two events will be message events starting with 'foo' |
| 46 | + expect(events[0].message?.startsWith('foo')); |
| 47 | +}); |
0 commit comments