Testing: Do More With Less

Rate this content
Bookmark

How can you be confident that your code is well-tested? For me, the criteria are straightforward: you feel comfortable deploying it automatically to production on a Friday evening, and the release pipeline stays as green as an evergreen tree. In this talk, I'll share some approaches that I am following to hit both targets for Node.js apps (APIs, BFFs, etc).

This talk has been presented at JSNation 2024, check out the latest edition of this JavaScript Conference.

FAQ

Integration tests in a Node.js application focus on testing how different parts of the application work together, providing good confidence at a relatively low cost and high speed. They ensure that the application behaves as expected when handling actual HTTP requests and responses.

End-to-end tests provide very high confidence that everything works as expected, but they are costly to write and maintain. They require a stable environment and are slower to execute compared to unit and integration tests.

Enabling static linters and type checks is a cost-free way to catch errors early in the development process, improving code quality before you even write your first test.

Contract testing with Pact allows you to verify that the mocked interactions in your tests are up-to-date with the actual API implementations. It ensures that your mocks are valid and that the contract between your application and downstream APIs is still accurate.

Dora metrics include four key metrics: Deployment Frequency, Lead Time for Changes, Change Failure Rate, and Mean Time to Recover. Three of these metrics are directly impacted by good testing practices.

According to the Testing Trophy strategy, you should start with integration tests for both happy and non-happy flows. Next, add unit tests for edge cases and reusable code. Finally, cover very few business-critical flows with end-to-end tests.

Observability in production is crucial because it helps you quickly identify and respond to any abnormalities or issues that arise after deployment. It includes metrics, logging, and event tracking to ensure that you can monitor the health of your application in real-time.

You can highlight that automated testing reduces regressions and ensures that new features do not break existing functionality. This investment in testing leads to faster development cycles and increases the overall reliability and quality of the product.

Automated tests ensure that your code is reliable and can be deployed frequently without manual intervention. This reduces the risk of regressions and allows for faster delivery of new features.

The 'Testing Trophy' strategy, popularized by Kent C. Dodds, emphasizes writing the right types of tests. It suggests starting with integration tests, then adding unit tests for edge cases and reusable code, and finally, using end-to-end tests for critical business flows.

Eugene Fidelin
Eugene Fidelin
27 min
13 Jun, 2024

Comments

Sign in or register to post your comment.
  • Eugene Fidelin
    Eugene Fidelin
    eBay
    Slides: https://www.slideshare.net/slideshow/testing-do-more-with-less-jsnation-2024/269740608
Video Summary and Transcription
This talk focuses on practical approaches for testing Node.js applications, including the use of Dora metrics and the testing trophy strategy. It emphasizes the importance of covering critical flows with integration and end-to-end tests, while also considering the cost and speed of different test types. The speaker recommends mocking third-party services and using snapshot testing, but warns about the potential for false positives. Playwright is suggested as a preferred tool, and the importance of automated test execution is emphasized.
Available in Español: Pruebas: Haz más con menos

1. Introduction to Testing Node.js Applications

Short description:

My talk is testing do more with less, and it would be some practical approach how you can test your Node.js application. But before many teams reach that, can answer yes on those questions, they need to go through some paths and implement some things. So how do you make sure that you're moving the right direction? So there is so-called Dora metrics, and it stays for DevOps research and something, an A. It has four key metrics, and three of them are directly impacted by good testing.

Hi, everyone. Thanks for coming. My talk is testing do more with less, and it would be some practical approach how you can test your Node.js application.

First, try to answer the question like is your code well tested? But be honest. Do you feel comfortable deploying it automatically on a Friday evening and just going home? Who can raise their hand? Cool. We have some brave people here in the room. Nice. But does your release pipeline always stay as green as your Christmas tree? It's also important. That's cool, guys. Maybe next time we're going to do talk together. I will learn from you.

But before many teams reach that, can answer yes on those questions, they need to go through some paths and implement some things. So how do you make sure that you're moving the right direction? So there is so-called Dora metrics, and it stays for DevOps research and something, an A. It basically helps you to understand how good your team or the company in terms of performance and velocity, so how good you are at shipping software.

It has four key metrics, and three of them are directly impacted by good testing. First is deployment frequency. It basically measures how often your team successfully deploys to production. You can imagine if you have, say, manual testing still yet, I hope not, but some companies still have manual testing, you cannot deploy very often. You probably deploy once every second week or once per month. Ideally, you should deploy on demand. Lead time for change. Testing also has a big impact on this metric because even if you have automated tests, imagine you have very flaky and slow end-to-end tests. It means that whenever you push the master, whenever your deploy pipeline starts, it might take you hours, and if it fails, it might take you days or even weeks to get the green deploy pipeline. So again, you're very slow. And the last but not the least where the test plays an important role is change failure rate. It basically measures how often your deployment causes some production issue, and it has a direct connection with the test coverage. But not the test coverage like you measure, oh, 80% of my lines are covered. I'm happy. No. This is the actual coverage.

2. Optimizing Test Coverage and Approaches

Short description:

The testing trophy helps you focus on writing the right tests. Unit tests have low cost and high speed, while end-to-end tests have high cost and low speed. Integration tests provide good confidence at a moderate cost. Start with writing integration tests and consider writing unit tests for specific cases. Talk to your business and product teams to identify critical flows.

Are you testing the right things? Are you covering the flows that bring the most value to the company? So how you can approach your testing, how you can write less test and get higher confidence. So again, I assume you at least heard, or even maybe you're already applying the testing trophy. So it is advocated and it became popular after Kent C. Dodds I think wrote an article about it and he's doing trainings around it. But basically, the main idea of the testing trophy is to help you to focus on writing the right tests. If you see each test has its cost and gives you some level of confidence, and it has some speed.

So, unit tests, the cost are very low, yeah? It's very easy to write unit tests, especially nowadays we have chat GPT, you can ask and it will generate unit tests for you automatically. They give you, I would say, average confidence because if you only have unit tests, it might not be enough to deploy automatically but the speed is very high. You can execute them faster. On the other end of spectrum, end-to-end tests. The cost for them are very high because it's not only the cost to write the end-to-end test but also to maintain it in the long term. Like you need a special infrastructure, you need special environment where you need to run them, that environment should be stable. But it gives you very high confidence. Like if your end-to-end test is green, you're very confident that everything works as expected, but the speed is low, yeah? So, somewhere in between there are integration tests. So, the cost for them are very close to unit tests, they give you very good confidence. I would say from my experience, only having integration test sometimes is good enough and you can live without end-to-end test and their speed is still high. And you can see that in this testing trophy, it emphasizes that amount of integration test should be, should overcome the amount of end-to-end and unit test.

So, how do you approach this? So, step number zero. You remember? You can enable static linters and type checks. It's free, yeah? Everyone should do it. Step number one. You start with writing integration test. And you try to write integration test for each happy and non-happy flow. And then you check your test coverage. You identify, okay, maybe there are some rear-edge cases for which there is no sense to write an integration test, you can write a unit test to cover them. Or maybe within your application, there are some reusable code that you used across multiple parts. So, maybe you consider it as a library. So, it also makes sense to write unit test for that. And the last, in terms of testing, talk to your business people, talk to your product. Ask them to identify the very few business critical flows.

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

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Network Requests with Cypress
TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Cecilia Martinez, a technical account manager at Cypress, discusses network requests in Cypress and demonstrates commands like cydot request and SCI.INTERCEPT. She also explains dynamic matching and aliasing, network stubbing, and the pros and cons of using real server responses versus stubbing. The talk covers logging request responses, testing front-end and backend API, handling list length and DOM traversal, lazy loading, and provides resources for beginners to learn Cypress.
Testing Pyramid Makes Little Sense, What We Can Use Instead
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
Gleb Bahmutov
Roman Sandler
2 authors
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.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.

Workshops on related topic

Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
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
Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Top Content
Featured Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
How to Start With Cypress
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
Filip Hric
Filip Hric
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.
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.