Introducing unit testing to your project

At some point all of us have been programming in Java, admit it, there is nothing wrong with it as long as you stopped. It was while working with JUnit testing framework, that I have first coded unit testing myself. My experience with it: “I never want to write another test in my life”. However, Java is pretty old, as is JUnit testing, and the progress is not constant.

Thanks to one of my courses I am into JavaScript now. Recently, I needed to write some testing code once more, and while expecting another grueling experience, I found myself actually enjoying the process. In spite of have “Java” as a part of its name, JavaScript bares no resemblance with it in terms of testing framework’s quality and variety.

My framework of choice was Jest – a code testing platform developed and maintained by Facebook. The main slogan of Jest is “Zero configuration testing platform”, and they live up to it. After going through the installation procedure, doing npm install and configuring package.json file, I got right into writing.

The code itself is really easy to read and pretty self-explanatory. Here is a snippet:

function sum(a, b) {
  return a + b;
module.exports = sum;
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);

The test function is the one who runs the actual test by receiving the description of the test and the callback of the test case. Inside, we have expect that runs multitude of functions. The result of the testing could be viewed on the console formatted and easy to read:

PASS  ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)

It just works!

I ended up implementing around 12 test functions before refraining, after all I was not doing test-driven development…. For now anyways.

So that was testing, however the more challenging part was configuring my ESlint to let Jest do its magic. You see, as a part of my previous assignment, I had to configure linting with the set of rules defined by Airbnb. Since, I am using a framework, the function that are being used are not defined in my code. So “no-undefined” rule had to go. It doesn’t sound like a big deal, and it is not if you know what you are doing, which I didn’t. I’ll spare you the details by saying that an hour of going through ESlint documentation (it is awesome) and fiddling with VSCode editor configurations did the trick.

To conclude, this week was not so much about learning unit testing, but getting to know your development environment. And it a good thing too, because now I can ease pain of my PR’s reviewers.=)

One thought on “Introducing unit testing to your project”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s