Skip to content

Commit 8a5e0c3

Browse files
authored
test: Fix flaky errorsInSession test (#7309)
1 parent ca1352d commit 8a5e0c3

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

packages/integration-tests/suites/replay/captureReplay/test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { expect } from '@playwright/test';
22
import { SDK_VERSION } from '@sentry/browser';
3-
import type { ReplayEvent } from '@sentry/types';
43

54
import { sentryTest } from '../../../utils/fixtures';
6-
import { envelopeRequestParser } from '../../../utils/helpers';
7-
import { shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers';
5+
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers';
86

97
sentryTest('should capture replays', async ({ getLocalTestPath, page }) => {
108
if (shouldSkipReplayTest()) {
@@ -25,10 +23,10 @@ sentryTest('should capture replays', async ({ getLocalTestPath, page }) => {
2523
const url = await getLocalTestPath({ testDir: __dirname });
2624

2725
await page.goto(url);
28-
const replayEvent0 = envelopeRequestParser(await reqPromise0) as ReplayEvent;
26+
const replayEvent0 = getReplayEvent(await reqPromise0);
2927

3028
await page.click('button');
31-
const replayEvent1 = envelopeRequestParser(await reqPromise1) as ReplayEvent;
29+
const replayEvent1 = getReplayEvent(await reqPromise1);
3230

3331
expect(replayEvent0).toBeDefined();
3432
expect(replayEvent0).toEqual({

packages/integration-tests/suites/replay/captureReplayViaBrowser/test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { expect } from '@playwright/test';
22
import { SDK_VERSION } from '@sentry/browser';
3-
import type { ReplayEvent } from '@sentry/types';
43

54
import { sentryTest } from '../../../utils/fixtures';
6-
import { envelopeRequestParser } from '../../../utils/helpers';
7-
import { waitForReplayRequest } from '../../../utils/replayHelpers';
5+
import { getReplayEvent, waitForReplayRequest } from '../../../utils/replayHelpers';
86

97
sentryTest('should capture replays (@sentry/browser export)', async ({ getLocalTestPath, page }) => {
108
// For this test, we skip all bundle tests, as we're only interested in Replay being correctly
@@ -27,10 +25,10 @@ sentryTest('should capture replays (@sentry/browser export)', async ({ getLocalT
2725
const url = await getLocalTestPath({ testDir: __dirname });
2826

2927
await page.goto(url);
30-
const replayEvent0 = envelopeRequestParser(await reqPromise0) as ReplayEvent;
28+
const replayEvent0 = getReplayEvent(await reqPromise0);
3129

3230
await page.click('button');
33-
const replayEvent1 = envelopeRequestParser(await reqPromise1) as ReplayEvent;
31+
const replayEvent1 = getReplayEvent(await reqPromise1);
3432

3533
expect(replayEvent0).toBeDefined();
3634
expect(replayEvent0).toEqual({

packages/integration-tests/suites/replay/errors/errorsInSession/test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ sentryTest(
7676
sentryTest.skip();
7777
}
7878

79+
const reqPromise0 = waitForReplayRequest(page, 0);
7980
const reqPromise1 = waitForReplayRequest(page, 1);
8081

8182
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
@@ -89,7 +90,7 @@ sentryTest(
8990
const url = await getLocalTestPath({ testDir: __dirname });
9091

9192
await page.goto(url);
92-
await page.click('#go-background');
93+
await reqPromise0;
9394

9495
await page.click('#drop');
9596
await page.click('#go-background');

packages/integration-tests/utils/replayHelpers.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RecordingEvent, ReplayContainer } from '@sentry/replay/build/npm/types/types';
22
import type { Breadcrumb, Event, ReplayEvent } from '@sentry/types';
33
import pako from 'pako';
4-
import type { Page, Request } from 'playwright';
4+
import type { Page, Request, Response } from 'playwright';
55

66
import { envelopeRequestParser } from './helpers';
77

@@ -37,8 +37,10 @@ type SnapshotNode = {
3737
* @param segmentId the segment_id of the replay event
3838
* @returns
3939
*/
40-
export function waitForReplayRequest(page: Page, segmentId?: number): Promise<Request> {
41-
return page.waitForRequest(req => {
40+
export function waitForReplayRequest(page: Page, segmentId?: number): Promise<Response> {
41+
return page.waitForResponse(res => {
42+
const req = res.request();
43+
4244
const postData = req.postData();
4345
if (!postData) {
4446
return false;
@@ -78,7 +80,8 @@ export async function getReplaySnapshot(page: Page): Promise<ReplayContainer> {
7880

7981
export const REPLAY_DEFAULT_FLUSH_MAX_DELAY = 5_000;
8082

81-
export function getReplayEvent(replayRequest: Request): ReplayEvent {
83+
export function getReplayEvent(resOrReq: Request | Response): ReplayEvent {
84+
const replayRequest = getRequest(resOrReq);
8285
const event = envelopeRequestParser(replayRequest);
8386
if (!isReplayEvent(event)) {
8487
throw new Error('Request is not a replay event');
@@ -103,7 +106,8 @@ type RecordingContent = {
103106
* @param replayRequest
104107
* @returns an object containing the replay breadcrumbs and performance spans
105108
*/
106-
export function getCustomRecordingEvents(replayRequest: Request): CustomRecordingContent {
109+
export function getCustomRecordingEvents(resOrReq: Request | Response): CustomRecordingContent {
110+
const replayRequest = getRequest(resOrReq);
107111
const recordingEvents = getDecompressedRecordingEvents(replayRequest);
108112

109113
const breadcrumbs = getReplayBreadcrumbs(recordingEvents);
@@ -128,21 +132,25 @@ function getReplayPerformanceSpans(recordingEvents: RecordingEvent[]): Performan
128132
.map(data => data.payload) as PerformanceSpan[];
129133
}
130134

131-
export function getFullRecordingSnapshots(replayRequest: Request): RecordingSnapshot[] {
135+
export function getFullRecordingSnapshots(resOrReq: Request | Response): RecordingSnapshot[] {
136+
const replayRequest = getRequest(resOrReq);
132137
const events = getDecompressedRecordingEvents(replayRequest) as RecordingEvent[];
133138
return events.filter(event => event.type === 2).map(event => event.data as RecordingSnapshot);
134139
}
135140

136-
function getIncrementalRecordingSnapshots(replayRequest: Request): RecordingSnapshot[] {
141+
function getIncrementalRecordingSnapshots(resOrReq: Request | Response): RecordingSnapshot[] {
142+
const replayRequest = getRequest(resOrReq);
137143
const events = getDecompressedRecordingEvents(replayRequest) as RecordingEvent[];
138144
return events.filter(event => event.type === 3).map(event => event.data as RecordingSnapshot);
139145
}
140146

141-
function getDecompressedRecordingEvents(replayRequest: Request): RecordingEvent[] {
147+
function getDecompressedRecordingEvents(resOrReq: Request | Response): RecordingEvent[] {
148+
const replayRequest = getRequest(resOrReq);
142149
return replayEnvelopeRequestParser(replayRequest, 5) as RecordingEvent[];
143150
}
144151

145-
export function getReplayRecordingContent(replayRequest: Request): RecordingContent {
152+
export function getReplayRecordingContent(resOrReq: Request | Response): RecordingContent {
153+
const replayRequest = getRequest(resOrReq);
146154
const fullSnapshots = getFullRecordingSnapshots(replayRequest);
147155
const incrementalSnapshots = getIncrementalRecordingSnapshots(replayRequest);
148156
const customEvents = getCustomRecordingEvents(replayRequest);
@@ -255,3 +263,9 @@ function normalizeNumberAttribute(num: number): string {
255263

256264
return `[${stepCount * step}-${(stepCount + 1) * step}]`;
257265
}
266+
267+
/** Get a request from either a request or a response */
268+
function getRequest(resOrReq: Request | Response): Request {
269+
// @ts-ignore we check this
270+
return typeof resOrReq.request === 'function' ? (resOrReq as Response).request() : (resOrReq as Request);
271+
}

0 commit comments

Comments
 (0)