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.

"Modern" fake-timer implementation doesn't work with PromiseJS.

See original GitHub issue

Awesome work on https://github.com/facebook/jest/pull/7776, thanks for that!!

šŸ› Bug Report

Iā€™m using Jest 26.1.0.

Testing async code with Promises, as described in this Jest doc, doesnā€™t seem to work with jest.useFakeTimers('modern'); (from https://github.com/facebook/jest/pull/7776; documented here) if youā€™re using the ā€˜promiseā€™ library from NPM.

For a while (starting in https://github.com/facebook/react-native/commit/3ff39870ce776c653823e1733363be0401896294), React Native has used that library as part of their Jest setup (which you get if you use preset: 'react-native'), so I expect this will affect most people using React Native (this is how I encountered it).

Otherwise, Iā€™m not sure how often people use that ā€˜promiseā€™ library, but itā€™s an official solution given in a Jest troubleshooting doc, and pops up as a workaround for some issues in this repo (e.g. here).

This is arguably a regression (or would be when ā€˜modernā€™ becomes the default), as this behavior isnā€™t observed with jest.useFakeTimers('legacy'); or jest.useRealTimers().

To Reproduce

E.g.,

global.Promise = require('promise');

jest.useFakeTimers('modern');

test('1 equals 1', async () => {
  await Promise.resolve();
  expect(1).toEqual(1);
});

Expected behavior

I expect the test to pass. Instead, five seconds go by and I get this failure:

  āœ• 1 equals 1 (5006 ms)

  ā— 1 equals 1

    : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

      3 | jest.useFakeTimers('modern');
      4 | 
    > 5 | test('1 equals 1', async () => {
        | ^
      6 |   await Promise.resolve();
      7 |   expect(1).toEqual(1);
      8 | });

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Object.<anonymous> (fetchData.test.js:5:1)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        5.762 s, estimated 6 s
Ran all test suites.
error Command failed with exit code 1.

Link to repl or repo (highly encouraged)

In the repro linked below, Iā€™ve closely imitated all the tests from the sections in the ā€œTesting Asynchronous Codeā€ doc that use Promises, to suggest that the failure happens in usual, well-documented ways of testing (provided youā€™re using PromiseJS).

In its current state, you should observe these timeout errors on tests that use Promises (just run yarn and yarn test).

Try uncommenting global.Promise = require('promise'); at the top of fetchData.test.js; the tests should all pass.

Try doing something other than jest.useFakeTimers('modern');; the tests should all pass.

(Incidentally, note that accidentally calling jest.useFakeTimers multiple times with different arguments in the same file seems disruptive and might hamper your investigation. Also, be sure to comment out jest.runAllTimers() if youā€™re using real timers.)

https://github.com/chrisbobbe/jest-async-modern-timers-repro

envinfo

  System:
    OS: macOS 10.15.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 10.20.1 - ~/.nvm/versions/node/v10.20.1/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - ~/.nvm/versions/node/v10.20.1/bin/npm
  npmPackages:
    jest: ^26.1.0 => 26.1.0 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:31
  • Comments:32

github_iconTop GitHub Comments

7reactions
Hedi-scommented, Jul 6, 2020

I have the same error with nock 12.0.3 and jest 26.1.0

The test case

test('nock', async () => {
    jest.useFakeTimers('modern');
    nock('http://modern').get("/").reply(200);
    await fetch(`http://modern`, { method: "GET" });
});

Failing :

 : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:
  > 18 |         test('nock', async () => {
    |         ^
    19 |             jest.useFakeTimers('modern');
    20 |             nock('http://modern').get("/").reply(200);
    21 |             await fetch(`http://modern`, { method: "GET" });

Itā€™s working when I replace

  • ā€˜modernā€™ by ā€˜legacyā€™ or
  • fetch(ā€¦) by Promise.resolve() (nock is not actually used)
6reactions
deimantasacommented, Sep 9, 2021

Is this moving along? As issue still persists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest fake timers with promises
useFakeTimers('modern ') works with Date but not with Promises since await flushPromises() never resolves. The best solution I found was to use @Ā ......
Read more >
Timer Mocks
In the following example we enable fake timers by calling jest.useFakeTimers() . This is replacing the original implementation ofĀ ...
Read more >
Fix the "not wrapped in act(...)" warning with Jest fake timers
This is based on my blog post: Fix the "not wrapped in act(...)" warning. ... Implement a simple Finite State Machine library in...
Read more >
Testing Node Promise-wrapped Timers Using Jest & Sinon.JS
... introduces how to test a few code examples in TypeScript that use both Timers and Promises. ... Your browser can't play this...
Read more >
@jest/fake-timers | Yarn - Package Manager
main. Features. Fixes. Chore & Maintenance. Performance. 29.3.1. Fixes. [jest-config] Do not warn about preset in ProjectConfig #13583Ā ...
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 Hashnode Post

No results found