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.

Jest - RN 0.61.1 Platform issue

See original GitHub issue

Any test that uses something from react-native (such as Platform) gives the error TypeError: _NativePlatformConstantsIOS.default.getConstants is not a function All these tests are working in 0.59.5 but are broken in 0.61.x.

Screen Shot 2019-10-03 at 12 37 32 PM

React Native version:

System: OS: macOS Mojave 10.14.4 CPU: (12) x64 Intel® Core™ i7-8750H CPU @ 2.20GHz Memory: 1.40 GB / 16.00 GB Shell: 5.3 - /bin/zsh Binaries: Node: 8.11.4 - ~/.nvm/versions/node/v8.11.4/bin/node Yarn: 1.13.0 - ~/.yvm/versions/v1.13.0/bin/yarn npm: 6.7.0 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3 Android SDK: API Levels: 23, 24, 25, 26, 27, 28 Build Tools: 28.0.3 System Images: android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom IDEs: Android Studio: 3.4 AI-183.6156.11.34.5692245 Xcode: 10.3/10G8 - /usr/bin/xcodebuild npmPackages: react: 16.9.0 => 16.9.0 react-native: 0.61.1 => 0.61.1 npmGlobalPackages: react-native-cli: 2.0.1

Steps To Reproduce

  1. Run jest.

Describe what you expected to happen:

The tests should run

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
nicoache1commented, Oct 3, 2019

For anyone reading this i found a temporary solution because its not good to be linked to the path of the library.

jest.mock( 'react-native/Libraries/Utilities/NativePlatformConstantsIOS', () => ({ ...require.requireActual( 'react-native/Libraries/Utilities/NativePlatformConstantsIOS', ), getConstants: () => ({ forceTouchAvailable: false, interfaceIdiom: 'en', isTesting: false, osVersion: 'ios', reactNativeVersion: { major: 60, minor: 1, patch: 0, }, systemName: 'ios', }), }), )

2reactions
mrbrentkellycommented, Nov 12, 2019

I’m having a similar issue with Animated. Under the hood it uses Platform.isTesting() to determine if AnimatedMock.js should be used but that method is returning undefined for this.constants.isTesting https://github.com/facebook/react-native/blob/85ac9cf6c7e33f6b5b97df7b2560c1da67976e56/Libraries/Utilities/Platform.ios.js#L58

I’m currently working around it with the following…

jest.mock(
  'react-native/Libraries/Utilities/Platform',
  (): object => ({
    ...require.requireActual('react-native/Libraries/Utilities/Platform'),
    isTesting: (): boolean => true,
  })
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking platform detection in Jest and React Native
mock gets hoisted so it's not in beforeEach , so it's run before everything. A viable option seems to be monkey patching RN.Platform.OS...
Read more >
Mocking React Native 0.61 modules with Jest - altany's blog
Version 0.61 seems to be solving several issues that stopped my team from upgrading to 0.60 earlier. All seemed to be going well...
Read more >
ts-jest cannot find module | The AI Search Engine You Control
Using a 0.61.0 build of RN, write a Jest test(doesn't have to assert anything in particular). In that test, mock a React Native ......
Read more >
Upgrading React Native from 0.61.5 to 0.62.3 - Medium
Before you upgrade. Better safe than sorry. You should check the React Native issue tracker to see if there are any major issues...
Read more >
@rnx-kit/jest-preset - npm
@rnx-kit/jest-preset. Build npm version. A Jest preset with support for React Native platform-specific extensions and TypeScript.
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