Skip to content

Commit

Permalink
fix stuff actually
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jan 3, 2025
1 parent 5b717ae commit 6380c4e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { expect } from '@playwright/test';
import type { SessionContext } from '@sentry/core';

import { sentryTest } from '../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers';
import { getFirstSentryEnvelopeRequest, waitForSession } from '../../../utils/helpers';

sentryTest('should update session when an error is thrown.', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const pageloadSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
const updatedSession = (
await Promise.all([page.locator('#throw-error').click(), getFirstSentryEnvelopeRequest<SessionContext>(page)])
)[1];

const updatedSessionPromise = waitForSession(page);
await page.locator('#throw-error').click();
const updatedSession = await updatedSessionPromise;

expect(pageloadSession).toBeDefined();
expect(pageloadSession.init).toBe(true);
Expand All @@ -25,9 +27,10 @@ sentryTest('should update session when an exception is captured.', async ({ getL
const url = await getLocalTestUrl({ testDir: __dirname });

const pageloadSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
const updatedSession = (
await Promise.all([page.locator('#capture-exception').click(), getFirstSentryEnvelopeRequest<SessionContext>(page)])
)[1];

const updatedSessionPromise = waitForSession(page);
await page.locator('#capture-exception').click();
const updatedSession = await updatedSessionPromise;

expect(pageloadSession).toBeDefined();
expect(pageloadSession.init).toBe(true);
Expand Down
30 changes: 24 additions & 6 deletions dev-packages/browser-integration-tests/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
Event,
EventEnvelope,
EventEnvelopeHeaders,
SessionContext,
TransactionEvent,
} from '@sentry/core';

Expand Down Expand Up @@ -157,7 +158,7 @@ export const countEnvelopes = async (
* @param {{ path?: string; content?: string }} impl
* @return {*} {Promise<void>}
*/
async function runScriptInSandbox(
export async function runScriptInSandbox(
page: Page,
impl: {
path?: string;
Expand All @@ -178,7 +179,7 @@ async function runScriptInSandbox(
* @param {string} [url]
* @return {*} {Promise<Array<Event>>}
*/
async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>> {
export async function getSentryEvents(page: Page, url?: string): Promise<Array<Event>> {
if (url) {
await page.goto(url);
}
Expand Down Expand Up @@ -250,6 +251,25 @@ export function waitForTransactionRequest(
});
}

export async function waitForSession(page: Page): Promise<SessionContext> {
const req = await page.waitForRequest(req => {
const postData = req.postData();
if (!postData) {
return false;
}

try {
const event = envelopeRequestParser<SessionContext>(req);

return typeof event.init === 'boolean' && event.started !== undefined;
} catch {
return false;
}
});

return envelopeRequestParser<SessionContext>(req);
}

/**
* We can only test tracing tests in certain bundles/packages:
* - NPM (ESM, CJS)
Expand Down Expand Up @@ -353,7 +373,7 @@ async function getMultipleRequests<T>(
/**
* Wait and get multiple envelope requests at the given URL, or the current page
*/
async function getMultipleSentryEnvelopeRequests<T>(
export async function getMultipleSentryEnvelopeRequests<T>(
page: Page,
count: number,
options?: {
Expand All @@ -374,7 +394,7 @@ async function getMultipleSentryEnvelopeRequests<T>(
* @param {string} [url]
* @return {*} {Promise<T>}
*/
async function getFirstSentryEnvelopeRequest<T>(
export async function getFirstSentryEnvelopeRequest<T>(
page: Page,
url?: string,
requestParser: (req: Request) => T = envelopeRequestParser as (req: Request) => T,
Expand All @@ -388,5 +408,3 @@ async function getFirstSentryEnvelopeRequest<T>(

return req;
}

export { runScriptInSandbox, getMultipleSentryEnvelopeRequests, getFirstSentryEnvelopeRequest, getSentryEvents };
2 changes: 1 addition & 1 deletion packages/core/src/baseclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
}

const session = currentScope.getSession() || isolationScope.getSession();
if (!isError && session) {
if (isError && session) {
this._updateSessionFromEvent(session, processedEvent);
}

Expand Down

0 comments on commit 6380c4e

Please sign in to comment.