question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:32 (27 by maintainers)

github_iconTop GitHub Comments

3reactions
Smrtnykcommented, Jul 30, 2021

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

3reactions
itayperrycommented, Apr 27, 2021

I’ll take a look into it - I might ask for some advice 😃 Hope I’ll be able to help! 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Timer Mocks - Jest
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation of ...
Read more >
Fake timers - Sinon.JS
Fake timers are synchronous implementations of setTimeout and friends that Sinon.JS can overwrite the global functions with to allow you to more easily...
Read more >
Jest mock timers not working as expected asynchronously
I want to test it with a mock api where the api responses are delayed as they would be in real life, but...
Read more >
@sinonjs/fake-timers - npm
Automatically incrementing mocked time ... FakeTimers supports the possibility to attach the faked timers to any change in the real system time.
Read more >
@jest/fake-timers | Yarn - Package Manager
... [jest-mock] Fix function overloads for spyOn to allow more correct type inference in complex object (#12442); [jest-mock] Handle overridden ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found