facebook/react-native

`global.Promise` replacement in Jest setup causing problems.

chrisbobbe opened this issue ยท 2 comments

Hi! ๐Ÿ‘‹

What is the purpose of replacing the global Promise in this line of jest/setup.js:

global.Promise = jest.requireActual('promise');

It looks like it was first introduced in 3ff3987 (quite a while ago) and has remained in a similar form since then. There's no explanation of this choice specifically (if I understand correctly), so I think if it must remain, it would be good to put a code comment there. ๐Ÿ™‚

Description

'promise' refers to the NPM package with that name.

It interferes with Jest when using their new "modern" implementation of fake timers (documented here), as I describe at jestjs/jest#10221.

One interesting note on that issue, from a Jest maintainer, is this:

As an aside, one should never replace global.Promise [...]. E.g. when using async-await you will always get the native Promise regardless of the value of global.Promise.

Might it be OK and appropriate to remove the line?

React Native version:

System:
    OS: macOS 10.15.5
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 4.84 GB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  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
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 23, 25, 26, 27, 28, 29
      Build Tools: 27.0.3, 28.0.3, 29.0.2
      System Images: android-28 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6241897
    Xcode: 11.5/11E608c - /usr/bin/xcodebuild
  npmPackages:
    react: 16.8.6 => 16.8.6 
    react-native: 0.60.6 => 0.60.6

Steps To Reproduce

Reproduction of the issue (and more discussion) at jestjs/jest#10221.

Expected Results

React Native's Jest setup doesn't interfere with ordinary, well-documented ways of testing with Jest.

Copy-pasting the error output I mention at jestjs/jest#10221 to help others find this issue:

  โœ• 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.

Fixed in #34659