<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Natalia Malesa on Medium]]></title>
        <description><![CDATA[Stories by Natalia Malesa on Medium]]></description>
        <link>https://medium.com/@nmalesa?source=rss-78f5e065d2b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1F_I9h_2VBgUjpQhGjc3eQ.jpeg</url>
            <title>Stories by Natalia Malesa on Medium</title>
            <link>https://medium.com/@nmalesa?source=rss-78f5e065d2b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 16:33:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nmalesa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Test If Your React Native Module Integrates With Android and iOS]]></title>
            <link>https://medium.com/better-programming/how-to-test-if-your-react-native-module-integrates-with-android-and-ios-aa18f43f296a?source=rss-78f5e065d2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/aa18f43f296a</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Natalia Malesa]]></dc:creator>
            <pubDate>Fri, 16 Sep 2022 11:43:35 GMT</pubDate>
            <atom:updated>2022-09-16T11:43:35.937Z</atom:updated>
            <content:encoded><![CDATA[<h4>Integration tests on React Native with Cavy</h4><figure><img alt="Brown and white guinea pig sitting on open notebook in front of computer" src="https://cdn-images-1.medium.com/max/1024/0*vDL6CMu0A8A9bBKQ" /><figcaption>Photo by <a href="https://unsplash.com/photos/0EhFkzIinlk">Dan Barrett</a> on <a href="https://unsplash.com/@brandman79?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In a mobile app, a feature may contain multiple modules that must communicate with each other. Unit testing checks that the individual modules work as intended. End-to-end (E2E) testing makes sure that the feature supports expected user behavior. Integration testing, however, falls somewhere in between — it tests the contracts between the modules of a feature and checks that the modules interact as planned.</p><p>An example use case for integration testing is verifying how an internal module, such as a JavaScript function for making in-app purchases in a React Native app, interacts with native software like Apple StoreKit or the Google Play Billing Library. Integration testing is especially important for React Native and other cross-platform mobile apps because one piece of code must successfully communicate with two different operating systems: Android and iOS.</p><p>In this tutorial, you will learn how to use the <a href="https://cavy.app/">Cavy test framework</a> to build and run integration tests in React Native apps.</p><h3><strong>Learning Outcomes</strong></h3><ul><li>Create a test helper component that will allow you to use Cavy for integration testing without interacting with the app’s UI</li><li>Write and run a Cavy integration test</li></ul><h3>A Practical Use Case for Integration Testing</h3><p>In our React Native app, we needed to test a file corruption fix. Android 10 has a known <a href="https://issuetracker.google.com/issues/135714729?pli=1">bug</a> in which writing data to an existing file may corrupt that file under certain circumstances. To fix this issue, we built an atomic file writing package for React Native: <a href="https://github.com/nmalesa/react-native-atomic-file-ops">react-native-atomic-file-ops</a>.</p><p>We wanted to test this package in our app. Testing mobile OS operations requires native code to run on a device / emulator / simulator. Jest and other JavaScript testing frameworks could not provide the coverage we needed because the bug affected how our app interacted with a particular Android version. We needed to test that data written to a file on Android 10 from a React Native app could be rewritten without corrupting the file. This communication looks like:</p><figure><img alt="Diagram showing how a React Native module communicates to native modules via a bridge" src="https://cdn-images-1.medium.com/max/960/1*GfeMChnK4lX-qh2cC_ZECA.png" /><figcaption>Communication between React Native and native modules</figcaption></figure><h4>Why Cavy?</h4><p>To test this communication in Jest requires mock functions. While Jest is useful for unit testing React Native components and JavaScript methods, it cannot access the mobile OS. Any interactions with the OS must be mocked. We wanted to run our Android code on a device and test the real — not mocked — behavior of our package.</p><p>React Native E2E test frameworks, like Cavy and <a href="https://github.com/wix/Detox">Detox</a>, provided a starting point for us to determine how to run integration tests. These frameworks support testing on emulators and devices. However, testing the contracts from the React Native module to native file systems in our app did not require UI interaction. We did not want to tie the file writing module and its tests to a real UI in our app.</p><p>Cavy’s architecture supports this need. The Cavy tester imports your app and embeds it in Cavy’s &lt;Tester /&gt; component. This pattern is customizable, allowing you to import, embed, and test any React Native component. Instead of importing your entire app for E2E testing, you can import a single component to test. As you will see when you start working on the example app, that component does not have to be part of your app’s UI.</p><p>See the example tester in Cavy’s documentation: “<a href="https://cavy.app/docs/getting-started/setting-cavy-up">Setting up the Cavy Tester</a>”.</p><p>Ready to write your first integration test with Cavy? Let’s get started!</p><h3>Write Integration Tests with Cavy</h3><p>You will build an app to test that a React Native module integrates with Android and iOS. In this example, you will test how react-native-atomic-file-ops communicates with native file systems.</p><h4>Getting Started</h4><p>Follow <a href="https://reactnative.dev/docs/environment-setup">React Native’s instructions for setting up your preferred development environment</a>. Use the React Native CLI (not Expo) to make sure the app can access the mobile OS.</p><p>Once your environment is set up, initialize your React Native app:</p><pre>npx react-native init cavyExampleApp</pre><p>This integration test will not be interacting with the UI. If you’d like to see a cute guinea pig when you boot your app, please feel free to use the <a href="https://github.com/nmalesa/react-native-atomic-file-ops/blob/main/example/App.js">example </a><a href="https://github.com/nmalesa/react-native-atomic-file-ops/blob/main/example/App.js">App.js code</a> and <a href="https://github.com/nmalesa/react-native-atomic-file-ops/blob/main/example/assets/guineaPig.png">image</a> for the landing screen. Otherwise, you can keep the default React Native screen or even simplify it down to display “Hello, world!”</p><figure><img alt="AtomicFileOps example app landing screen with guinea pig image on iPhone 13 simulator" src="https://cdn-images-1.medium.com/max/960/1*44OZXmzOxVB-y0WAteNaqQ.png" /><figcaption>Example app landing screen</figcaption></figure><p>Install the following packages:</p><pre>yarn add react-native-atomic-file-ops</pre><pre>yarn add react-native-fs</pre><p><a href="https://github.com/itinance/react-native-fs">react-native-fs</a> provides read access to the Android and iOS file systems. You will use its API in your example test.</p><p>If you are building an iOS app, also install the packages with <a href="https://cocoapods.org/">CocoaPods</a>:</p><pre>cd ios</pre><pre>pod install</pre><p>Add Cavy to your app. Install the Cavy CLI on your local machine to quickly spin up the required scaffolding in your project and easily run tests.</p><pre>yarn add cavy --dev</pre><pre>yarn global add cavy-cli</pre><p>Once Cavy is installed in your project, initialize the Cavy tester:</p><pre>cavy init</pre><p>Your project should now contain an index.test.js file and a specs folder in the root directory. specs is where you will build your tests. It should contain an exampleSpec.js file.</p><p>Rename exampleSpec.js to atomicFileWritingSpec.js.</p><h4>Create the Test Helper Component</h4><p>In the specs directory, create a new file named fileWritingTestHelper.js. This file will store the testable React Native component that Cavy will refer to when running tests.</p><p>Input the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/992e8007728fb973610fc54aa49acae5/href">https://medium.com/media/992e8007728fb973610fc54aa49acae5/href</a></iframe><p>fileWritingTestHelper.js creates a testable React Native component that you can reference in your integration tests without needing to <a href="https://cavy.app/docs/getting-started/hooking-up-components#wrap-function">wrap</a> or assign refs to UI components.</p><p>To finish setting up the Cavy tester, open and configure index.test.js.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a25a642330aac754fd7b3cb60691e784/href">https://medium.com/media/a25a642330aac754fd7b3cb60691e784/href</a></iframe><h4>Write the Integration Test</h4><p>Let’s write the integration test! You will work in atomicFileWritingSpec.js.</p><p>Import the following modules:</p><pre>// The module you are testing<br>import AtomicFileOps from &#39;react-native-atomic-file-ops&#39;;</pre><pre>// Provides read access to the Android and iOS file systems<br>import RNFS from &#39;react-native-fs&#39;;</pre><p>Create the following variables:</p><pre>// You will write your data to fileName<br>const fileName = &#39;AtomicFileOpsCavy.test&#39;;<br><br>// filePath provides the absolute path to the file<br>const filePath = `${RNFS.DocumentDirectoryPath}/${fileName}`;</pre><p>Before running your test, you should have some checks in place:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e57e913c6a7b16534e7ff5424d5ea89/href">https://medium.com/media/0e57e913c6a7b16534e7ff5424d5ea89/href</a></iframe><p>Now you will write your test case. You will test that when you overwrite existing data on a file with new data, the file contains only the new, uncorrupted data.</p><p>The Cavy integration test will make sure that the contracts between the React Native and native modules work as expected.</p><p>After the beforeEach function and still inside export default function(spec) { }, add the test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70eab28326cb9461997e423331eea7b5/href">https://medium.com/media/70eab28326cb9461997e423331eea7b5/href</a></iframe><p>You can view atomicFileWritingSpec.js in its entirety on <a href="https://github.com/nmalesa/react-native-atomic-file-ops/blob/main/example/specs/atomicFileWritingSpec.js">GitHub</a>.</p><p>You are now ready to run your test.</p><h4>Run the Test</h4><p>Run the test from the command line:</p><pre>// Android<br>cavy run-android<br><br>// iOS<br>cavy run-ios</pre><figure><img alt="Terminal output of passing Cavy test on Android showing “1) tests react-native-atomic-file-ops: overwrites JSON” with a green check mark" src="https://cdn-images-1.medium.com/max/1024/1*LYdo5abPCAAuanaZlw6JQQ.png" /><figcaption>Terminal output of passing Cavy test on Android</figcaption></figure><p>Congratulations! You have successfully tested the integration of a React Native module with native modules.</p><p>If your test is not passing, it is helpful to set a breakpoint or use the debugger statement within the test to pause Cavy and check the test output.</p><h3>Conclusion</h3><p>Making sure your React Native modules work as expected across multiple platforms is integral to building a great app. Along with unit and E2E testing, integration testing should be an essential part of your React Native testing strategy.</p><p>Cavy provides an easy-to-use, customizable framework that supports integration testing on React Native. With the test helper component pattern, you can create testable components to use in your integration tests without having to interact with your app’s UI.</p><p>Of course, you can use Cavy for your UI and E2E testing, too!</p><p>Thank you for reading, and happy testing!</p><h4>Resources</h4><ul><li><a href="https://cavy.app/">Cavy</a></li><li><a href="https://github.com/nmalesa/react-native-atomic-file-ops/tree/main/example">Example app source code</a></li><li><a href="https://github.com/nmalesa/react-native-atomic-file-ops">react-native-atomic-file-ops</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aa18f43f296a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/how-to-test-if-your-react-native-module-integrates-with-android-and-ios-aa18f43f296a">How to Test If Your React Native Module Integrates With Android and iOS</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>