Testing your inputs and dependencies like a boss

noriller

Bruno Noriller

Posted on December 18, 2022

Testing your inputs and dependencies like a boss

Before you break anything else, make sure you're not already dealing with broken stuff.

Don't wait until it's too late

Testing inputs and dependencies before making any other changes can save you a ton of time and frustration. Trust me, I've been there - trying to fix a problem in a test and realizing that the issue was actually with the input or a dependency. By proactively testing these elements, you can identify and fix any issues early on, ensuring that your code is reliable and stable.

Real-life examples of input and dependency testing struggles

I had one experience while testing the frontend where no matter what I did, I couldn't get one test to pass. And to make matters worse, when I tested it manually, it worked as intended.

So, where the heck was the problem?

It turns out that I had made myself a dev utility that would fill the form I was trying to test. When I actually tried to fill the form manually, then I could reproduce the error the tests were showing (a problem with how it was binding values).

Another case was the opposite - it worked manually, but the tests just wouldn't work. In this case, I was using AntD and it has a problem in that it doesn't work with userEvent.click and you have to use fireEvent.mouseDown (there were other similar problems with how it works in tests).

Be cautious and proactive with input and dependency testing

I'm not saying to test every single input, click, and other action, but it's important to be a little paranoid and check if the library you're using actually behaves as you expect in tests.

If it doesn't, then you have to create helpers that will do what you need when you want. (For example, I have helpers for the AntD case of needing the mousedown instead of the click).

And at the very least, you should test the inputs in tests you have no idea why it's not working as intended. (A test not passing? Test if the inputs are working as they should, remove them if you want after making it pass.)

Testing dependencies to ensure smooth updates

Problems with inputs and dependencies in tests are just the tip of the iceberg when it comes to issues you can avoid through testing.

Dependencies evolve over time and often come with breaking changes that can impact your code. By testing the dependencies you use, you can update them without worrying about breaking anything. This is especially important for component libraries used in the frontend, as updates can sometimes cause issues with layout and functionality. While it's not necessary to go overboard with dependency testing, it's a good idea to create a suite of tests for the dependencies you use, so that you can update them with confidence and verify that nothing is broken.

Double-checking dependencies to avoid problems

While you generally trust the dependencies you use, it's still a good idea to double-check them from time to time to make sure they are functioning as expected.

By doing so, you can identify and fix any issues that may have arisen and ensure that your own code is functioning properly.

Testing inputs and dependencies can help you proactively identify and fix issues, ensuring that your code is reliable and stable. It may take a little extra time upfront, but it can save you a lot of headaches and frustration in the long run.

So don't be lazy - take a few minutes to test your inputs and dependencies, and you'll be glad you did.


Cover Photo by Nubelson Fernandes on Unsplash

💖 💪 🙅 🚩
noriller
Bruno Noriller

Posted on December 18, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related