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.

Cannot pass tests via jest if react-native-share was used

See original GitHub issue

Steps to reproduce

  1. Add import Share from 'react-native-share'; to any component.
  2. Run jest

Expected behaviour

Test should be passed.

Actual behaviour

  ● Test suite failed to run

    TypeError: Cannot read property 'FACEBOOK' of undefined

    >  9 | import Share from 'react-native-share';

Environment

React Native Environment Info: System: OS: macOS High Sierra 10.13.6 CPU: (4) x64 Intel® Core™ i5-4288U CPU @ 2.60GHz Memory: 33.05 MB / 8.00 GB Shell: 5.3 - /bin/zsh Binaries: Node: 8.13.0 - ~/.nvm/versions/node/v8.13.0/bin/node Yarn: 1.12.3 - /usr/local/bin/yarn npm: 6.4.1 - ~/.nvm/versions/node/v8.13.0/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3 Android SDK: API Levels: 26, 27, 28 Build Tools: 27.0.3, 28.0.3 System Images: android-27 | Google Play Intel x86 Atom IDEs: Android Studio: 3.2 AI-181.5540.7.32.5056338 Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild npmPackages: react: 16.6.1 => 16.6.1 react-native: 0.57.5 => 0.57.5 npmGlobalPackages: react-native-cli: 2.0.1

react-native-share

"react-native-share": "^1.1.3"

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

13reactions
elisechantcommented, Sep 7, 2019

The code you need in your jest setup file is:

jest.mock('react-native-share', () => {
  return {
    open: jest.fn(),
  };
});
3reactions
mikehardycommented, Mar 30, 2019

On another repo (https://github.com/react-native-community/react-native-device-info) I help maintain, we explicitly tell people to mock:

[tests] - Cannot run my test suite when using this library
react-native-device-info contains native code, and needs to be mocked. Jest Snapshot support may work though.

Here's how to do it with jest for example:

// in your package.json:
"jest": {
  "setupFiles": [
    "./testenv.js"
  ],


// testenv.js:
jest.mock('react-native-device-info', () => {
  return {
    getModel: jest.fn(),
  };
});

Though we did just merge something that may make jest snapshots work (I don’t use them (yet?) so I don’t know): https://github.com/react-native-community/react-native-device-info/pull/375

Which is just to say this may be completely expected behavior. The fix may just be documentation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot pass tests via jest if react-native-share was used #407
Steps to reproduce Add import Share from 'react-native-share'; to any component. Run jest Expected behaviour Test should be passed.
Read more >
Cannot pass tests via jest if react-native-share was used #407
Steps to reproduce. Add import Share from 'react-native-share'; to any component. Run jest. Expected behaviour. Test should be passed. Actual behaviour.
Read more >
Mocking platform detection in Jest and React Native
Is there a sensible way to mock this with Jest and/or something else, so I can test both branches in one test run?...
Read more >
Testing React Native apps with Jest | by Emily Xiong - Nx Blog
How to write unit and e2e tests for React Native apps using Jest in an Nx ... The first time running the unit...
Read more >
React Testing Library Tutorial - Robin Wieruch
In this React Testing Library tutorial, we will go through all the steps necessary to unit test and integration test your React components...
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