Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '0.1',
initialScope: {
user: {
id: '1337',
email: '[email protected]',
username: 'user1337',
},
},
debug: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<a id='navigate' href="foo">Navigate</button>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Route } from '@playwright/test';
import { expect } from '@playwright/test';
import type { SessionContext } from '@sentry/types';

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

sentryTest('should start a new session on pageload.', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });
const session = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);

expect(session).toBeDefined();
expect(session.init).toBe(true);
expect(session.errors).toBe(0);
expect(session.status).toBe('ok');
expect(session.did).toBe('1337');
});

sentryTest('should start a new session with navigation.', async ({ getLocalTestPath, page, browserName }) => {
// Navigations get CORS error on Firefox and WebKit as we're using `file://` protocol.
if (browserName !== 'chromium') {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });
await page.route('**/foo', (route: Route) => route.fulfill({ path: `${__dirname}/dist/index.html` }));

const initSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);

await page.click('#navigate');

const newSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);

expect(newSession).toBeDefined();
expect(newSession.init).toBe(true);
expect(newSession.errors).toBe(0);
expect(newSession.status).toBe('ok');
expect(newSession.sid).toBeDefined();
expect(initSession.sid).not.toBe(newSession.sid);
expect(newSession.did).toBe('1337');
});
4 changes: 2 additions & 2 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export function startSession(context?: SessionContext): Session {
const session = makeSession({
release,
environment,
user: isolationScope.getUser(),
user: currentScope.getUser() || isolationScope.getUser(),
...(userAgent && { userAgent }),
...context,
});
Expand Down Expand Up @@ -413,7 +413,7 @@ export function endSession(): void {
const isolationScope = getIsolationScope();
const currentScope = getCurrentScope();

const session = isolationScope.getSession();
const session = currentScope.getSession() || isolationScope.getSession();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was also incorrect before

if (session) {
closeSession(session);
}
Expand Down