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.

Support for useFakeTimers('modern')

See original GitHub issue

I would like to be able to use this lib to mock out dynamo but am reaching a blocking issue when using jest v26 features, namely useFakeTimers('modern').

Using the preset, any test that calls into the local db results in the following error:

TypeError: Cannot read property 'useFakeTimers' of undefined
...
jest.useFakeTimers(`modern`);
     ^

I attempted to patch in the calls to useFakeTimers to accept the "modern" type argument, but that did not change the outcome. Any test that does not include the call to the db works as expected. I am fairly new to using jest so I’m not counting out user error, but it seems like something is off only when this lib is involved in the test. Worth noting, this will become the default in the next major version of jest.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
mgmolisanicommented, Nov 20, 2020

Sure. Here is basically the testing I was able to do to see where tests failed to run. It did not matter where I set jest timers to 'modern'. timers: 'modern' in config, beforeAll, beforeEach, in the test body, all failed similarly.

describe(`test`, () => {
  it(`uses dynalite and modern timers`, async () => {
    jest.useFakeTimers(`modern`)
    // ^ This fails with that extremely weird error suggesting jest is undefined
    await someFunctionThatUsesTheDb()
  })
  it(`uses dynalite and legacy timers`, async () => {
    jest.useFakeTimers()
    // ^ This works but doesn't provide new APIs and will not be the default in jest v27
    await someFunctionThatUsesTheDb()
  })
  it(`doesnt use dynalite `, async () => {
    jest.useFakeTimers(`modern`)
    // ^ This works fine
    await someFunctionThatDoesntUseTheDb()
  })
})
1reaction
fresholliecommented, Nov 25, 2020

Release as v3.2.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest 26: Tick Tock
In Jest 26, this new implementation will remain opt-in and can be activated by calling jest.useFakeTimers('modern') or by passing modern to the ...
Read more >
Problems with using useFakeTimers('modern') in a create ...
I was trying to test a component that used Lodash's debounce function without having to slow the tests down by waiting for the...
Read more >
"Modern" fake-timer implementation doesn't work with ... - GitHub
Testing async code with Promises, as described in this Jest doc, doesn't seem to work with jest.useFakeTimers('modern'); (from #7776; ...
Read more >
How to use jest.useFakeTimers('modern') and jest ...
useFakeTimers ('modern') and jest.setSystemTime() but I don't know why it breaks my test. Does anyone know how to work with these two helpers ......
Read more >
Jest fake timers with promises - Stack Overflow
useFakeTimers ('modern') works with Date but not with Promises since await flushPromises() never resolves. The best solution I found was to ...
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