Skip to content

fix(tests): Clear mocks in MetricsInstrumentation tests. #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2021
Merged
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
35 changes: 21 additions & 14 deletions packages/tracing/test/browser/metrics.test.ts
Original file line number Diff line number Diff line change
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());
});
});