Skip to content

Commit 1ecaa20

Browse files
committed
test(loader): Ensure we only fetch CDN bundle once
1 parent f08811e commit 1ecaa20

File tree

5 files changed

+70
-9
lines changed

5 files changed

+70
-9
lines changed

packages/browser-integration-tests/fixtures/loader.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
setTimeout(() => {
2+
const cdnScript = document.createElement('script');
3+
cdnScript.src = '/cdn.bundle.js';
4+
5+
cdnScript.addEventListener('load', () => {
6+
window.Sentry.init({
7+
dsn: 'https://[email protected]/1337',
8+
});
9+
10+
setTimeout(() => {
11+
window.doSomethingWrong();
12+
}, 500);
13+
});
14+
15+
document.head.appendChild(cdnScript);
16+
}, 100);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
20+
await page.route(`${TEST_HOST}/*.*`, route => {
21+
const file = route.request().url().split('/').pop();
22+
23+
if (file === 'cdn.bundle.js') {
24+
cdnLoadedCount++;
25+
}
26+
27+
const filePath = path.resolve(__dirname, `./dist/${file}`);
28+
29+
return fs.existsSync(filePath) ? route.fulfill({ path: filePath }) : route.continue();
30+
});
31+
32+
const req = waitForErrorRequest(page);
33+
34+
const url = await getLocalTestUrl({ testDir: __dirname, skipRouteHandler: true });
35+
36+
await page.goto(url);
37+
38+
const eventData = envelopeRequestParser(await req);
39+
40+
expect(eventData.exception?.values?.length).toBe(1);
41+
expect(eventData.exception?.values?.[0]?.value).toBe('window.doSomethingWrong is not a function');
42+
expect(cdnLoadedCount).toBe(1);
43+
});

packages/browser-integration-tests/utils/fixtures.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type TestFixtures = {
2727
_autoSnapshotSuffix: void;
2828
testDir: string;
2929
getLocalTestPath: (options: { testDir: string }) => Promise<string>;
30-
getLocalTestUrl: (options: { testDir: string }) => Promise<string>;
30+
getLocalTestUrl: (options: { testDir: string; skipRouteHandler?: boolean }) => Promise<string>;
3131
forceFlushReplay: () => Promise<string>;
3232
runInChromium: (fn: (...args: unknown[]) => unknown, args?: unknown[]) => unknown;
3333
runInFirefox: (fn: (...args: unknown[]) => unknown, args?: unknown[]) => unknown;
@@ -49,19 +49,21 @@ const sentryTest = base.extend<TestFixtures>({
4949
],
5050

5151
getLocalTestUrl: ({ page }, use) => {
52-
return use(async ({ testDir }) => {
52+
return use(async ({ testDir, skipRouteHandler = false }) => {
5353
const pagePath = `${TEST_HOST}/index.html`;
5454

5555
await build(testDir);
5656
generateLoader(testDir);
5757

5858
// Serve all assets under
59-
await page.route(`${TEST_HOST}/*.*`, route => {
60-
const file = route.request().url().split('/').pop();
61-
const filePath = path.resolve(testDir, `./dist/${file}`);
59+
if (!skipRouteHandler) {
60+
await page.route(`${TEST_HOST}/*.*`, route => {
61+
const file = route.request().url().split('/').pop();
62+
const filePath = path.resolve(testDir, `./dist/${file}`);
6263

63-
return fs.existsSync(filePath) ? route.fulfill({ path: filePath }) : route.continue();
64-
});
64+
return fs.existsSync(filePath) ? route.fulfill({ path: filePath }) : route.continue();
65+
});
66+
}
6567

6668
return pagePath;
6769
});

packages/browser-integration-tests/utils/generatePage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SentryScenarioGenerationPlugin from './generatePlugin';
99

1010
const LOADER_TEMPLATE = readFileSync(path.join(__dirname, '../fixtures/loader.js'), 'utf-8');
1111

12-
const LOADER_CONFIGS: Record<string, { bundle: string; options: Record<string, unknown>; lazy: boolean }> = {
12+
export const LOADER_CONFIGS: Record<string, { bundle: string; options: Record<string, unknown>; lazy: boolean }> = {
1313
loader_base: {
1414
bundle: 'browser/build/bundles/bundle.es5.min.js',
1515
options: {},

0 commit comments

Comments
 (0)