|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +import { sentryTest, TEST_HOST } from '../../../../utils/fixtures'; |
| 6 | +import { LOADER_CONFIGS } from '../../../../utils/generatePage'; |
| 7 | +import { envelopeRequestParser, waitForErrorRequest } from '../../../../utils/helpers'; |
| 8 | + |
| 9 | +const bundle = process.env.PW_BUNDLE || ''; |
| 10 | +const isLazy = LOADER_CONFIGS[bundle]?.lazy; |
| 11 | + |
| 12 | +sentryTest('it does not download the SDK if the SDK was loaded in the meanwhile', async ({ getLocalTestUrl, page }) => { |
| 13 | + // When the loader is eager, this does not work and makes no sense |
| 14 | + if (isLazy !== true) { |
| 15 | + sentryTest.skip(); |
| 16 | + } |
| 17 | + |
| 18 | + let cdnLoadedCount = 0; |
| 19 | + let sentryEventCount = 0; |
| 20 | + |
| 21 | + await page.route('https://dsn.ingest.sentry.io/**/*', route => { |
| 22 | + sentryEventCount++; |
| 23 | + |
| 24 | + return route.fulfill({ |
| 25 | + status: 200, |
| 26 | + contentType: 'application/json', |
| 27 | + body: JSON.stringify({ id: 'test-id' }), |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + await page.route(`${TEST_HOST}/*.*`, route => { |
| 32 | + const file = route.request().url().split('/').pop(); |
| 33 | + |
| 34 | + if (file === 'cdn.bundle.js') { |
| 35 | + cdnLoadedCount++; |
| 36 | + } |
| 37 | + |
| 38 | + const filePath = path.resolve(__dirname, `./dist/${file}`); |
| 39 | + |
| 40 | + return fs.existsSync(filePath) ? route.fulfill({ path: filePath }) : route.continue(); |
| 41 | + }); |
| 42 | + |
| 43 | + const req = waitForErrorRequest(page); |
| 44 | + |
| 45 | + const url = await getLocalTestUrl({ testDir: __dirname, skipRouteHandler: true }); |
| 46 | + |
| 47 | + await page.goto(url); |
| 48 | + |
| 49 | + const eventData = envelopeRequestParser(await req); |
| 50 | + |
| 51 | + await waitForFunction(() => cdnLoadedCount === 2); |
| 52 | + |
| 53 | + // Still loaded the CDN bundle twice |
| 54 | + expect(cdnLoadedCount).toBe(2); |
| 55 | + |
| 56 | + // But only sent to Sentry once |
| 57 | + expect(sentryEventCount).toBe(1); |
| 58 | + |
| 59 | + // Ensure loader does not overwrite init/config |
| 60 | + const options = await page.evaluate(() => (window as any).Sentry.getCurrentHub().getClient()?.getOptions()); |
| 61 | + expect(options?.replaysSessionSampleRate).toBe(0.42); |
| 62 | + |
| 63 | + expect(eventData.exception?.values?.length).toBe(1); |
| 64 | + expect(eventData.exception?.values?.[0]?.value).toBe('window.doSomethingWrong is not a function'); |
| 65 | +}); |
| 66 | + |
| 67 | +async function waitForFunction(cb: () => boolean, timeout = 2000, increment = 100) { |
| 68 | + while (timeout > 0 && !cb()) { |
| 69 | + await new Promise(resolve => setTimeout(resolve, increment)); |
| 70 | + await waitForFunction(cb, timeout - increment, increment); |
| 71 | + } |
| 72 | +} |
0 commit comments