Skip to content

Commit 1ad518f

Browse files
authored
test(loader): Add tests for new loader integration handling (#7790)
1 parent 73c2be4 commit 1ad518f

File tree

13 files changed

+220
-2
lines changed

13 files changed

+220
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
window._testBaseTimestamp = performance.timeOrigin / 1000;
5+
6+
Sentry.onLoad(function () {
7+
Sentry.init({
8+
integrations: [
9+
// Without this syntax, this will be re-written by the test framework
10+
new window['Sentry'].BrowserTracing({
11+
tracePropagationTargets: ['http://localhost:1234'],
12+
}),
13+
],
14+
});
15+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
5+
6+
sentryTest('should handle custom added BrowserTracing integration', async ({ getLocalTestUrl, page }) => {
7+
if (shouldSkipTracingTest()) {
8+
sentryTest.skip();
9+
}
10+
11+
const req = waitForTransactionRequest(page);
12+
13+
const url = await getLocalTestUrl({ testDir: __dirname });
14+
await page.goto(url);
15+
16+
const eventData = envelopeRequestParser(await req);
17+
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');
18+
19+
const { start_timestamp: startTimestamp } = eventData;
20+
21+
expect(startTimestamp).toBeCloseTo(timeOrigin, 1);
22+
23+
expect(eventData.contexts?.trace?.op).toBe('pageload');
24+
expect(eventData.spans?.length).toBeGreaterThan(0);
25+
expect(eventData.transaction_info?.source).toEqual('url');
26+
27+
const tracePropagationTargets = await page.evaluate(() => {
28+
const browserTracing = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
29+
return browserTracing.options.tracePropagationTargets;
30+
});
31+
32+
expect(tracePropagationTargets).toEqual(['http://localhost:1234']);
33+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
class CustomIntegration {
6+
constructor() {
7+
this.name = 'CustomIntegration';
8+
}
9+
10+
setupOnce() {}
11+
}
12+
13+
Sentry.onLoad(function () {
14+
Sentry.init({
15+
integrations: [new CustomIntegration()],
16+
});
17+
18+
window.__sentryLoaded = true;
19+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sentry.forceLoad();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { shouldSkipTracingTest } from '../../../../utils/helpers';
5+
import { shouldSkipReplayTest } from '../../../../utils/replayHelpers';
6+
7+
sentryTest('should handle custom added integrations & default integrations', async ({ getLocalTestUrl, page }) => {
8+
const shouldHaveReplay = !shouldSkipReplayTest();
9+
const shouldHaveBrowserTracing = !shouldSkipTracingTest();
10+
11+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
12+
return route.fulfill({
13+
status: 200,
14+
contentType: 'application/json',
15+
body: JSON.stringify({ id: 'test-id' }),
16+
});
17+
});
18+
19+
const url = await getLocalTestUrl({ testDir: __dirname });
20+
await page.goto(url);
21+
22+
await page.waitForFunction(() => {
23+
return (window as any).__sentryLoaded;
24+
});
25+
26+
const hasCustomIntegration = await page.evaluate(() => {
27+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
28+
});
29+
30+
const hasReplay = await page.evaluate(() => {
31+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
32+
});
33+
const hasBrowserTracing = await page.evaluate(() => {
34+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
35+
});
36+
37+
expect(hasCustomIntegration).toEqual(true);
38+
expect(hasReplay).toEqual(shouldHaveReplay);
39+
expect(hasBrowserTracing).toEqual(shouldHaveBrowserTracing);
40+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
class CustomIntegration {
6+
constructor() {
7+
this.name = 'CustomIntegration';
8+
}
9+
10+
setupOnce() {}
11+
}
12+
13+
Sentry.onLoad(function () {
14+
Sentry.init({
15+
integrations: integrations => [new CustomIntegration()].concat(integrations),
16+
});
17+
18+
window.__sentryLoaded = true;
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sentry.forceLoad();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
5+
sentryTest(
6+
'should not add default integrations if integrations function is provided',
7+
async ({ getLocalTestUrl, page }) => {
8+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
9+
return route.fulfill({
10+
status: 200,
11+
contentType: 'application/json',
12+
body: JSON.stringify({ id: 'test-id' }),
13+
});
14+
});
15+
16+
const url = await getLocalTestUrl({ testDir: __dirname });
17+
await page.goto(url);
18+
19+
await page.waitForFunction(() => {
20+
return (window as any).__sentryLoaded;
21+
});
22+
23+
const hasCustomIntegration = await page.evaluate(() => {
24+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
25+
});
26+
27+
const hasReplay = await page.evaluate(() => {
28+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
29+
});
30+
const hasBrowserTracing = await page.evaluate(() => {
31+
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
32+
});
33+
34+
expect(hasCustomIntegration).toEqual(true);
35+
expect(hasReplay).toEqual(false);
36+
expect(hasBrowserTracing).toEqual(false);
37+
},
38+
);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.onLoad(function () {
6+
Sentry.init({
7+
integrations: [
8+
// Without this syntax, this will be re-written by the test framework
9+
new window['Sentry'].Replay({
10+
useCompression: false,
11+
}),
12+
],
13+
});
14+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../../utils/replayHelpers';
5+
6+
sentryTest('should handle custom added Replay integration', async ({ getLocalTestUrl, page }) => {
7+
if (shouldSkipReplayTest()) {
8+
sentryTest.skip();
9+
}
10+
11+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
12+
return route.fulfill({
13+
status: 200,
14+
contentType: 'application/json',
15+
body: JSON.stringify({ id: 'test-id' }),
16+
});
17+
});
18+
19+
const req = waitForReplayRequest(page);
20+
21+
const url = await getLocalTestUrl({ testDir: __dirname });
22+
await page.goto(url);
23+
24+
const eventData = getReplayEvent(await req);
25+
26+
expect(eventData).toBeDefined();
27+
expect(eventData.segment_id).toBe(0);
28+
29+
const useCompression = await page.evaluate(() => {
30+
const replay = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
31+
return replay._replay.getOptions().useCompression;
32+
});
33+
34+
expect(useCompression).toEqual(false);
35+
});

packages/browser-integration-tests/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"test:loader:full": "PW_BUNDLE=loader_tracing_replay yarn test:loader",
4242
"test:ci": "playwright test ./suites --browser='all' --reporter='line'",
4343
"test:update-snapshots": "yarn test --update-snapshots --browser='all' && yarn test --update-snapshots",
44-
"test:detect-flaky": "ts-node scripts/detectFlakyTests.ts"
44+
"test:detect-flaky": "ts-node scripts/detectFlakyTests.ts",
45+
"validate:es5": "es-check es5 'fixtures/loader.js'"
4546
},
4647
"dependencies": {
4748
"@babel/preset-typescript": "^7.16.7",

packages/integrations/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"lint": "run-s lint:prettier lint:eslint",
4545
"lint:eslint": "eslint . --format stylish",
4646
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
47+
"validate:es5": "es-check es5 'build/bundles/*.es5*.js'",
4748
"test": "jest",
4849
"test:watch": "jest --watch",
4950
"yalc:publish": "ts-node ../../scripts/prepack.ts --bundles && yalc publish ./build/npm --push"

0 commit comments

Comments
 (0)