Skip to content

Commit

Permalink
fix(tests): Clear mocks in MetricsInstrumentation tests. (#3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan committed Aug 31, 2021
1 parent f5b8e43 commit 1d1b41c
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/tracing/test/browser/metrics.test.ts
Expand Up @@ -173,6 +173,10 @@ describe('addResourceSpans', () => {
});

describe('MetricsInstrumentation', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('does not initialize trackers when on node', () => {
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
Expand All @@ -183,35 +187,38 @@ describe('MetricsInstrumentation', () => {
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
});

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;
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;
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;
global.process = backup;

trackers.forEach(tracker => expect(tracker).not.toBeCalled());
trackers.forEach(tracker => expect(tracker).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;
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 = backup;
global.process = processBackup;
global.document = documentBackup;

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

0 comments on commit 1d1b41c

Please sign in to comment.