First steps with test-driven development

Jr Medina
4 min readMar 8, 2021

--

I have recently graduated from a Software Engineering bootcamp. During this 15-week course, I went from being afraid to touch my terminal, to learning how to further my knowledge based on a “let's try it out” mentality. I discovered that an error is nothing to be afraid of, but something to help lead you in the right direction. I have determined to read the errors and understand where they are trying to point me. If my next attempt gets me a new error, instead of frustration, I now feel I am getting closer to my goal.

This got me thinking about test-driven development. I have only dipped my toes into it, but it has already proven useful when solving problems. I remember seeing our head instructor lead a live assessment coding class where he was going to show the class his thought process on how he goes about solving assessments. While this lesson was exciting, I quickly realized I was lost when he started building test suites for the problems to prove that the functions he was creating were behaving as they were expected.

Que Jest! This is a framework for testing within JavaScript. It was very easy to get started, and I plan on using this any chance I get as it allows me to check those edge cases to make sure my function is fool proof. You can find the documentation following this URL https://jestjs.io/docs/en/getting-started.

I did have an issue getting the scripts set up in my package.json shown above, but by running “npm init” in my terminal, I was able to replace the scripts in the new package to “jest”.

Now we can create a multiplication function in a multiply.js file, and a multiply.test.js file, as the test suite will look for a test file. In the “multiply.js” file make a function that will multiply two numbers, and export the module as shown below.

Next, we will set a multiply variable and require the pathway leading to the “multiply.js file” in our “multiply.test.js” file.

With this in place, we can now write a test to check and see if our function works the way we want it to work. I will be using three keywords in this test suite, “test”, “expect” and “toEqual”; I have also seen “toBe”, but need to get further into the documentation to figure out best practices. As shown below, we have “test” followed by an open parenthesis and a description of what the function should do(as a string).

This is followed by an anonymous function that contains the word “expect” in the curly braces followed by a parenthesis that contains the “multiply” function invoked with our 2 times 5 arguments passed through as parameters.

At the end of this is our “toEqual(10)” which is exactly what it looks like. The more you look it also reads very nicely. We expect our multiply function with arguments of 2 and 5 to equal 10.

With this in place, you run “npm run test multiply.test.js” in your terminal and the framework will let you know if the function behaves correctly. Below is a few tests run and what it looks like with a passing test.

This is the start of my journey with Jest, as mentioned earlier, I plan on continuing my learning with TDD and will write a supplement blog as my learning becomes more advanced.

--

--

Jr Medina
Jr Medina

Written by Jr Medina

Soon-to-be Flatiron School Successor, with an enthusiasm towards Front-End Web Development, lifelong learning, and continuous improvement.

No responses yet