"Modern" fake-timer implementation doesn't work with PromiseJS.
See original GitHub issueAwesome 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:
- Created 3 years ago
- Reactions:31
- Comments:32
Top GitHub Comments
I have the same error with nock 12.0.3 and jest 26.1.0
The test case
Failing :
Itās working when I replace
Is this moving along? As issue still persists.