Skip to content
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

test(replay): Add test for fetch and XHR performance spans #7224

Merged
merged 4 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions packages/integration-tests/suites/replay/requests/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;
window.Replay = new Sentry.Replay({
flushMinDelay: 500,
flushMaxDelay: 500,
useCompression: true,
});

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,

integrations: [window.Replay],
});
16 changes: 16 additions & 0 deletions packages/integration-tests/suites/replay/requests/subject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
document.getElementById('go-background').addEventListener('click', () => {
Object.defineProperty(document, 'hidden', { value: true, writable: true });
const ev = document.createEvent('Event');
ev.initEvent('visibilitychange');
document.dispatchEvent(ev);
});

document.getElementById('fetch').addEventListener('click', () => {
fetch('https://example.com');
});

document.getElementById('xhr').addEventListener('click', () => {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com');
xhr.send();
});
11 changes: 11 additions & 0 deletions packages/integration-tests/suites/replay/requests/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<button id="go-background">Go to background</button>
<button id="fetch">New Fetch Request</button>
<button id="xhr">New Fetch Request</button>
</body>
</html>
74 changes: 74 additions & 0 deletions packages/integration-tests/suites/replay/requests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../utils/fixtures';
import { expectedFetchPerformanceSpan, expectedXHRPerformanceSpan } from '../../../utils/replayEventTemplates';
import { getReplayRecordingContent, shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers';

sentryTest('replay recording should contain fetch request span', async ({ getLocalTestPath, page }) => {
if (shouldSkipReplayTest()) {
sentryTest.skip();
}

// we're only interested in segment 1 which should contain the fetch span
const reqPromise1 = waitForReplayRequest(page, 1);

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

await page.route('https://example.com', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: 'hello world',
});
});

const url = await getLocalTestPath({ testDir: __dirname });

await page.goto(url);
await page.click('#fetch');
await page.click('#go-background');

const { performanceSpans } = getReplayRecordingContent(await reqPromise1);

expect(performanceSpans).toContainEqual(expectedFetchPerformanceSpan);
});

sentryTest('replay recording should contain XHR request span', async ({ getLocalTestPath, page }) => {
if (shouldSkipReplayTest()) {
sentryTest.skip();
}

// we're only interested in segment 1 which should contain the fetch span
const reqPromise1 = waitForReplayRequest(page, 1);

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

await page.route('https://example.com', route => {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: 'hello world',
});
});

const url = await getLocalTestPath({ testDir: __dirname });

await page.goto(url);
await page.click('#xhr');

const { performanceSpans } = getReplayRecordingContent(await reqPromise1);

expect(performanceSpans).toContainEqual(expectedXHRPerformanceSpan);
});
22 changes: 22 additions & 0 deletions packages/integration-tests/utils/replayEventTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ export const expectedFPPerformanceSpan = {
endTimestamp: expect.any(Number),
};

export const expectedFetchPerformanceSpan = {
op: 'resource.fetch',
description: expect.any(String),
startTimestamp: expect.any(Number),
endTimestamp: expect.any(Number),
data: {
method: expect.any(String),
statusCode: expect.any(Number),
},
};

export const expectedXHRPerformanceSpan = {
op: 'resource.xhr',
description: expect.any(String),
startTimestamp: expect.any(Number),
endTimestamp: expect.any(Number),
data: {
method: expect.any(String),
statusCode: expect.any(Number),
},
};

/* Breadcrumbs */

export const expectedClickBreadcrumb = {
Expand Down