Fake timers override mocks on performance object
See original GitHub issue- FakeTimers version : I am using jest: 26.6.3 which uses FakeTimers
- Environment : Windows 10 10.0.19042
- Other libraries you are using: jest 26.6.3
I am actually redirected here from https://github.com/facebook/jest/issues/11330 I am using jest with jsdom
What did you expect to happen? I would expect that properties that I added to be preserved after fake timers install
What actually happens performance object is overriden and properties I added before install are gone
How to reproduce I will explain how I did it in jest which they say they just only install FakeTimers and don’t do any logic Please check this https://github.com/facebook/jest/issues/11330
I have global mocks file where before testing I do this
Object.defineProperties(performance, {
timing: {
value: {
connectStart: now + 1,
connectEnd: now + 1,
domComplete: now + 100,
domContentLoadedEventEnd: now + 50,
domContentLoadedEventStart: now + 40,
domInteractive: now + 39,
domLoading: now + 10,
domainLookupStart: now + 1,
domainLookupEnd: now + 1,
fetchStart: now + 1,
loadEventEnd: now + 1000,
loadEventStart: now + 1000,
navigationStart: now,
redirectEnd: 0,
redirectStart: 0,
requestStart: now + 1,
responseStart: now + 2,
responseEnd: now + 30,
secureConnectionStart: 0,
unloadEventEnd: 0,
unloadEventStart: 0
},
writable: true
},
navigation: {
value: {
type: 0
},
writable: true
},
getEntries: {
value: (): PerformanceEntry[] => {
return [];
},
writable: true
},
getEntriesByType: {
value: (type: string): (PerformanceNavigationTiming | PerformanceResourceTiming)[] => {
return performanceObj.filter(perf => {
return perf.entryType === type;
});
},
writable: true
},
getEntriesByName: {
value: (): PerformanceEntry[] => {
return [];
},
writable: true
},
setResourceTimingBufferSize: {
value: jest.fn(),
writable: true
},
clearResourceTimings: {
value: jest.fn(),
writable: true
}
});
My app relies on these values. After I install FakeTimers the performance.timing is gone With jest legacy timers (which are not sinon/FakeTimers) this is not happening
From what I get is that FakeTimers mock performance.now() etc… but is it possible to preserve values that were added to performance object before clock install?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:32 (27 by maintainers)
I do understand that you need to overwrite performance.now() due to timers controll in sync manner, but can you explain why clock.performance[‘getEntries’] = NOOP_ARRAY; clock.performance[‘getEntriesByName’] = NOOP_ARRAY; clock.performance[‘getEntriesByType’] = NOOP_ARRAY; need to be overriden? In my case I mock these in my global setup that happens before tests run (they do not give anything in jsdom) so I can have some mock resources to work in my tests. In your proposal after I install the clock it will override the spies and I will need to remock them again
I’ll take a look into it - I might ask for some advice 😃 Hope I’ll be able to help! 🙂