Skip to content

Commit

Permalink
test(replay): Add test for fetch and XHR performance spans (#7224)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Feb 17, 2023
1 parent 00d2360 commit 7f4c4ec
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
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>
79 changes: 79 additions & 0 deletions packages/integration-tests/suites/replay/requests/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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();
}

const reqPromise0 = waitForReplayRequest(page, 0);
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: spans0 } = getReplayRecordingContent(await reqPromise0);
const { performanceSpans: spans1 } = getReplayRecordingContent(await reqPromise1);
const performanceSpans = [...spans0, ...spans1];

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

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

const reqPromise0 = waitForReplayRequest(page, 0);
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');
await page.click('#go-background');

const { performanceSpans: spans0 } = getReplayRecordingContent(await reqPromise0);
const { performanceSpans: spans1 } = getReplayRecordingContent(await reqPromise1);
const performanceSpans = [...spans0, ...spans1];

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

0 comments on commit 7f4c4ec

Please sign in to comment.