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
4 changes: 2 additions & 2 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-lines */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Measurements, SpanContext } from '@sentry/types';
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, logger } from '@sentry/utils';
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';

import { Span } from '../span';
import { Transaction } from '../transaction';
Expand All @@ -22,7 +22,7 @@ export class MetricsInstrumentation {
private _lcpEntry: LargestContentfulPaint | undefined;

public constructor() {
if (global && global.performance) {
if (!isNodeEnv() && global?.performance) {
if (global.performance.mark) {
global.performance.mark('sentry-tracing-init');
}
Expand Down
32 changes: 31 additions & 1 deletion packages/tracing/test/browser/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Span, Transaction } from '../../src';
import { _startChild, addResourceSpans, ResourceEntry } from '../../src/browser/metrics';
import { _startChild, addResourceSpans, MetricsInstrumentation, ResourceEntry } from '../../src/browser/metrics';
import { addDOMPropertiesToGlobal } from '../testutils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
declare var global: any;

describe('_startChild()', () => {
it('creates a span with given properties', () => {
Expand Down Expand Up @@ -167,3 +171,29 @@ describe('addResourceSpans', () => {
);
});
});

describe('MetricsInstrumentation', () => {
it('does not initialize trackers when on node', () => {
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);

new MetricsInstrumentation();

trackers.forEach(tracker => expect(tracker).not.toBeCalled());
});

it('initializes trackers when not on node and `global.performance` is available.', () => {
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
const backup = global.process;
global.process = undefined;

const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);
new MetricsInstrumentation();
global.process = backup;

trackers.forEach(tracker => expect(tracker).toBeCalled());
});
});