From f16dd8bacb67d333234d6c4a77fe550f05cfbb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 31 Aug 2021 09:53:53 +0000 Subject: [PATCH 1/2] fix: don't run browser metrics in worker Minimal fix for getsentry/sentry-javascript#3934 --- packages/tracing/src/browser/metrics.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/tracing/src/browser/metrics.ts b/packages/tracing/src/browser/metrics.ts index d7bfd434ba5c..b8b3e51f3467 100644 --- a/packages/tracing/src/browser/metrics.ts +++ b/packages/tracing/src/browser/metrics.ts @@ -23,7 +23,7 @@ export class MetricsInstrumentation { private _clsEntry: LayoutShift | undefined; public constructor() { - if (!isNodeEnv() && global?.performance) { + if (!isNodeEnv() && global?.performance && global?.document) { if (global.performance.mark) { global.performance.mark('sentry-tracing-init'); } From 01a267a6f2247498773caf5d8bbc17dce0c9396f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 31 Aug 2021 10:31:59 +0000 Subject: [PATCH 2/2] test: add test case exercising this --- packages/tracing/test/browser/metrics.test.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/tracing/test/browser/metrics.test.ts b/packages/tracing/test/browser/metrics.test.ts index 114a9a480798..429d29131810 100644 --- a/packages/tracing/test/browser/metrics.test.ts +++ b/packages/tracing/test/browser/metrics.test.ts @@ -183,7 +183,25 @@ describe('MetricsInstrumentation', () => { trackers.forEach(tracker => expect(tracker).not.toBeCalled()); }); - it('initializes trackers when not on node and `global.performance` is available.', () => { + it('does not initialize trackers when not on node but `global.document` is not available (in worker)', () => { + // window not necessary for this test, but it is here to exercise that it is absence of document that is checked + addDOMPropertiesToGlobal(['performance', 'addEventListener', 'window']); + const processBackup = global.process; + global.process = undefined; + const documentBackup = global.document; + global.document = undefined; + + const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker => + jest.spyOn(MetricsInstrumentation.prototype as any, tracker), + ); + new MetricsInstrumentation(); + global.process = processBackup; + global.document = documentBackup; + + trackers.forEach(tracker => expect(tracker).not.toBeCalled()); + }); + + it('initializes trackers when not on node and `global.performance` and `global.document` are available.', () => { addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']); const backup = global.process; global.process = undefined;