Skip to content

test(loader): Add tests for new loader integration handling #7790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/browser-integration-tests/fixtures/loader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window._testBaseTimestamp = performance.timeOrigin / 1000;

Sentry.onLoad(function () {
Sentry.init({
integrations: [
// Without this syntax, this will be re-written by the test framework
new window['Sentry'].BrowserTracing({
tracePropagationTargets: ['http://localhost:1234'],
}),
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';

sentryTest('should handle custom added BrowserTracing integration', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const req = waitForTransactionRequest(page);

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

const eventData = envelopeRequestParser(await req);
const timeOrigin = await page.evaluate<number>('window._testBaseTimestamp');

const { start_timestamp: startTimestamp } = eventData;

expect(startTimestamp).toBeCloseTo(timeOrigin, 1);

expect(eventData.contexts?.trace?.op).toBe('pageload');
expect(eventData.spans?.length).toBeGreaterThan(0);
expect(eventData.transaction_info?.source).toEqual('url');

const tracePropagationTargets = await page.evaluate(() => {
const browserTracing = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
return browserTracing.options.tracePropagationTargets;
});

expect(tracePropagationTargets).toEqual(['http://localhost:1234']);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

class CustomIntegration {
constructor() {
this.name = 'CustomIntegration';
}

setupOnce() {}
}

Sentry.onLoad(function () {
Sentry.init({
integrations: [new CustomIntegration()],
});

window.__sentryLoaded = true;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sentry.forceLoad();
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { shouldSkipTracingTest } from '../../../../utils/helpers';
import { shouldSkipReplayTest } from '../../../../utils/replayHelpers';

sentryTest('should handle custom added integrations & default integrations', async ({ getLocalTestUrl, page }) => {
const shouldHaveReplay = !shouldSkipReplayTest();
const shouldHaveBrowserTracing = !shouldSkipTracingTest();

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

await page.waitForFunction(() => {
return (window as any).__sentryLoaded;
});

const hasCustomIntegration = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
});

const hasReplay = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
});
const hasBrowserTracing = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
});

expect(hasCustomIntegration).toEqual(true);
expect(hasReplay).toEqual(shouldHaveReplay);
expect(hasBrowserTracing).toEqual(shouldHaveBrowserTracing);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

class CustomIntegration {
constructor() {
this.name = 'CustomIntegration';
}

setupOnce() {}
}

Sentry.onLoad(function () {
Sentry.init({
integrations: integrations => [new CustomIntegration()].concat(integrations),
});

window.__sentryLoaded = true;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sentry.forceLoad();
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';

sentryTest(
'should not add default integrations if integrations function is provided',
async ({ getLocalTestUrl, page }) => {
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

await page.waitForFunction(() => {
return (window as any).__sentryLoaded;
});

const hasCustomIntegration = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('CustomIntegration');
});

const hasReplay = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
});
const hasBrowserTracing = await page.evaluate(() => {
return !!(window as any).Sentry.getCurrentHub().getClient().getIntegrationById('BrowserTracing');
});

expect(hasCustomIntegration).toEqual(true);
expect(hasReplay).toEqual(false);
expect(hasBrowserTracing).toEqual(false);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.onLoad(function () {
Sentry.init({
integrations: [
// Without this syntax, this will be re-written by the test framework
new window['Sentry'].Replay({
useCompression: false,
}),
],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../../utils/replayHelpers';

sentryTest('should handle custom added Replay integration', async ({ getLocalTestUrl, page }) => {
if (shouldSkipReplayTest()) {
sentryTest.skip();
}

await page.route('https://dsn.ingest.sentry.io/**/*', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ id: 'test-id' }),
});
});

const req = waitForReplayRequest(page);

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

const eventData = getReplayEvent(await req);

expect(eventData).toBeDefined();
expect(eventData.segment_id).toBe(0);

const useCompression = await page.evaluate(() => {
const replay = (window as any).Sentry.getCurrentHub().getClient().getIntegrationById('Replay');
return replay._replay.getOptions().useCompression;
});

expect(useCompression).toEqual(false);
});
3 changes: 2 additions & 1 deletion packages/browser-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"test:loader:full": "PW_BUNDLE=loader_tracing_replay yarn test:loader",
"test:ci": "playwright test ./suites --browser='all' --reporter='line'",
"test:update-snapshots": "yarn test --update-snapshots --browser='all' && yarn test --update-snapshots",
"test:detect-flaky": "ts-node scripts/detectFlakyTests.ts"
"test:detect-flaky": "ts-node scripts/detectFlakyTests.ts",
"validate:es5": "es-check es5 'fixtures/loader.js'"
},
"dependencies": {
"@babel/preset-typescript": "^7.16.7",
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"lint": "run-s lint:prettier lint:eslint",
"lint:eslint": "eslint . --format stylish",
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
"validate:es5": "es-check es5 'build/bundles/*.es5*.js'",
"test": "jest",
"test:watch": "jest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts --bundles && yalc publish ./build/npm --push"
Expand Down