Skip to content

Commit 35c3473

Browse files
mydeabillyvg
authored andcommitted
test(loader): Ensure loader handles SDK being loaded in the meanwhile (#7970)
1 parent 1e80026 commit 35c3473

File tree

4 files changed

+99
-8
lines changed

4 files changed

+99
-8
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
replaysSessionSampleRate: 0.42,
9+
});
10+
11+
setTimeout(() => {
12+
window.doSomethingWrong();
13+
}, 500);
14+
});
15+
16+
document.head.appendChild(cdnScript);
17+
}, 100);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

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)