<?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 Viskantas Juodenas on Medium]]></title>
        <description><![CDATA[Stories by Viskantas Juodenas on Medium]]></description>
        <link>https://medium.com/@viskantas.juodenas?source=rss-845b98e794f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*st_AQ4L0jaYtAvWc</url>
            <title>Stories by Viskantas Juodenas on Medium</title>
            <link>https://medium.com/@viskantas.juodenas?source=rss-845b98e794f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:03:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@viskantas.juodenas/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[Cypress: Mocking GraphQL Requests]]></title>
            <link>https://medium.com/revel-systems/cypress-mocking-graphql-requests-fc1afda4654?source=rss-845b98e794f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc1afda4654</guid>
            <category><![CDATA[cypress]]></category>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[mocking]]></category>
            <dc:creator><![CDATA[Viskantas Juodenas]]></dc:creator>
            <pubDate>Thu, 17 Dec 2020 16:33:49 GMT</pubDate>
            <atom:updated>2020-12-17T16:54:41.839Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*T1ZcBtQraUFwbcy-Wbn_VA.png" /></figure><p><em>To mock or not to mock</em> — that’s the question that popped up at <a href="https://revelsystems.com/">Revel</a> when we started using Cypress testing for one of our Frontend applications.</p><p>As with many other Frontend applications, ours doesn’t work in isolation. It communicates with several Backend services to post and retrieve data. And if we want to run Cypress tests on our Frontend app, we need to have API requests somehow resolved. There are 2 possible approaches:</p><ul><li><strong>Use real Backend services. </strong>Deploy Backend services that are required for Cypress testing. This way you will have real E2E tests and you will have confidence that your application as a whole is working without any issues.</li><li><strong>Mock request responses. </strong>If you don’t want to deploy Backend services to test your Frontend, mock all requests to return fake data. Sounds easier! And it will isolate the Frontend testing Backend service errors. But that’s no longer E2E testing. Cypress tests will no longer give confidence that the Frontend application is integrating with Backend services correctly.</li></ul><h4>Our Chosen Approach</h4><p>At the first glance <strong>using real Backend services</strong> for Cypress testing seemed to be a more appropriate option. With this approach, we would have confidence that our application works as a whole. But we quickly realized a problem — our application has a lot of configurable features. If we wanted to test our Frontend against all possible feature configurations, we would need to spend a lot of resources just to set up and deploy Backend services that would be capable of covering all test scenarios. For us, that simply wasn’t an option we wanted to pursue…</p><p>Then we explored the other solution of <strong>mocking all API requests</strong> to return fake data. This also has some drawbacks. When mocking API requests, your tests no longer assert the contract between Frontend and API. The fake data that you will use to mock requests can get outdated and no longer represent real data that is received from Backend services.</p><p>Luckily for us, <strong>Typescript</strong> and <strong>GraphQL</strong> save the day! As we are using GraphQL for our Backend services, they have easily accessible schemas that contain all requested input and output types. With the help of <a href="https://github.com/dotansimha/graphql-code-generator"><strong>graphql-codegen</strong></a><strong>,</strong> we simply fetch and convert those schemas to frontend Typescript types (neat!).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z0FwgwKZDiFdcVfFmBja2w.jpeg" /></figure><p>If these Typescript types built from the GraphQL schema are used properly throughout the Frontend code, you can feel confident the API integration is correct. Not only that, but we can also reuse those types to ensure our Cypress requests mock data has type safety. This way we know that our mock data is always accurate and reflects realistic API responses. Perfect!</p><p><strong>Type-safe</strong> code and request mocking provides us confidence in our Frontend integration with the API and allows us to test our frontend code in isolation. Because all requests are mocked, we don’t need to deploy any Backend services for Cypress testing, and our tests are quick as hell as the faked responses resolve immediately 🚀</p><p>Naturally, this request mocking option still leaves Backend services untested. For them, we have an API testing strategy completely separated from the Frontend application. And we still do some smoke testing to verify the whole application experience is working without errors. But this approach allows us to do the majority of testing separately for each service, giving us quicker testing setup and source of test failures identification.</p><h4><strong>Let’s See Some Examples!</strong></h4><p>We have created a suite of helpers that allows us to freely mock and control GraphQL operations when running Cypress tests. Though for this article we will display a barebone version of how we approach mocking GraphQL operations.</p><p>As an example, here is a GraphQL query operation that our Frontend might execute. Let’s see how we would mock this request.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee58fe4f241b37167b44e685b9f30174/href">https://medium.com/media/ee58fe4f241b37167b44e685b9f30174/href</a></iframe><p>Because we convert GraphQL schemas to Typescript types, we already have the response type in our Frontend code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97b92bd6cce53b53b971b11a097a1e08/href">https://medium.com/media/97b92bd6cce53b53b971b11a097a1e08/href</a></iframe><p>We can reuse this Typescript interface to add type safety to our API mock data. This way we will be sure that the response mock data is always correct. If the API contract would change for the productList query, after re-generating <em>graphql.types.ts</em> file and running Cypress tests we would receive a Typescript error telling us that the response mock data is outdated and no longer reflects what the products query would return.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9852e8ec7221da4fe7b5f859dbb14fb/href">https://medium.com/media/d9852e8ec7221da4fe7b5f859dbb14fb/href</a></iframe><p>Now we need to use this data to mock the productList GraphQL operation. Cypress does come with a <a href="https://docs.cypress.io/api/commands/route.html#Syntax">cypress.route()</a> function that allows mocking network requests, but it does not work as well for GraphQL requests. For GraphQL, it’s common practice to have all queries executed under a single request endpoint. We needed to create a custom helper function that allows us to mock requests not by URL, but by the GraphQL operation name.</p><p>Here is a simplified version of what we use. A helper function is going to mock all window.fetch calls that are executed with /graphql endpoint. The fetch stub will look for mock data based on the GraphQL operation name. If request mock data is found, it will be used as the request’s response. If not — the request will not be mocked.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8debe042b60c6dc74df61d5f487c1226/href">https://medium.com/media/8debe042b60c6dc74df61d5f487c1226/href</a></iframe><p>With everything set up, we can effortlessly mock GraphQL requests inside Cypress tests 🙌</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e0353e7d8755581a88f5c084b75964c/href">https://medium.com/media/0e0353e7d8755581a88f5c084b75964c/href</a></iframe><h4>Type Safety in Action</h4><p>To also demonstrate how type safety keeps our mock data correct, let’s imagine our productList query implementation has changed. The GraphQL service is modified to return uuid rather than the id field.</p><p>As we are constantly re-generating GraphQL service Typescript types, we would immediately have an updated response type in our Frontend code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56ea409398ebe56c7604e5e46cfb1e20/href">https://medium.com/media/56ea409398ebe56c7604e5e46cfb1e20/href</a></iframe><p>After the ProductListQuery interface update, our Cypress tests will immediately throw an error that our mock response data is not what API could possibly return. Tests will not run until you fix mock data to be accurate against the GraphQL contract.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w-jStfFzxF9u_vr3z-DHpQ.png" /></figure><h4>Handling Lots of Requests</h4><p><em>But what if I have a lot of requests to mock? Will I need to manually set up all mocks for every Cypress test?</em></p><p>Not necessarily! We approached this problem by setting up a list of default mock data for all API requests. These mocks are designed so the Frontend application could fully function when running Cypress tests without throwing errors. For every Cypress test, we call a helper function that automatically applies those mocks for all GraphQL requests when visiting a URL.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d9d55c0010183642bcc60b0afa167a4/href">https://medium.com/media/4d9d55c0010183642bcc60b0afa167a4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cbcd843b95d1b79b8712e820a773fcf/href">https://medium.com/media/1cbcd843b95d1b79b8712e820a773fcf/href</a></iframe><p>Writing a Cypress test with all requests mocked is now very straightforward. You can either call visitStubbed without any mock overrides if the default ones are sufficient for the test case or override any operation with a custom mock.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b307b2b39f9d482bb5995e49958e31d/href">https://medium.com/media/4b307b2b39f9d482bb5995e49958e31d/href</a></iframe><h4>Conclusion</h4><p>It’s possible to write Cypress tests by either deploying Backend services, or by mocking all requests made from the Frontend application. Which approach to choose is up to the development team, and the decision will depend on the application itself and what kind of tests are desired.</p><p>Our team went for the mocking option supported with strong type safety. This gave us fast tests, easy setup and we didn’t lose confidence in application integration with Backend services.</p><p>In the end, the most important thing is not the approach chosen, but producing tests that will give you confidence in your web application. We’d like to encourage you to try out things and see what works best for you to achieve this goal!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc1afda4654" width="1" height="1" alt=""><hr><p><a href="https://medium.com/revel-systems/cypress-mocking-graphql-requests-fc1afda4654">Cypress: Mocking GraphQL Requests</a> was originally published in <a href="https://medium.com/revel-systems">Revel Systems Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>