<?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 Bartosz Sumper on Medium]]></title>
        <description><![CDATA[Stories by Bartosz Sumper on Medium]]></description>
        <link>https://medium.com/@bartosz.sumper?source=rss-b02dee16dbfe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XJPPkEhK8Y7GQcPOtyiW_A.jpeg</url>
            <title>Stories by Bartosz Sumper on Medium</title>
            <link>https://medium.com/@bartosz.sumper?source=rss-b02dee16dbfe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 22:19:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bartosz.sumper/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[Detox your GraphQL]]></title>
            <link>https://medium.com/swmansion/detox-your-graphql-74b58dca3221?source=rss-b02dee16dbfe------2</link>
            <guid isPermaLink="false">https://medium.com/p/74b58dca3221</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[automated-testing]]></category>
            <dc:creator><![CDATA[Bartosz Sumper]]></dc:creator>
            <pubDate>Wed, 05 Dec 2018 15:03:35 GMT</pubDate>
            <atom:updated>2018-12-05T15:03:35.783Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PRj9Y_BVuZuMHJygzswewg.png" /></figure><p>As your app is getting more complex, it requires more resources for maintaining its quality. If you haven’t written a single test for your codebase, that is about the time to start adding some. You may start with unit testing or snapshot testing. However, to have a complete picture of crucial flows in your app and if new changes are introducing any regression, you will sooner or later need to look into End-to-End (E2E) testing.</p><p>E2E tests tend to have an opinion of being hard to maintain, intermittently failing and needing a sophisticated setup. At Software Mansion we helped our clients add E2E tests to their React Native projects and, from our experience, it can be quite painless if done correctly, mostly thanks to a great E2E library for React Native called <a href="https://github.com/wix/Detox">Detox</a>.</p><p>What often makes E2E tests unstable is when the app we test rely on some data being fetched over the network. If we use staging version of our backend to run our tests against it, but someone changed the data on our tests account or deleted the account completely, the tests will fail. Moreover, we often need to test modifying the data on the server side, which can be a painful thing to do — just imagine restoring user’s address after edit form test fails. Also, sometimes our tests are tightly coupled to the account’s data and we would have to maintain not one, but five of these accounts in order to test different variants of user data. Therefore, using production or staging servers with preconfigured accounts is definitely not a scalable way to test an app.</p><p>When writing unit tests these type of problems are solved by mocking. Turns out we can apply the concept of mocking to E2E tests as well. Similarly to how we can mock methods or classes in our unit tests, we can mock the whole server that our app under test connects with. Doing so might be a pretty lengthy process — just imagine mocking all requests to your web service. However, if your server uses GraphQL, then you can use wonderful tools done by the Apollo team to mock it in no time!</p><p>In this article we show how GraphQL server can be mocked and then controlled from a Detox test to create an independent E2E testing environment for a React Native application.</p><h3>Detoxing the app</h3><p>First things first — before we show how to mock the server for our app, we need to have an app. We prepared a simple application for Pokemon Trainers to serve as an example. It displays trainer’s profile and a summary of their favorite Pokemon.</p><p>The app renders Home screen with two buttons: one for opening trainer profile screen and one for opening screen with the details about trainer’s favorite Pokemon. Let’s focus on the Pokemon screen. When we enter the screen it makes a query to the server to fetch some details and displays them in a column:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3ab97e6dcf9a8ff0bf2f7a32b935dc2/href">https://medium.com/media/d3ab97e6dcf9a8ff0bf2f7a32b935dc2/href</a></iframe><p>It is time to write our first test in Detox — if you haven’t heard about this tool, or would like to learn more about it, check the project’s <a href="https://github.com/wix/Detox">GitHub repository</a>.</p><p>Our test will check if we display Pokemon’s level on the screen or “MaxLvl” text when the Pokemon has reached maximum level (let’s assume the maximum level is 30).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a574eecac7795fab590980fbaee49da/href">https://medium.com/media/0a574eecac7795fab590980fbaee49da/href</a></iframe><p>The first problem we encounter is that we have to somehow “switch” which Pokemon will be returned to our app. To get different Pokemons, so that we can test both combinations, we could use two different user accounts. However, this approach does not scale well as the number of combinations we’d like to test grows. So in order to get different data depending on which test we run, we want to control the server from within our tests and make it return what is needed for a particular test case.</p><h3>Mocking GraphQL</h3><p>Before we start building mocked version of our server, we need to install all of the needed libraries in our React Native client app. The installed packages will only be used locally by a developer or on a Continuous Integration server, so we should add them to devDependencies:</p><pre>yarn add --dev express apollo-server-express</pre><p>There is one more thing we need to do in our client application to make it work with the mocked GraphQL server — we need to configure it to use the mocked server’s address. Usually, we have separate configuration files for different environments, such as production or staging, that are used to set various environmental variables including server’s address and API keys. The best thing to do is to add a new config file for E2E tests. To keep our example app simple, we hardcoded server address to be <a href="http://localhost:8089/graphql">http://localhost:8089/graphql</a>which is an address of the mocked server we run in the testing environment.</p><h3>1. Get your schema!</h3><p>If you worked with GraphQL before, you most likely have seen a GraphQL schema. GraphQL schema is a file with all types and mutation definitions your GraphQL server exposes. <a href="https://gist.github.com/barsumek/89dd857273828a6098858d6c1b0aa794">Here</a> is how the schema looks for our example app.</p><p>The best thing about mocking with Apollo Server is that it can automatically generate mocks for types’ definitions — the only thing you need is to provide it with a GraphQL schema. The most convenient way of getting the schema is by using <a href="https://gist.github.com/barsumek/e0c90f1b8dff17944c48d0607f794a64">introspection query</a>, that fetches the whole schema from your server (it works regardless of the backend stack your GraphQL server is using). Once you fetch the schema using this query, you can save it in a file and use for mocking.</p><p>Remember to keep the file in sync with the schema on your production server. Even though you could fetch it automatically each time you run the tests, we recommend not to do so. One of the most prominent advantages of using a mocked server is that it allows you to isolate your tests from an external dependency in a form of your production or staging server — fetching the schema from the server before each test would add that dependency back. To avoid getting your test randomly fail when the schema changes on your server, we recommend that you commit the schema file side by side with your E2E test. When your backend introduces breaking schema changes (which should not happen as often given it usually has to support older versions of client apps), you will likely need to make changes to your client code as well, and at that point have a chance to update the schema file used for E2E tests.</p><p>To understand the structure of our example GraphQL API, below we show GraphQL types’ definitions that are much more readable than the generated schema we linked to before.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5323208c36aef2dc80bcb69f42112cdb/href">https://medium.com/media/5323208c36aef2dc80bcb69f42112cdb/href</a></iframe><p>That is all what Apollo Server needs to generate default mocks and allow a creation of more sophisticated ones. However, before we begin with mocking, we have to learn how to start and stop the server from within Detox tests.</p><h3>2. Start the server!</h3><p>When Detox starts executing our tests, it has to initialize mocked server with all of the needed mocks for a given test, and make it available for the app to connect and send requests to.</p><p>First, we need to create our mocked instance of Apollo Server — we will use express and apollo-server-express to do it. The new version of apollo-server-express provide a convenient API for creating an instance with a mocked schema:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63f86669f78d99650936dfa894a585cb/href">https://medium.com/media/63f86669f78d99650936dfa894a585cb/href</a></iframe><p>We will need a HTTP server to listen for our app’s requests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c0c239e2ed29abaa5a16061f02beeed/href">https://medium.com/media/5c0c239e2ed29abaa5a16061f02beeed/href</a></iframe><p>We will have to restart the server after each test or after a test suite, so we need a way to stop it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/562e003f26df7ef02c7a9cc61586fc9a/href">https://medium.com/media/562e003f26df7ef02c7a9cc61586fc9a/href</a></iframe><p>That’s all! We are now ready to mock some Pokemons!</p><h3>3. Mock it!</h3><p>By default, Apollo mock server provides some defaults for the fields defined in our schema. However, with that default configuration, every String is mocked as Hello World and sometimes it is not what we desire. Other field types have their defaults too. E.g., fields of type Integer will return a random number. Thankfully, this behavior can be customized. In our case, we would like to mock the Pokemon’s level, which should be an integer between 0 and 30. Here’s how this can be done:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72b61579be65ceb34eb7147efb7cf870/href">https://medium.com/media/72b61579be65ceb34eb7147efb7cf870/href</a></iframe><p>The first mock returns 10 as the Pokemon’s level and the second one returns 30 which is the maximum level in our application. Now, we can pass one of these mocks to startGraphQLServer function. This is all we have to do to create a mocked GraphQL server that returns a custom value for a given field.</p><p>If you would like to make the mock server return a specific value for all the fields of a given type, e.g., to change the default Hello World value of String to John Doe, you can do that as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f0701b9a1a0a10574d0292c736fba0a/href">https://medium.com/media/2f0701b9a1a0a10574d0292c736fba0a/href</a></iframe><p>All String values will be John Doe now, but you can still override it by providing a different value for a specific field. This “global” type mocking is especially useful if you have custom scalars (non-standard types like Date), as you can easily set them to one value or provide a function that generates them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d029de159efa9a9638260551babeac0c/href">https://medium.com/media/d029de159efa9a9638260551babeac0c/href</a></iframe><p>You can combine mocks using destructuring operator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bbfc227ae370cd4afbeb280d1dee651/href">https://medium.com/media/6bbfc227ae370cd4afbeb280d1dee651/href</a></iframe><p>If you are interested in other mocking options, then <a href="https://www.apollographql.com/docs/apollo-server/features/mocking.html#Customizing-mocks">Apollo Documentation</a> describes all of them.</p><h3>4. Wrap it up!</h3><p>We have everything we need to mock the server in our tests. Let’s get back to it and add the needed code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d269c7440d5c9abb18a64cac0138161/href">https://medium.com/media/1d269c7440d5c9abb18a64cac0138161/href</a></iframe><p>To sum up, in each test we start our mocked GraphQL server and pass all of the needed mocks to it. Our client application is configured to send requests to the mocked server, so it receives data mocked by us specifically for a given test. Now, we can run Detox tests and see our tests pass — everything thanks to mocking!</p><h3>Summary</h3><p>We hope that the above example convinced you that testing RN apps is nothing to be afraid of, and also that GraphQL is a very powerful abstraction that not only simplifies data fetching but makes testing our apps much simpler.</p><p>If you are interested in mocking GraphQL APIs in the context of E2E testing, follow our blog and we will be happy to share more content on that in the future! Also, check out the example app on our <a href="https://github.com/SoftwareMansion/detox-your-graphql">Github repo</a> — you can start React Native app with a standalone mocked server or run Detox tests. Do not forget to star it! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74b58dca3221" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swmansion/detox-your-graphql-74b58dca3221">Detox your GraphQL</a> was originally published in <a href="https://medium.com/swmansion">Software Mansion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>