Skip to main content

Command Palette

Search for a command to run...

Testing JavaScript Applications: Ch1. Introduction to automated testing

Published
10 min read
Testing JavaScript Applications: Ch1. Introduction to automated testing
A

I’m a Frontend Software Engineer with 3+ years of experience building fast, scalable, and user-friendly web applications. I specialize in performance optimization, refactoring legacy code, and enhancing UI/UX, particularly in complex dashboard applications. I focus on improving load times and delivering smooth user experiences by collaborating closely with cross-functional teams.

In this series, I'm going to discuss Testing Javascript Applications book by Lucas da Costa, This book focuses on automation testing from the perspective of software developers to build software more confidently, in less time and also to enable the QA team to perform more creative and proactive work.

This book focuses on Putting those pieces together and answers questions like :

  • Should software engineers always write tests?

  • If so, which types of tests, for what, and how many?

  • How do software development and QA fit together?

This book contains 12 chapters divided into three main parts:

  1. Testing JavaScript Applications: covers what automated tests are, why they are important, the different types of automated tests, and how each type of test impacts your projects.

  2. Writing Tests: uses practical examples to teach you how to write the different types of tests that you learned about in the first part.

  3. Business Impact: covers complementary techniques to amplify the positive impact that writing tests can have on your business.

We will start with Chapter 1: Introduction to automated testing which explains what automated tests are and the advantages of writing them.


[1] What is an automated test?

In the software industry, the demand for new features is growing fast, making it critical to frequently ship software that works. That’s what automated tests are here for, Developers focus on building features and no longer afford time for manual testing. At this point, writing tests is not only good practice, it’s an industry standard with more job postings requiring some degree of knowledge about automated software testing. Tests are valuable for projects of all sizes because they:

  • Help avoid defects.

  • Reduce the cost of failure associated with a project.

So, Automated testing is simply replacing customer interactions with code.

[1.1] Replacing Customer Interactions with Code

Let’s think about how a user tells our program to add something to a shopping cart:

  • The application allows users to interact through a website that sends an HTTP request to the backend to add items to their cart. The addToCart function identifies the customer's cart based on the sender's session and updates the website according to the server's response.

  • We can replace the customer with a piece of software that can call the addToCart function. Now, we don’t depend on someone to manually add items to a cart and look at the response. Instead, we have a piece of code that does the verification for us. That’s an automated test.

  • Also, When testing, we can replicate user actions, such as adding items to a cart, to simulate real-world scenarios. By doing so, we can detect any potential bugs or issues before they occur in the production environment, which can be more costly to fix.

[1.2] Testing Functionality with External Dependencies

Tests can also interact with larger scopes by testing functionality that uses External Dependencies like a Database.

For example: if we want to test a scenario like "Adding a large number of macaroons to the cart":

  • Before that, The server must check the database to ensure that there are enough macaroons in stock before adding them to the cart. If the quantity requested is smaller than or equal to the amount in stock, the macaroons will be added to the cart, and the server will send a response to the client which updates accordingly.

NOTE: When conducting tests, it is important to use a separate database instead of the production database to avoid the risk of losing data or causing inconsistencies. A separate database also allows for easier identification of bugs as the test environment is fully controlled and not influenced by customer actions.

[1.3] Role of Tests in Software Development

Tests play a crucial role in software development, but it is important to understand their limitations. "Tests can’t prove your software works; they can only prove it doesn’t".

Tests serve as experiments, encoding the developer's expectations about how the software should behave. However, just because tests have passed in the past does not always mean the application will behave the same way in the future. That's why it's important to increase the efficacy of tests to ensure that they closely resemble what real users do. The more tests there are, and the more they simulate actual user behavior, the more guarantees they provide.

Despite all this, automated tests do not replace the need for manual testing. Manual testing, which involves verifying work as end-users would do, and investing time in exploratory testing, is still indispensable.


[2] Why automated tests matter?

Tests provide quick and fail-proof feedback, which improves the development process in several ways. We will examine how swift and precise feedback can:

  1. Makes the development workflow more uniform and predictable

  2. Facilitates issue reproduction and test case documentation

  3. Enhances collaboration among different teams

  4. Shortens the time to deliver high-quality software

[2.1] Predictability

Having a predictable development process means preventing the introduction of unexpected behavior during the implementation of a feature or the fixing of a bug. Also Reducing the number of surprises during development makes tasks easier to estimate and causes developers to revisit their work less often.

  • Manually ensuring that your entire software works as you expect is a time-consuming and error-prone process.

  • Tests improve this process because they decrease the time it takes to get feedback on the code you write and, therefore, make it quicker to fix mistakes.

To illustrate how tests can make development more predictable, let’s imagine we have a new feature: "Enable customers to track the status of their orders"

  • Manually testing the "tracking" feature of the shopping process would be time-consuming, requiring the tester to go through the (testing environment setup + shopping process) each time. This would involve:

    1. clearing databases

    2. opening a browser

    3. adding items to the cart

    4. scheduling a delivery, going through the checkout

    5. finally testing the tracking feature.

  • As we can see, the manual testing process is time-consuming, developers may write bigger chunks of code at a time, delaying feedback on the software's performance, and By the time the feedback arrives, it may be too late, making it difficult to locate bugs in the new code written before the testing process.
  • With an automated test, you can write less code before getting feedback. So your automated tests can call the trackOrder functionality directly and you can avoid touching unnecessary parts of your application before you’re sure that the trackOrder functionality works correctly.

Writing tests for small chunks of code can make it easier to locate bugs and issues. If a test fails after writing only a few lines of code, it is easier to identify the root cause of the issue. on the other hand, if a large amount of code is written before testing, it becomes more challenging to identify the source of the problem, especially if other parts of the application are affected.

  • Automated tests provide early detection of issues, enabling developers to make corrections quickly. Running tests frequently provides precise feedback on which part of the application is broken as soon as it occurs.

The less time it takes to get feedback once you’ve written code, the more predictable your development process will be.

[2.2] Reproducibility

In manual testing, the more steps a particular task has, the more likely a human is to make mistakes following them. This process is also long and error-prone, and there are many different ways to approach each testing step.

Automated tests ensure that these steps are followed to the letter, making it easier and quicker to reproduce bugs to fix them and ensure they aren’t present anymore. This is particularly important when testing applications that have multiple testing steps because we can ensure that these steps are followed exactly each time.

Also, If the list of test cases grows too long or if there are too many steps, the room for human mistakes gets bigger.

  • Here is an example of a case with multiple bugs that require the same repeated steps to be reproduced and tested manually:

  • Even if you decide to maintain a checklist for those test cases, you will have the overhead of keeping that documentation always up-to-date. If you ever forget to update it and something not described in a test case happens, who’s wrong—the application or the documentation?

Automated tests do the exact same actions every time you execute them. When a machine is running tests, it neither forgets any steps nor makes mistakes.

[2.3] Collaboration

When working with other developers, new concerns arise regarding how changes to different parts of the application may interfere with each other.

For example, if you are working on a new feature feat 1 and another developer (Alice) is working on another feature feat 2, How to ensure that the 2 features are not going to interfere with each other?

  • This requires coordination and communication to ensure that everyone's work is compatible and doesn't interfere with each other. This is done by manually testing changes that are resulted from the integration between both features, and this will be additional effort added for testing.

  • Besides being time-consuming, this process is also error-prone. You have to remember all the steps and edge cases to test in both your work and Alice’s, an also still need to follow them exactly.

Adding automated tests to solve this benefits everyone. Tests serve as up-to-date documentation, guiding further work and saving time in the future. If multiple developers are working on a project, automated tests ensure that changes to one part of the application don't interfere with another.

Writing tests whenever changes are made creates a productive collaboration cycle, where one developer helps those who will work on that part of the codebase next.

Well-written tests are the best documentation a developer can have because they are always up-to-date and need to pass.

This approach reduces communication overhead but does not eliminate the need for communication, which is the foundation stone for every project to succeed. Automated tests remarkably improve the collaboration process, but they become even more effective when paired with other practices, such as code reviews.

[2.4] Speed

While tests improve the development process, ultimately, what matters for the business is speed and correctness. When developers can produce code quickly, ensure it's bug-free, and integrate it with everyone else's work, the business succeeds. Preventing regressions and making deployments safer, and also contribute to the success of the business.

While writing tests does have a cost in terms of time, the benefits are greater than the drawbacks. Although writing a test may take more time initially than doing a manual test, the value extracted from it increases the more it's run.

For example:

  • If it takes five minutes to write an automated test and one minute to do a manual test, as soon as the automated test runs for the fifth time, it will have paid for itself. Given how frequently tests are run, the time invested in writing them is well worth it.

  • In contrast to manual testing, which will always take the same amount of time or more. As time passes, the total effort involved in manual tests grows much quicker.

The difference in effort and time between writing automated tests and performing manual testing:

Writing tests is like making an investment in stocks. You may pay a big price up-front, but you will continue to reap the payoffs for a long time.

  • Long-term projects benefit the most from tests because the longer the project runs, the more effort is saved, and the more you can invest in new features or other meaningful activities.

  • Short-term projects don't benefit much from tests since they don't live long enough to justify the effort saved with testing over time.


Summary

Automated tests are programs that automate the task of testing software. They interact with the application, compare its output to expected output, and provide feedback when output is incorrect.

Automated tests reduce the distance between writing code and receiving feedback, making the development process more predictable and reducing surprises. They always follow the same steps and make it easier to reproduce bugs. Well-written tests serve as documentation for developers and help businesses deliver higher-quality software faster, driving up profits.

Although writing tests requires an investment of time, the more often a test runs, the more time it saves, making them more critical for long-term projects.

References

Testing Javascript Applications