Dissecting Complexity in Tests

Rate this content
Bookmark

Learn about the most common reasons for complexity in tests, how it manifests, and how to deal with that complexity to produce elegant tests even for the most complex systems.

This talk has been presented at TestJS Summit 2022, check out the latest edition of this JavaScript Conference.

FAQ

The two main types of complexity in software tests are complexity arising from the system being tested, such as a React component or a backend handler, and complexity introduced by the tests themselves.

When unsure of what to test, start by focusing on the most critical user-facing paths, such as the login and checkout flows for e-commerce products, or key functionalities in internal tools and libraries.

A useful testing philosophy is to 'test like the user,' which involves modeling tests from the user's perspective, emulating user actions, and ensuring assertions reflect user expectations.

Investing in a proper testing setup is crucial as it creates a controlled environment where tests can run smoothly, facilitating the handling of complexities such as HTTP request mocking or database interactions.

The main purpose of writing tests is to describe the intention behind the system, confirming that the code works as expected and validating the developer's intentions rather than just increasing code coverage.

The design of a system directly impacts its testability. Well-designed systems are easier to test, whereas poorly designed systems with intertwined functionalities and dependencies are harder to test effectively.

When choosing a testing environment, select one that matches where the code will run. For example, use a browser for testing Next.js pages and a Node.js-based framework for JavaScript functions to ensure effectiveness and efficiency.

Managing test complexity during the setup phase involves creating a robust environment that addresses complexities upfront, such as by mocking HTTP requests and preparing test databases, thus simplifying subsequent testing phases.

Artem Zakharchenko
Artem Zakharchenko
15 min
03 Nov, 2022

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Today's Talk discusses complexity in tests and how to effectively deal with it. The speaker emphasizes the importance of testing critical user-facing paths and modeling tests from the user's perspective. They also highlight the significance of creating a testing setup that allows any test to run smoothly and the implicit testability of a well-designed system. The Talk explores the impact of choosing the right testing environment, the role of testing setup in mitigating complexity, and the importance of test structure and expectations. The speaker provides practical tips for tackling complexity in tests, such as keeping tests flat, using helper utilities, and splitting tests into separate files.

1. Introduction to Complexity in Tests

Short description:

Today, I would like to speak about complexity in tests. Complexity is destined to happen, but it's how we choose to deal with it that matters. Complexity in tests can come from the system being tested or from the tests themselves. When dealing with complexity from the system, start by testing the most critical user-facing paths. When it comes to testing, model it from the user's perspective and invest enough into testing setup.

Hello, everyone. My name is Artem, and I'm a software engineer at Kotlinbox. Today I would like to speak about complexity in tests, but before I begin, allow me to ask you a simple question. Have you ever felt like writing a test for a feature would require more time and effort than the feature itself? Well, like me, you have. Then, chances are you were dealing with one or maybe multiple ways how complexity may manifest in your code base.

But you shouldn't feel bad about it, because no matter how great engineers we are and what incredible code we write, complexity is destined to happen. It's fine. Complexity in itself is not the issue. It's how we choose to deal or not to deal with how it manifests what matters. And while complexity can be a broad topic, for the sake of today's talk, I would like to reference to it as a quality or state of being hard to write, understand, and maintain a test. And when it comes to complexity in tests, it can be divided into two main groups. It's the complexity that comes from the system that we're testing, and this can be really any code. A React component, a backend route handler, or a JavaScript library. And complexity that comes from the tests that we're writing.

So let's start from the system. And one of the most common ways how people stumble upon complexity, coming from the code that they test, is that they don't know what to test. I'm pretty sure you've been in this situation. You open an existing file and it seems to be doing everything possible in the universe, and you have zero idea how to even approach testing that. Well, there's actually a great rule you can follow in these situations. It is, whenever you're in doubt, start by testing the most critical user-facing paths. So if you're building an e-commerce product, well, starting a test strategy from a logging flow or a checkout flow makes the most sense. And if you're developing internal tooling or libraries, then start from those happy paths that users expect, and that should set you on the right track.

And then, when you know what to test, the next biggest problem, the next challenge, is how to test that. And I think very often, when we feel struggle with how to approach testing, it's because we may miss some sort of testing philosophy. And one of the most useful approaches that I've adopted over the years is testing like the user. What it means is that whenever you write a test, try to model it from the user's perspective. So your test actions that you perform would emulate the actions that that user would do with your software. And your assertions that you write actually reflect user expectations as the result to their actions. And then another thing that helps tremendously is when you invest enough into testing setup. And I feel this is very often overlooked and it's a shame, because the testing setup is perhaps one of the most important phases that deals with the complexity.

2. Complexity in Tests: Purpose and Testability

Short description:

The point of this phase is to create a universe where any test can run without problems. Each test should have a purpose, which is to describe the intention behind the system. Testing the testability of a system is an implicit test in itself. Poorly designed systems are hard to test, while well-designed systems make testing easier.

Because the point of this phase is to create this universe, this box, where any test can run, or any test that you want to write can actually execute without problems. And I'm going to talk about testing setup a little bit later into the talk.

Okay, so when you know what to test and how to test, you may be stumbling into another problem that is writing too many tests. And it may sound like a good thing at first, but it's not really because each test should have a purpose. And we often seem to forget the purpose behind testing in general.

And we write tests not to gain code coverage or to have the CI passing, although we want that. We actually write tests for a single reason. And that is we write tests to describe intention behind the system. Think about it. Whenever you write a piece of logic in your code, you have some sort of intention. You want that code to do something. But unless you have an automated test to validate that intention, you have no proof that your code works as expected.

So the next time you approach a test, ask yourself a question. Is what I'm testing actually related to the intention behind this code? Because if it's not, chances are you can drop this test and still lose no value in your testing setup.

And then the other thing is that, well, real world is quite more complex than that. And sometimes they're objectively complex systems, right? Or are there? Because one thing I love about testing is that a testability of the system is an implicit test in itself.

Now, what this means is that when you have poorly designed, poorly architectured systems, as a consequence, they're going to be really hard to test. And the opposite stands true also. Let me give you a few examples of how this manifests.

So in this get user function, we fetch user from the database. But we also fetch all the post for the user. And this feels that it doesn't belong here. Because now to properly test this function, we need to also mock everything related to posts. And this is a stretch. What maybe the proper approach here would be to split this one function into two and test them all in isolation, which would be much easier.

Another example is related to dependencies that our code introduces. Like this shopping cart controller. You can see that in the constructor, we're creating a new database connection. Well maybe that's not a good idea because to test this controller now, we need to implicitly mock this database constructor somehow. Why not just pass it as an argument to the constructor, do dependency injection, and thus allow us to test, for example, against the test database during testing, which would make this whole experience much easier.

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.
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.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.

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.
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.
Detox 101: How to write stable end-to-end tests for your React Native application
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
Yevheniia Hlovatska
Yevheniia Hlovatska
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