DevOps Part 3: The cursed Test

Hi there! Hope you made it through the other two parts well. This time we will be focusing on accomplishing the next tasks:

  • Setup a Unit Test framework and play with it
  • Setup a status page for tests results

These will be the final tasks in order to accomplish DevOps level 1 (this is just scratching the surface of all of what DevOps can do).

So for the first part, you need to choose you preferred unit testing framework, or if you don’t know any like I do, go with the fist link that popups with the search “unit test framework <your-coding-language>”. I went with Mocha and Chai since I’m using NodeJS. They pretty much go hand in hand. There’s this neat blog post about how to set properly Mocha and write your first tests! I did some toying around and looks pretty cool.

Now before going into setting up the page for the test results, you first need to create your nodeJS server, you can set it up pretty easily just by typing “npm init” and it will guide you through the process. You will end up with a “index.js” or something at the root of your directory. This will be your entry point. I will not bother to do custom HTML and stuff because it takes some time. If you want to do simple stuff consider using some sort of rendering tool, like Pug!

To get the results, I went ahead and used a “reporter” tool. This is a tool that allows Mocha to display the test results in console. But with some commands magic you can save those results into a file! Below I detail the commands used to save the results as a JSON file inside my server. You can look around to see the many options Mocha has for reporting the results.

mocha ./PATH/TO/TESTS/FOLDER --recursive -R json > outputFile.json

Now this recursively checks the “tests folder” for any tests inside and outputs that to the console as a JSON formatted string, then that I save it “>” into an “outputFile.json” file. And voilĂ !

After you have that file, inside your “index.js” you can set up an endpoint that will read the output file and parse it to a JSON object. With that its pretty simple to check if all tests passed or not, and any other content you want to have. If you want to remember how to read or write files in NodeJS, check this guide out!

So now it’s only a matter of sending in the response of the endpoint any content you want, you can send a HTML file with the contents of the result in a beautiful format! it’s up to you, the sky is the limit my friend.

So that concludes my series in DevOps, hope you had a fantastic time and well, keep learning, that’s what keeps us living ;). (now that are some deep thoughts you didn’t expect)

Leave a comment