Skip to content

Commit 0619ed2

Browse files
committed
fix: Address PR comments
* ref: Switch from isBrowser -> ignoreDuration * test: Add sanity check test for sec to ms conversion * ref: Update session.close tests to assert on session instead of spying on implementation
1 parent cdf1c87 commit 0619ed2

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

packages/browser/src/sdk.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ function startSessionTracking(): void {
199199
return;
200200
}
201201

202-
hub.startSession({ isBrowser: true });
202+
// The session duration for browser sessions does not track a meaningful
203+
// concept that can be used as a metric.
204+
// Automatically captured sessions are akin to page views, and thus we
205+
// discard their duration.
206+
hub.startSession({ ignoreDuration: true });
203207
hub.captureSession();
204208

205209
// We want to create a session for every navigation as well
@@ -209,7 +213,7 @@ function startSessionTracking(): void {
209213
if (from === undefined || from === to) {
210214
return;
211215
}
212-
hub.startSession({ isBrowser: true });
216+
hub.startSession({ ignoreDuration: true });
213217
hub.captureSession();
214218
},
215219
type: 'history',

packages/hub/src/session.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Session implements SessionInterface {
2828
public environment?: string;
2929
public ipAddress?: string;
3030
public init: boolean = true;
31-
public isBrowser: boolean = false;
31+
public ignoreDuration: boolean = false;
3232

3333
public constructor(context?: Omit<SessionContext, 'started' | 'status'>) {
3434
// Both timestamp and started are in seconds since the UNIX epoch.
@@ -54,8 +54,8 @@ export class Session implements SessionInterface {
5454
}
5555

5656
this.timestamp = context.timestamp || timestampInSeconds();
57-
if (context.isBrowser) {
58-
this.isBrowser = context.isBrowser;
57+
if (context.ignoreDuration) {
58+
this.ignoreDuration = context.ignoreDuration;
5959
}
6060
if (context.sid) {
6161
// Good enough uuid validation. — Kamil
@@ -70,11 +70,7 @@ export class Session implements SessionInterface {
7070
if (typeof context.started === 'number') {
7171
this.started = context.started;
7272
}
73-
// The session duration for browser sessions does not track a meaningful
74-
// concept that can be used as a metric.
75-
// Automatically captured sessions are akin to page views, and thus we
76-
// discard their duration.
77-
if (this.isBrowser) {
73+
if (this.ignoreDuration) {
7874
this.duration = undefined;
7975
} else if (typeof context.duration === 'number') {
8076
this.duration = context.duration;

packages/hub/test/session.test.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ describe('Session', () => {
77
it('initializes with the proper defaults', () => {
88
const session = new Session().toJSON();
99

10-
const sessionStartTime = session.timestamp;
11-
10+
// Grab current year to check if we are converting from sec -> ms correctly
11+
const currentYear = new Date(timestampInSeconds() * 1000).toISOString().slice(0, 4);
1212
expect(session).toEqual({
1313
attrs: {},
1414
duration: 0,
1515
errors: 0,
1616
init: true,
1717
sid: expect.any(String),
18-
started: expect.any(String),
18+
started: expect.stringMatching(currentYear),
1919
status: SessionStatus.Ok,
20-
timestamp: expect.any(String),
20+
timestamp: expect.stringMatching(currentYear),
2121
});
2222

2323
expect(session.sid).toHaveLength(32);
2424

2525
// started and timestamp should be the same on creation
26-
expect(session.started).toEqual(sessionStartTime);
27-
expect(session.timestamp).toEqual(sessionStartTime);
26+
expect(session.started).toEqual(session.timestamp);
2827
});
2928

3029
describe('update', () => {
@@ -41,9 +40,13 @@ describe('Session', () => {
4140
['sets an did', { did: 'specialID123' }, { did: 'specialID123' }],
4241
['overwrites user did with custom did', { did: 'custom-did', user: { id: 'user-id' } }, { did: 'custom-did' }],
4342
['sets a started time', { started: time }, { started: new Date(time * 1000).toISOString() }],
44-
['does not set a duration for browser env', { isBrowser: true }, { duration: undefined }],
43+
['does not set a duration for browser env', { ignoreDuration: true }, { duration: undefined }],
4544
['sets a duration', { duration: 12000 }, { duration: 12000 }],
46-
['does not use custom duration for browser env', { duration: 12000, isBrowser: true }, { duration: undefined }],
45+
[
46+
'does not use custom duration for browser env',
47+
{ duration: 12000, ignoreDuration: true },
48+
{ duration: undefined },
49+
],
4750
[
4851
'does not set a negative duration',
4952
{ timestamp: 10, started: 100 },
@@ -88,30 +91,26 @@ describe('Session', () => {
8891
describe('close', () => {
8992
it('exits a normal session', () => {
9093
const session = new Session();
91-
const mockUpdate = jest.spyOn(session, 'update');
92-
expect(mockUpdate).toHaveBeenCalledTimes(0);
94+
expect(session.status).toEqual(SessionStatus.Ok);
9395
session.close();
94-
expect(mockUpdate).toHaveBeenCalledTimes(1);
95-
expect(mockUpdate).toHaveBeenLastCalledWith({ status: SessionStatus.Exited });
96+
expect(session.status).toEqual(SessionStatus.Exited);
9697
});
9798

9899
it('updates session status when give status', () => {
99100
const session = new Session();
100-
const mockUpdate = jest.spyOn(session, 'update');
101+
expect(session.status).toEqual(SessionStatus.Ok);
101102

102-
session.close(SessionStatus.Crashed);
103-
expect(mockUpdate).toHaveBeenCalledTimes(1);
104-
expect(mockUpdate).toHaveBeenLastCalledWith({ status: SessionStatus.Crashed });
103+
session.close(SessionStatus.Abnormal);
104+
expect(session.status).toEqual(SessionStatus.Abnormal);
105105
});
106106

107-
it('only changes status ok', () => {
107+
it('only changes status ok to exited', () => {
108108
const session = new Session();
109-
session.status = SessionStatus.Abnormal;
110-
const mockUpdate = jest.spyOn(session, 'update');
109+
session.update({ status: SessionStatus.Crashed });
110+
expect(session.status).toEqual(SessionStatus.Crashed);
111111

112112
session.close();
113-
expect(mockUpdate).toHaveBeenCalledTimes(1);
114-
expect(mockUpdate).toHaveBeenLastCalledWith();
113+
expect(session.status).toEqual(SessionStatus.Crashed);
115114
});
116115
});
117116
});

packages/types/src/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export interface SessionContext {
5353
errors?: number;
5454
user?: User | null;
5555
// Sessions are handled differently in browser environments
56-
isBrowser?: boolean;
56+
ignoreDuration?: boolean;
5757
}
5858

5959
/**

0 commit comments

Comments
 (0)