E2E Tests for API – Saving Nerves and Hours

Rate this content
Bookmark

Having adequate test coverage means a lot for the good API. But what can make the whole life pathetic is endless mocking of data and functions for integration tests. Every time you've changed the code you need to fix the mock. After several iterations the correct thought is - what's went wrong?


Alternative approach is e2e tests for the API. Which require only minimal mocks and data preparation. The rest - pure code of your API. Change the code - e2e test remains the same.

This talk is about my experience of switching from integration tests to e2e tests for the API, proc and cons and how I started to feel happy about tests.

8 min
19 Nov, 2021

Video Summary and Transcription

This Talk discusses the use of end-to-end tests for API development, specifically using the Nest.js framework. The process of initializing the Nest API for testing is explained, along with customization options such as overriding authentication guards. The benefits of end-to-end tests are highlighted, including ease of modification and serving as additional documentation for the API. The challenges of writing the initial version of the test and a trick for mocking the date in tests are also mentioned.

1. Introduction to End-to-End Tests for API

Short description:

Hello, everyone. My name is Valentin Kononov, and today my lightning talk is about end-to-end tests for API. How I did it. How it saved my nerves and hours and so on so forth. But still just a little bit about myself. So, I am a developer, mostly working with TypeScript and JavaScript these days, so with Node.js, and especially with the backend. And on one of my last projects, we had a backend written in NestJS, and we struggled a lot with testing it, especially when we need to change some functionality. That's how we tried to eventually move to end-to-end test, and here is what my talk about.

Hello, everyone. My name is Valentin Kononov, and today my lightning talk is about end-to-end tests for API. How I did it. How it saved my nerves and hours and so on so forth.

But still just a little bit about myself. So, I am a developer, mostly working with TypeScript and JavaScript these days, so with Node.js, and especially with the backend. And on one of my last projects, we had a backend written in NestJS, and we struggled a lot with testing it, especially when we need to change some functionality. That's how we tried to eventually move to end-to-end test, and here is what my talk about.

At the moment, I'm working at a Unity company, and it's all about fun and games and stuff, but I am working in the advertisement department, so we have a lot of services and both frontend and backends, and of course, we need to test them carefully.

Straight to the point, we don't have too much time. When we talk about API testing, what do we usually mean? First of all, we need to test the data flow, so how the data flows through our services, what are DB queries, so what input, output and data transformers. And in most cases, we just need to test if crude operations work fine and the syndication work fine and all the endpoints are correct, and what we usually do to achieve that. Of course, in most cases, we have integration tests.

And how does it work? Integration tests usually test the whole module, like the whole service, but the service is not something which is present by itself, it's something which communicates with other modules like a database or data repository or some syndication model, something else. So that's why to test it, we need to mock some dependencies. And when we did that and the test is working fine, what do we do next? We update the code, we make some new features. And what do we need to do next? We need to update the mock dependencies, so that the test keeps working. And what else? Yes, we need to update mock dependencies once again, and again, and again, and again.

So I really faced it. We had a really big, important, super mega-critical test about some export process. And every time we changed a minor thing in the export code, we needed to completely rewrite the whole test. That was bad. So, how usually the integration test could look like with all the mocks? So it's actually a huge pile of different kinds of mocking functions, like you see in the slides. Sometimes we mock the whole module, sometimes we mock just one function. And of course, when something is changed in the function, especially in the last example here, the update function was changed, and its output was changed. Even the format of output, like it was an object, became just one simple number. So, we needed to update the mock. And that actually sucked because we spent a lot of time just for supporting the tests. But it should be vice versa, so tests should save all the time for the development. So is there any alternatives of how we can proceed so that the whole process would be easier for us as developers? Basically, my recipe for that was end-to-end tests.

2. End-to-End Tests for API

Short description:

We discussed using the Nest.js framework for end-to-end tests in API development. End-to-end tests involve starting up the entire API in a testing environment and testing it point by point. The initialization of the Nest API involves creating a testing module, initializing it, and creating a Nest application. Tests can be customized by overriding authentication guards. The tests themselves involve making POST or GET requests to specific endpoints, sending request data, and expecting specific response codes and properties. End-to-end tests have pros such as being easy to fix, modify, and serving as additional documentation for the API. However, writing the initial version of the test can take time, especially when data preparation is required. A trick for mocking the date in tests is also shared.

We discussed with the team, okay, we have Nest.js framework. It has a lot of testing capabilities. So why don't we use it for end-to-end tests? And what is the end-to-end test for when we're talking about the API? It's when you start up the whole API, like the whole node service in your testing environment, and you test it at point by point.

So in this slide, you can see how initialization of the Nest API could look like. So you see, create testing module, you initialize it, you're like, create Nest application. In the end, you should close it and of course you can overwrite anything you want. For example, Nest, we use so-called guards for authentication. Here, we can override it, initialize and close in the very end when all the tests passed. And authentication guard mock is a pretty simple thing, which you just write once and use everywhere. It's really such simple stuff.

And here is how the whole test would look like. So you literally request, you do a POST or GET request to some certain endpoint, for some certain URL. And in this sample, because it's POST, you can send somebody details, like what kind of project data we should add. And you can expect, like here in the last line, you can expect some kind of code, like it should return 201. Which means successfully created. And you can also do a GET request and also form some URL, send requests, expect some code. And also expect some response. And check that the response properties are correct.

What do you mean? So the pros and cons. The pros, easy to fix, easy to modify and you test the whole workflow. And as you see right here the whole test is very readable and it's also an additional source of documentation for your API. But of course there are some cons and bad sides. So it can take time to write the initial version of this test because in some cases you need to prepare the data. Of course when you have just a crude API you can use just only get or post requests to create data and to make sure it's created. But in some cases you need to prepare the data in a database and this minimal database should be run somewhere in a continuous integration service. And just for you we have a really special trick, really small. Sometimes, in my experience, only once. But we needed to fake the date. I prepared some data and I should make sure that this data is created with this date now. And usually we did it like that. But in some code we actually use not just the now function, but also construct. And there is a little bit more comprehensive solution of how to mock the date for both places for now and for the constructor. So just make a screenshot, it's a very useful trick, see you later.

And here's the link for my slides, my website and my doc, which inspired me for all of that stuff. So, as a summary, make tests. It's really a great thing when you do tests. And entering tests makes the changes faster and you can cover the whole workflow with the last mocks and be much more happy.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.
TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Whether you're testing your UI or API, Cypress gives you all the tools needed to work with and manage network requests. This intermediate-level task demonstrates how to use the cy.request and cy.intercept commands to execute, spy on, and stub network requests while testing your application in the browser. Learn how the commands work as well as use cases for each, including best practices for testing and mocking your network requests.
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
The testing pyramid - the canonical shape of tests that defined what types of tests we need to write to make sure the app works - is ... obsolete. In this presentation, Roman Sandler and Gleb Bahmutov argue what the testing shape works better for today's web applications.
TestJS Summit 2022TestJS Summit 2022
27 min
Full-Circle Testing With Cypress
Top Content
Cypress has taken the world by storm by brining an easy to use tool for end to end testing. It’s capabilities have proven to be be useful for creating stable tests for frontend applications. But end to end testing is just a small part of testing efforts. What about your API? What about your components? Well, in my talk I would like to show you how we can start with end-to-end tests, go deeper with component testing and then move up to testing our API, circ
TestJS Summit 2022TestJS Summit 2022
20 min
Testing Web Applications with Playwright
Top Content
Testing is hard, testing takes time to learn and to write, and time is money. As developers we want to test. We know we should but we don't have time. So how can we get more developers to do testing? We can create better tools.Let me introduce you to Playwright - Reliable end-to-end cross browser testing for modern web apps, by Microsoft and fully open source. Playwright's codegen generates tests for you in JavaScript, TypeScript, Dot Net, Java or Python. Now you really have no excuses. It's time to play your tests wright.
TestJS Summit 2021TestJS Summit 2021
31 min
Test Effective Development
Top Content
Developers want to sleep tight knowing they didn't break production. Companies want to be efficient in order to meet their customer needs faster and to gain competitive advantage sooner. We ALL want to be cost effective... or shall I say... TEST EFFECTIVE!But how do we do that?Are the "unit" and "integration" terminology serves us right?Or is it time for a change? When should we use either strategy to maximize our "test effectiveness"?In this talk I'll show you a brand new way to think about cost effective testing with new strategies and new testing terms!It’s time to go DEEPER!

Workshops on related topic

React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Featured Workshop
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
The web has evolved. Finally, testing has also. Cypress is a modern testing tool that answers the testing needs of modern web applications. It has been gaining a lot of traction in the last couple of years, gaining worldwide popularity. If you have been waiting to learn Cypress, wait no more! Filip Hric will guide you through the first steps on how to start using Cypress and set up a project on your own. The good news is, learning Cypress is incredibly easy. You'll write your first test in no time, and then you'll discover how to write a full end-to-end test for a modern web application. You'll learn the core concepts like retry-ability. Discover how to work and interact with your application and learn how to combine API and UI tests. Throughout this whole workshop, we will write code and do practical exercises. You will leave with a hands-on experience that you can translate to your own project.
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
Top Content
WorkshopFree
Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop
TestJS Summit 2023TestJS Summit 2023
48 min
API Testing with Postman Workshop
WorkshopFree
In the ever-evolving landscape of software development, ensuring the reliability and functionality of APIs has become paramount. "API Testing with Postman" is a comprehensive workshop designed to equip participants with the knowledge and skills needed to excel in API testing using Postman, a powerful tool widely adopted by professionals in the field. This workshop delves into the fundamentals of API testing, progresses to advanced testing techniques, and explores automation, performance testing, and multi-protocol support, providing attendees with a holistic understanding of API testing with Postman.
1. Welcome to Postman- Explaining the Postman User Interface (UI)2. Workspace and Collections Collaboration- Understanding Workspaces and their role in collaboration- Exploring the concept of Collections for organizing and executing API requests3. Introduction to API Testing- Covering the basics of API testing and its significance4. Variable Management- Managing environment, global, and collection variables- Utilizing scripting snippets for dynamic data5. Building Testing Workflows- Creating effective testing workflows for comprehensive testing- Utilizing the Collection Runner for test execution- Introduction to Postbot for automated testing6. Advanced Testing- Contract Testing for ensuring API contracts- Using Mock Servers for effective testing- Maximizing productivity with Collection/Workspace templates- Integration Testing and Regression Testing strategies7. Automation with Postman- Leveraging the Postman CLI for automation- Scheduled Runs for regular testing- Integrating Postman into CI/CD pipelines8. Performance Testing- Demonstrating performance testing capabilities (showing the desktop client)- Synchronizing tests with VS Code for streamlined development9. Exploring Advanced Features - Working with Multiple Protocols: GraphQL, gRPC, and more
Join us for this workshop to unlock the full potential of Postman for API testing, streamline your testing processes, and enhance the quality and reliability of your software. Whether you're a beginner or an experienced tester, this workshop will equip you with the skills needed to excel in API testing with Postman.