Skip to content

Commit 1d1b41c

Browse files
authored
fix(tests): Clear mocks in MetricsInstrumentation tests. (#3944)
1 parent f5b8e43 commit 1d1b41c

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

packages/tracing/test/browser/metrics.test.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ describe('addResourceSpans', () => {
173173
});
174174

175175
describe('MetricsInstrumentation', () => {
176+
afterEach(() => {
177+
jest.clearAllMocks();
178+
});
179+
176180
it('does not initialize trackers when on node', () => {
177181
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
178182
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
@@ -183,35 +187,38 @@ describe('MetricsInstrumentation', () => {
183187
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
184188
});
185189

186-
it('does not initialize trackers when not on node but `global.document` is not available (in worker)', () => {
187-
// window not necessary for this test, but it is here to exercise that it is absence of document that is checked
188-
addDOMPropertiesToGlobal(['performance', 'addEventListener', 'window']);
189-
const processBackup = global.process;
190+
it('initializes trackers when not on node and `global.performance` and `global.document` are available.', () => {
191+
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
192+
193+
const backup = global.process;
190194
global.process = undefined;
191-
const documentBackup = global.document;
192-
global.document = undefined;
193195

194196
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
195197
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
196198
);
199+
197200
new MetricsInstrumentation();
198-
global.process = processBackup;
199-
global.document = documentBackup;
201+
global.process = backup;
200202

201-
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
203+
trackers.forEach(tracker => expect(tracker).toBeCalled());
202204
});
203205

204-
it('initializes trackers when not on node and `global.performance` and `global.document` are available.', () => {
205-
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
206-
const backup = global.process;
206+
it('does not initialize trackers when not on node but `global.document` is not available (in worker)', () => {
207+
// window not necessary for this test, but it is here to exercise that it is absence of document that is checked
208+
addDOMPropertiesToGlobal(['performance', 'addEventListener', 'window']);
209+
210+
const processBackup = global.process;
207211
global.process = undefined;
212+
const documentBackup = global.document;
213+
global.document = undefined;
208214

209215
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
210216
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
211217
);
212218
new MetricsInstrumentation();
213-
global.process = backup;
219+
global.process = processBackup;
220+
global.document = documentBackup;
214221

215-
trackers.forEach(tracker => expect(tracker).toBeCalled());
222+
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
216223
});
217224
});

0 commit comments

Comments
 (0)