How to add jest specific setup to create-react-native-app? - react-native

I created a project with create-react-native-app and I want to use jest-enzyme matchers, in their Readme there is a section on how to use it with create-react-app, but I did not find any info on how to use it with create-react-native-app.
To add these matchers all I need is to create a jest setup file, but create-react-native-app does not seen to allow for custom jest setup file.
So it's possible to a use a custom jest setup file without ejecting from create-react-native-app?
Also is there any way to add a jest setup file for create-react-native-app?

Maybe what you are looking for is http://airbnb.io/enzyme/docs/guides/react-native.html which explains as follows:
Unfortunately, React Native has many environmental dependencies that can be hard to simulate without a host device.
This can be difficult when you want your test suite to run with typical Continuous Integration servers such as Travis.
A pure JS mock of React Native exists and can solve this problem in the majority of use cases.
To install it, run:
npm i --save-dev react-native-mock
After adding that dependency you need to import the /mock within your tests you should good to go. Maybe I am out of context, you can update your problem and give us some extra context so we can craft a better answer.
Ultimately, there is something important to mention in this thread: https://github.com/airbnb/enzyme/issues/928 for the time of this post RN is in version 0.47 and working with react 16 alpha which is not a supported version for enzyme (the people at enzyme only work with rc versions) so if that is your case things looks quite difficult as there is no official support, however, last comment https://github.com/airbnb/enzyme/issues/928#issuecomment-324584942 offers unofficial alternatives
PD: make sure you follow the installation instructions here: http://airbnb.io/enzyme/docs/installation/index.html

Related

React-Native, React-Native-Web and React-Navigation/Stack

Has anyone been able to get React-Native 0.7, React-Native-Web and React-Navigation/Stack 6.2.3 to work on web? I have a small app working on iOS and Android which I am trying to enable on web too. iOS and Android portion work fine and using metro.
However, webpack build continues to error out with "Module parse failed: Unexpected token" on the NavigationContainer.
I tried searching both react-native-web and react-navigation Issues on their respective GitHub Repositories. However, could not find something to solve my issue. Also tried modifying webpack.config with several different configurations/loaders.
Used links below as a reference too:
https://arry.medium.com/how-to-add-react-native-web-to-an-existing-react-native-project-eb98c952c12f
https://gist.github.com/skabbes/0bfa0a969aac8ec13f716dda1ad2ab43
Yes, it happens that I maintain a library that does just that: creates a bare React Native app that also runs on the web: 🌒 Luna - https://github.com/criszz77/luna
Here is the live example: https://criszz77.github.io/luna/
You can find more on the wiki page about how it's implemented and you can also implement it by yourself: https://github.com/criszz77/luna/wiki
By using react-scripts, you don't have to handle webpack, babel and other hard configs by yourself.
It currently doesn't run on 0.7 (didn't have time to make the update), but it's in plan to support all the latest versions of course ASAP.
EDIT: Updated to 0.70.3 🎉

is a vanilla axios npm install considered a legit way to call api's in rn?

I've used axios in vanilla React apps which has worked well for me and axios seems to be a popular choice for this purpose. I did an npm install of axios into my RN app this evening. I wired it up to call an API and it returned data from the API as expected in my local Android emulator.
Is it really that easy to use axios in RN? I didn't configure any gradle dependencies or do any other type of special setup step or configuration beyond the basic npm install. I'm assuming that this setup will run as-is on iOS as well. If it's really that simple then that makes me pretty happy compared to all the hoops I've had to jump through to get the various firebase modules configured and working properly
Yes most 3rd party modules for React Native are very easy to use and only require you to run npm install - packetname - without touching any native code or extra files like gradle for example.

I need help uploading something to expo

I want to create my own podcast app.
I found this pre-written code online (github.com/diego3g/rocketcasts) and want to tweak it in order to create my own app.
How do I upload this code into expo?
Unfortunately that React-Native app was not built using Expo. You can tell because it has ios and android folders in its root directory.
Digging deeper you can see that it uses react-native-config as a dependency. This dependency requires access to native code, which Expo does not allow. You would have to go through and remove any dependency that requires native code if you wish to use Expo. This may remove functionality and could take some time to resolve issues.
Also this application was built using react-native: 0.47.2. Currently the most recent version of react-native is 0.57.8 with 0.58.0 just around the corner. That is quite a jump to make if you plan on upgrading the react-native version. Upgrading RN is not as simple as just changing a version code.
Similarly the repos last commit was over a year ago. That means that the majority of dependencies will be out of date and will need upgraded. Again, some dependencies aren’t a simple upgrade you would need to check every single one to make sure that it doesn’t cause unforeseen problems. You would also need to check their compatibility with Expo.
Personally I wouldn’t copy the project. I would look at how they have implemented the functionality and then in a new react-native project I would re-implement the functionality. That way all dependencies would be up to date and you would know that it would work.
Going down the path of editing an existing project like this one may be quite a challenge.
Either way good luck.
You can find more information about Expo here https://docs.expo.io/
And you can find a good explanation of the differences between Expo and react-native init here What is the difference between Expo and React Native?
The standard way to tweak an existing GitHub repository you don't own is to:
fork the repo (that is a GitHub operation, creating a copy of the repo in your account)
clone it and modify it: you can then push back to your GitHub fork.

Webpack HMR with React Native

Is there a recent Webpack HMR react-native boilerplate out there?
I am having trouble finding a best practice for integrating an existing setup with Webpack and HMR to be used with react-native and the react-native packager. The resources I found online on this topic are dated.
I know that it uses Facebook's watchman.
I also know of the webpack-watchman-plugin, but I don't think that will allow me to trigger a webpack build and serve the compiled assets to react-native. I know that watchman has -- trigger functionality, but don't think digging in the packager local CLI is the answer either.
I'm at a loss for finding decent documentation for Facebook's packager, and not sure where to get started on this. My goal is to take an existing boilerplate used for web React, and use the boilerplate setup for react-native. I understand about the rendering differences, this is mostly the build tool part.
I've resolved this issue by not trying to integrate webpack with react-native, but by not using webpack at all and using react-native's packager. Still working on it, but I like it so far! Using it for my Open-Source react-native boilerplate project, strangeluv-native

Configure Mocha/Babel to resolve React Native platform extensions?

I'm using Mocha with react-native-mock to run tests on React Native applications. However, I'm having issues when running tests on components which import platform-specific files, e.g. require('./MyComponent') resolves to MyComponent.android.js when running on Android. I'd like to be able to configure Mocha or Babel (whichever is appropriate) to be able to resolve these requires based on some configuration, e.g. an environment variable.
I'm aware of another solution which involves adding another file manually, but I'm specifically looking for something that automates this.